結果
問題 | No.86 TVザッピング(2) |
ユーザー |
|
提出日時 | 2014-12-07 01:27:30 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,184 bytes |
コンパイル時間 | 3,032 ms |
コンパイル使用メモリ | 78,380 KB |
実行使用メモリ | 54,480 KB |
最終ジャッジ日時 | 2024-06-11 16:00:05 |
合計ジャッジ時間 | 8,221 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 WA * 2 |
ソースコード
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;}}}}