結果
問題 | No.13 囲みたい! |
ユーザー | core123 |
提出日時 | 2019-04-18 17:55:18 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 284 ms / 5,000 ms |
コード長 | 2,324 bytes |
コンパイル時間 | 2,839 ms |
コンパイル使用メモリ | 82,352 KB |
実行使用メモリ | 62,276 KB |
最終ジャッジ日時 | 2023-10-22 08:22:10 |
合計ジャッジ時間 | 7,425 ms |
ジャッジサーバーID (参考情報) |
judge9 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 136 ms
57,320 KB |
testcase_01 | AC | 136 ms
57,212 KB |
testcase_02 | AC | 137 ms
57,684 KB |
testcase_03 | AC | 259 ms
61,344 KB |
testcase_04 | AC | 241 ms
60,656 KB |
testcase_05 | AC | 271 ms
61,768 KB |
testcase_06 | AC | 242 ms
60,920 KB |
testcase_07 | AC | 272 ms
62,052 KB |
testcase_08 | AC | 284 ms
62,276 KB |
testcase_09 | AC | 268 ms
61,948 KB |
testcase_10 | AC | 213 ms
59,824 KB |
testcase_11 | AC | 253 ms
60,836 KB |
testcase_12 | AC | 188 ms
57,748 KB |
testcase_13 | AC | 221 ms
60,656 KB |
testcase_14 | AC | 226 ms
60,560 KB |
testcase_15 | AC | 145 ms
57,708 KB |
ソースコード
import java.util.*; import java.util.stream.*; import static java.lang.Math.*; public class Main{ public static int[] dx={-1,0,0,1}; public static int[] dy={0,1,-1,0}; public static void main(String... args){ Scanner sc=new Scanner(System.in); int w=sc.nextInt(); int h=sc.nextInt(); int[][] m=new int[h][w]; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ m[i][j]=sc.nextInt(); } } boolean[][] reached=new boolean[h][w]; boolean ans=false; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ if(reached[i][j])continue; final int num=m[i][j]; Deque<Cell> deq=new ArrayDeque<>(); deq.add(new Cell(null,new Pair(i,j))); while(deq.size()>0){ Cell cell=deq.poll(); if(reached[cell.next.x][cell.next.y]){ //put("(x,y)=(%d,%d)",cell.next.x,cell.next.y); put("possible"); return; } reached[cell.next.x][cell.next.y]=true; for(int k=0;k<4;k++){ int x=cell.next.x+dx[k]; int y=cell.next.y+dy[k]; if(x<0||x>=h||y<0||y>=w)continue; if(m[x][y]!=num)continue; Pair next=new Pair(x,y); if(next.equals(cell.prev))continue; deq.addLast(new Cell(cell.next,next)); } } } } put("impossible"); } public static class Cell{ final Pair prev,next; Cell(Pair prev,Pair next){ this.prev=prev; this.next=next; } } public enum Direction{ UP, DOWN, LEFT, RIGHT; } public static class Pair{ final int x,y; Pair(int x,int y){ this.x=x; this.y=y; } @Override public String toString(){ return String.format("Pair(%d,%d)",x,y); } @Override public int hashCode(){ return x*1000+y; } @Override public boolean equals(Object obj){ if(obj==null)return false; if(obj instanceof Pair){ Pair pair=(Pair)obj; return x==pair.x&&y==pair.y; } return false; } } public static void print(Object object){ System.out.print(object); } public static void put(Object object) { System.out.println(object); } public static void put(){ System.out.println(); } public static void print(String format,Object... args){ System.out.print(String.format(format,args)); } public static void put(String format,Object... args) { System.out.println(String.format(format,args)); } }