結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
core123
|
| 提出日時 | 2019-04-18 17:55:18 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 269 ms / 5,000 ms |
| コード長 | 2,324 bytes |
| コンパイル時間 | 2,419 ms |
| コンパイル使用メモリ | 82,652 KB |
| 実行使用メモリ | 59,220 KB |
| 最終ジャッジ日時 | 2024-09-22 09:21:23 |
| 合計ジャッジ時間 | 6,463 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
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));
}
}
core123