結果

問題 No.86 TVザッピング(2)
ユーザー scachescache
提出日時 2014-12-07 01:48:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 302 ms / 5,000 ms
コード長 2,183 bytes
コンパイル時間 3,380 ms
コンパイル使用メモリ 74,752 KB
実行使用メモリ 57,040 KB
最終ジャッジ日時 2023-08-26 14:59:18
合計ジャッジ時間 9,649 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,988 KB
testcase_01 AC 126 ms
56,148 KB
testcase_02 AC 125 ms
55,848 KB
testcase_03 AC 126 ms
55,884 KB
testcase_04 AC 124 ms
56,116 KB
testcase_05 AC 124 ms
55,576 KB
testcase_06 AC 124 ms
55,996 KB
testcase_07 AC 124 ms
56,108 KB
testcase_08 AC 153 ms
56,276 KB
testcase_09 AC 222 ms
57,040 KB
testcase_10 AC 302 ms
55,844 KB
testcase_11 AC 128 ms
56,180 KB
testcase_12 AC 126 ms
55,800 KB
testcase_13 AC 125 ms
55,968 KB
testcase_14 AC 172 ms
56,208 KB
testcase_15 AC 203 ms
56,624 KB
testcase_16 AC 193 ms
56,564 KB
testcase_17 AC 128 ms
55,936 KB
testcase_18 AC 131 ms
55,864 KB
testcase_19 AC 131 ms
55,836 KB
testcase_20 AC 127 ms
55,540 KB
testcase_21 AC 224 ms
56,576 KB
testcase_22 AC 127 ms
55,972 KB
testcase_23 AC 190 ms
56,196 KB
testcase_24 AC 129 ms
55,800 KB
testcase_25 AC 124 ms
55,512 KB
testcase_26 AC 125 ms
55,808 KB
testcase_27 AC 125 ms
56,100 KB
testcase_28 AC 126 ms
55,516 KB
testcase_29 AC 156 ms
56,272 KB
testcase_30 AC 125 ms
56,260 KB
testcase_31 AC 126 ms
56,088 KB
testcase_32 AC 127 ms
55,776 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++;
				}
				
				if(d<=1)
					continue;
				
				tc++;
				if(tc>(n+m)*4)
					break;
				for(int k=0;k<4;k++){
					sx = j;
					sy = i;
					rec(j, i, k);
					res |= isOk();
				}

				if(res)
					break;
			}
			
			if(tc>(n+m)*4 || 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;
		int c = 0;
		while(!isEnd){
			c++;
			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