結果
問題 | No.13 囲みたい! |
ユーザー | scache |
提出日時 | 2014-11-12 03:34:21 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 270 ms / 5,000 ms |
コード長 | 1,870 bytes |
コンパイル時間 | 2,784 ms |
コンパイル使用メモリ | 78,072 KB |
実行使用メモリ | 47,048 KB |
最終ジャッジ日時 | 2024-04-21 12:14:39 |
合計ジャッジ時間 | 6,343 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 117 ms
40,244 KB |
testcase_01 | AC | 129 ms
41,336 KB |
testcase_02 | AC | 120 ms
40,004 KB |
testcase_03 | AC | 264 ms
47,048 KB |
testcase_04 | AC | 247 ms
46,568 KB |
testcase_05 | AC | 270 ms
47,020 KB |
testcase_06 | AC | 241 ms
46,792 KB |
testcase_07 | AC | 247 ms
46,744 KB |
testcase_08 | AC | 257 ms
46,600 KB |
testcase_09 | AC | 256 ms
46,784 KB |
testcase_10 | AC | 201 ms
43,596 KB |
testcase_11 | AC | 248 ms
46,620 KB |
testcase_12 | AC | 187 ms
41,876 KB |
testcase_13 | AC | 217 ms
45,704 KB |
testcase_14 | AC | 219 ms
45,852 KB |
testcase_15 | AC | 141 ms
41,320 KB |
ソースコード
import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; /** * yukicoder no.13 * @author scache * */ public class Main { public static void main(String[] args) { Main p = new Main(); } public Main() { Scanner sc = new Scanner(System.in); int w = sc.nextInt(); int h = sc.nextInt(); int[][] field = new int[h+2][w+2]; for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++){ field[i][j] = sc.nextInt(); } } solve(field); } public void solve(int[][] field) { int[][] visit = new int[field.length][field[0].length]; for(int i=0;i<visit.length;i++) Arrays.fill(visit[i], -1); for(int i=1;i<=field.length-2;i++){ for(int j=1;j<=field[i].length-2;j++){ if(visit[i][j]>=0) continue; visit[i][j] = 0; if(bfs(field, visit, j, i)){ System.out.println("possible"); return; } } } System.out.println("impossible"); } int[] dx = {-1, 0, 1, 0}; int[] dy = {0, 1, 0, -1}; private boolean bfs(int[][] field, int[][] visit, int cx, int cy){ LinkedList<Point> q = new LinkedList<Point>(); LinkedList<Point> from = new LinkedList<Point>(); q.offer(new Point(cx, cy)); from.offer(new Point(cx, cy)); while(!q.isEmpty()){ Point p = q.poll(); Point f = from.poll(); for(int i=0;i<4;i++){ int nx = p.x + dx[i]; int ny = p.y + dy[i]; if(field[p.y][p.x]!=field[ny][nx]) continue; if(visit[ny][nx]>0 && (nx!=f.x || ny!=f.y)){ return true; }else if(visit[ny][nx]<0){ q.offer(new Point(nx, ny)); from.offer(new Point(p.x, p.y)); visit[ny][nx] = visit[p.y][p.x]+1; } } } return false; } private class Point{ public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } }