結果

問題 No.86 TVザッピング(2)
ユーザー scachescache
提出日時 2014-12-06 21:58:19
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,294 bytes
コンパイル時間 3,910 ms
コンパイル使用メモリ 76,040 KB
実行使用メモリ 56,764 KB
最終ジャッジ日時 2023-09-02 08:59:00
合計ジャッジ時間 9,053 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 121 ms
55,832 KB
testcase_02 AC 120 ms
55,620 KB
testcase_03 AC 119 ms
56,216 KB
testcase_04 WA -
testcase_05 AC 122 ms
56,192 KB
testcase_06 AC 121 ms
55,996 KB
testcase_07 AC 118 ms
55,756 KB
testcase_08 AC 151 ms
56,180 KB
testcase_09 AC 157 ms
56,764 KB
testcase_10 AC 168 ms
56,360 KB
testcase_11 AC 121 ms
55,680 KB
testcase_12 AC 121 ms
55,576 KB
testcase_13 AC 123 ms
55,776 KB
testcase_14 AC 149 ms
56,400 KB
testcase_15 AC 150 ms
56,464 KB
testcase_16 AC 151 ms
56,652 KB
testcase_17 WA -
testcase_18 AC 123 ms
55,620 KB
testcase_19 AC 123 ms
56,148 KB
testcase_20 AC 122 ms
55,984 KB
testcase_21 AC 166 ms
56,448 KB
testcase_22 AC 119 ms
55,792 KB
testcase_23 AC 150 ms
56,168 KB
testcase_24 AC 124 ms
56,092 KB
testcase_25 AC 119 ms
56,072 KB
testcase_26 AC 116 ms
56,192 KB
testcase_27 AC 119 ms
55,796 KB
testcase_28 AC 118 ms
56,192 KB
testcase_29 AC 151 ms
56,288 KB
testcase_30 AC 119 ms
55,656 KB
testcase_31 AC 121 ms
55,712 KB
testcase_32 AC 119 ms
56,176 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};
	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;
				map[i][j] = 'a';
				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]=='.'){
//						System.out.println(j +" " + i +" " + k);
						map[i][j] = 'a';
						res |= rec(nx, ny, k);
						res = isOk();
					}
				}
				
				map[i][j] = res ? 'a' : '.';
				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 boolean rec(int x, int y, int curDir) {
		
//		for(int i=0;i<map.length;i++)
//			System.out.println(Arrays.toString(map[i]));
//		System.out.println();
		
		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[y][x] = 'a';
			if(map[ny][nx]=='a')
				return true;
			
			boolean res = rec(nx, ny, (curDir+i)%4);
			if(res)
				return true;
			map[y][x] = '.';
		}
		
		return false;
	}

}
0