結果
問題 | No.86 TVザッピング(2) |
ユーザー |
|
提出日時 | 2014-12-07 01:48:39 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 257 ms / 5,000 ms |
コード長 | 2,183 bytes |
コンパイル時間 | 3,964 ms |
コンパイル使用メモリ | 86,356 KB |
実行使用メモリ | 48,324 KB |
最終ジャッジ日時 | 2025-06-20 13:58:04 |
合計ジャッジ時間 | 8,459 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
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; } } } }