結果

問題 No.86 TVザッピング(2)
ユーザー scachescache
提出日時 2014-12-07 01:27:30
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,184 bytes
コンパイル時間 3,640 ms
コンパイル使用メモリ 75,560 KB
実行使用メモリ 56,588 KB
最終ジャッジ日時 2023-09-02 09:10:56
合計ジャッジ時間 9,221 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 123 ms
56,008 KB
testcase_02 AC 124 ms
55,496 KB
testcase_03 AC 123 ms
55,424 KB
testcase_04 AC 120 ms
55,840 KB
testcase_05 AC 124 ms
55,484 KB
testcase_06 AC 120 ms
55,792 KB
testcase_07 AC 121 ms
55,744 KB
testcase_08 AC 153 ms
56,512 KB
testcase_09 AC 177 ms
56,348 KB
testcase_10 AC 187 ms
56,436 KB
testcase_11 AC 121 ms
56,036 KB
testcase_12 AC 120 ms
55,580 KB
testcase_13 AC 122 ms
55,492 KB
testcase_14 AC 158 ms
56,160 KB
testcase_15 AC 159 ms
56,308 KB
testcase_16 AC 157 ms
56,352 KB
testcase_17 WA -
testcase_18 AC 124 ms
56,136 KB
testcase_19 AC 125 ms
55,948 KB
testcase_20 AC 123 ms
55,544 KB
testcase_21 AC 164 ms
56,140 KB
testcase_22 AC 125 ms
55,492 KB
testcase_23 AC 159 ms
56,172 KB
testcase_24 AC 126 ms
56,164 KB
testcase_25 AC 123 ms
55,552 KB
testcase_26 AC 120 ms
55,684 KB
testcase_27 AC 122 ms
55,972 KB
testcase_28 AC 124 ms
55,816 KB
testcase_29 AC 151 ms
56,588 KB
testcase_30 AC 120 ms
56,016 KB
testcase_31 AC 124 ms
55,488 KB
testcase_32 AC 124 ms
55,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main86 {
	public static void main(String[] args) {
		Main86 p = new Main86();
	}

	char[][]  map;
	public Main86() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		map = new char[n][];
		for(int i=0;i<map.length;i++)
			map[i] = sc.next().toCharArray();
		solve(n, m);
	}


	int[] dx = {-1, 0, 1, 0};
	int[] dy = {0, 1, 0, -1};
	int sx = -1;
	int sy = -1;
	public void solve(int n, int m) {
		boolean res =false;
		int tc = 0;
		for(int i=0;i<map.length;i++){
			for(int j=0;j<map[i].length;j++){
				if(map[i][j]!='.')
					continue;
					
				int d = 0;
				for(int k=0;k<4;k++){
					int nx = j+dx[k];
					int ny = i+dy[k];
					if(0<=nx && nx<map[0].length && 0<=ny && ny<map.length && map[ny][nx]=='.')
						d += (k%2==0 ? 1 : 10);
				}
				
				if(d != 11 && d!=22)
					continue;
				
				tc++;
				if(tc>10)
					break;
				for(int k=0;k<4;k++){
					sx = j;
					sy = i;
					rec(j, i, k);
					res = isOk();
				}

				if(res)
					break;
			}
			
			if(tc>10 || res)
				break;
		}
		
		System.out.println(res ? "YES" : "NO");
	}

	private boolean isOk() {
		boolean ok = true;
		for(int i=0;i<map.length;i++){
			for(int j=0;j<map[i].length;j++){
				if(map[i][j]=='.')
					ok = false;
				else if(map[i][j] == 'a')
					map[i][j] = '.';
				
			}
		}
		return ok;
	}

	private void rec(int x, int y, int curDir) {
//		System.out.println(" " + x +" " + y +" " + curDir);
//		for(int i=0;i<map.length;i++)
//			System.out.println(Arrays.toString(map[i]));
//		System.out.println();
		boolean isEnd = false;
		while(!isEnd){
			isEnd =true;
			for(int i=0;i<2;i++){
				int nx = x + dx[(curDir+i)%4];
				int ny = y + dy[(curDir+i)%4];
				
				if(nx<0 || map[0].length<=nx || ny<0 || map.length<=ny)
					continue;
				
				if(map[ny][nx]!='.')
					continue;
				
				map[ny][nx] = 'a';
				if(nx==sx && ny==sy){
					isEnd=true;
					break;
				}
				x = nx;
				y = ny;
				curDir = (curDir+i)%4;
				isEnd = false;
				break;
			}
		}
		
	}

}
0