結果

問題 No.124 門松列(3)
ユーザー 37zigen37zigen
提出日時 2016-05-01 17:19:37
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,149 bytes
コンパイル時間 2,802 ms
コンパイル使用メモリ 79,216 KB
実行使用メモリ 58,360 KB
最終ジャッジ日時 2024-04-15 08:21:43
合計ジャッジ時間 8,661 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 251 ms
58,172 KB
testcase_01 WA -
testcase_02 AC 136 ms
54,468 KB
testcase_03 AC 257 ms
58,116 KB
testcase_04 AC 243 ms
57,732 KB
testcase_05 AC 244 ms
57,892 KB
testcase_06 AC 136 ms
54,272 KB
testcase_07 AC 133 ms
54,376 KB
testcase_08 AC 135 ms
54,312 KB
testcase_09 AC 134 ms
54,176 KB
testcase_10 AC 136 ms
54,048 KB
testcase_11 AC 135 ms
54,040 KB
testcase_12 AC 135 ms
53,992 KB
testcase_13 AC 139 ms
54,184 KB
testcase_14 AC 141 ms
54,132 KB
testcase_15 AC 142 ms
54,000 KB
testcase_16 WA -
testcase_17 AC 146 ms
54,120 KB
testcase_18 WA -
testcase_19 AC 149 ms
54,168 KB
testcase_20 WA -
testcase_21 AC 142 ms
54,096 KB
testcase_22 AC 168 ms
54,268 KB
testcase_23 AC 210 ms
57,136 KB
testcase_24 AC 222 ms
57,668 KB
testcase_25 AC 219 ms
57,336 KB
testcase_26 AC 197 ms
56,456 KB
testcase_27 AC 187 ms
54,404 KB
testcase_28 AC 249 ms
58,360 KB
testcase_29 AC 240 ms
57,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;

public class Main{
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int w=sc.nextInt();
		int h=sc.nextInt();
		int[][] table=new int[h][w];
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				table[i][j]=sc.nextInt();
			}
		}
		BFS bfs=new BFS(table);
		int ans=bfs.bfs(0, 0, w-1, h-1);
		System.out.println(ans);
	}

	int[] dx={1,-1,0,0};
	int[] dy={0,0,-1,1};
	class BFS{
		Queue<Vertice> q;
		final long INF=Long.MAX_VALUE/4;
		int[][] table;
		int w,h;
		boolean[][] arrived;

		BFS(int[][] table){
			h=table.length;
			w=table[0].length;
			this.table=new int[h][w];
			for(int i=0;i<h;i++){
				for(int j=0;j<w;j++){
					this.table[i][j]=table[i][j];
				}
			}
			q=new ArrayDeque<Vertice>(h*w);
			arrived=new boolean[h][w];
		}
		int bfs(int sx,int sy,int gx,int gy){
			q.add(new Vertice(sx,sy,-1,-1,-1,-1,-1,-1,0));
			int ans=-1;
			while(!q.isEmpty()){
				Vertice v=q.poll();
				if(v.x==gx&&v.y==gy){
					ans=v.d;
					break;
				}
				for(int i=0;i<4;i++){
					int nx=v.x+dx[i];
					int ny=v.y+dy[i];
					if(nx<0||ny<0||nx>=w||ny>=h)continue;
					if(arrived[ny][nx])continue;
					if(v.d>=1){
						if(!isKadomatsu(table[ny][nx],table[v.y][v.x],table[v.y1][v.x1])){
							continue;
						}
					}
					q.add(new Vertice(nx,ny,v.x,v.y,v.x1,v.y1,v.x2,v.y2, v.d+1));
					if(v.d>=2)arrived[ny][nx]=true;
				}
			}
			return ans;
		}
	}
	class Vertice{
		int x;
		int y;
		int d;
		int x1,y1,x2,y2,x3,y3;//(x1,y1)は一つ前の座標、(x2,y2)は二つ前の座標
		Vertice(int x,int y,int x1,int y1,int x2,int y2,int x3,int y3,int d){
			this.x=x;
			this.y=y;
			this.d=d;
			this.x1=x1;
			this.y1=y1;
			this.x2=x2;
			this.y2=y2;
			this.x3=x3;
			this.y3=y3;
		}
	}
	boolean isKadomatsu(int n1,int n2,int n3){
		if(n2>n1&&n2>n3&&n1!=n3){
			return true;
		}else if(n2<n1&&n2<n3&&n1!=n3){
			return true;
		}else {
			return false;
		}
	}
	void tr(Object...o){System.out.println(Arrays.deepToString(o));}
}
0