結果

問題 No.13 囲みたい!
ユーザー kuramukuramu
提出日時 2016-02-11 13:18:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 775 ms / 5,000 ms
コード長 1,543 bytes
コンパイル時間 2,583 ms
コンパイル使用メモリ 76,560 KB
実行使用メモリ 39,808 KB
最終ジャッジ日時 2024-04-21 12:22:29
合計ジャッジ時間 5,160 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
36,996 KB
testcase_01 AC 58 ms
36,824 KB
testcase_02 AC 59 ms
36,768 KB
testcase_03 AC 107 ms
39,408 KB
testcase_04 AC 99 ms
38,968 KB
testcase_05 AC 775 ms
39,768 KB
testcase_06 AC 104 ms
39,088 KB
testcase_07 AC 148 ms
39,808 KB
testcase_08 AC 116 ms
39,292 KB
testcase_09 AC 110 ms
39,112 KB
testcase_10 AC 66 ms
37,172 KB
testcase_11 AC 106 ms
39,132 KB
testcase_12 AC 63 ms
36,940 KB
testcase_13 AC 78 ms
38,160 KB
testcase_14 AC 96 ms
38,144 KB
testcase_15 AC 61 ms
36,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {
	 static int W;
	 static int H;
	 static int[] dx = {-1,1,0,0};
	 static int[] dy = {0,0,-1,1};
	 public static void main(String[] args) {
	      BufferedReader stdReader =new BufferedReader(new InputStreamReader(System.in));
	      try {
	    	  String[] line = stdReader.readLine().split(" ");
	    	  W = Integer.parseInt(line[0]);
	    	  H = Integer.parseInt(line[1]);
	    	  int[][] board = new int[H][W];
	    	  for(int i=0;i<H;i++){
	    		  String[] temp = stdReader.readLine().split(" ");
	    		  for(int j=0;j<W;j++){
	    			  board[i][j] = Integer.parseInt(temp[j]);
	    		  }
	    	  }
	    	  for(int i=0;i<H;i++){
	    		  for(int j=0;j<W;j++){
	    			  if(isCheck(i,j,-1,-1,board,board[i][j])){
	    				  System.out.println("possible");
	    				  return;
	    			  }    				  	    			  
	    		  }
	    	  }
			  System.out.println("impossible");
	      } catch (IOException e) {
			e.printStackTrace();
	      }
	}
	 
	 public static boolean isCheck(int x,int y,int fromx,int fromy,int[][] board,int num){
		 board[x][y] = 0;
		 for(int i=0;i<dx.length;i++){
			 if(x+dx[i]>=0 && x+dx[i]<H && y+dy[i]>=0 && y+dy[i]<W && !(x+dx[i]==fromx && y+dy[i]==fromy)){
				 if(board[x+dx[i]][y+dy[i]]==0){
					 return true; 
				 }
				 if(board[x+dx[i]][y+dy[i]]==num){
					 if(isCheck(x+dx[i], y+dy[i],x,y,board,num)){
						 return true;
					 }
				 }
			 }
		 }
		 board[x][y] = num;
		 return false;
	 }
}
0