結果

問題 No.86 TVザッピング(2)
ユーザー scache
提出日時 2014-12-06 21:58:19
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 2,294 bytes
コンパイル時間 3,671 ms
コンパイル使用メモリ 80,096 KB
実行使用メモリ 47,800 KB
最終ジャッジ日時 2025-06-20 13:57:56
合計ジャッジ時間 7,972 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 28 WA * 2
権限があれば一括ダウンロードができます

ソースコード

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