結果

問題 No.124 門松列(3)
ユーザー kou6839kou6839
提出日時 2015-01-15 20:53:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 249 ms / 5,000 ms
コード長 2,032 bytes
コンパイル時間 2,181 ms
コンパイル使用メモリ 75,176 KB
実行使用メモリ 60,060 KB
最終ジャッジ日時 2023-09-04 13:16:48
合計ジャッジ時間 8,791 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 241 ms
60,060 KB
testcase_01 AC 127 ms
55,556 KB
testcase_02 AC 126 ms
56,292 KB
testcase_03 AC 249 ms
59,968 KB
testcase_04 AC 235 ms
59,660 KB
testcase_05 AC 235 ms
59,364 KB
testcase_06 AC 128 ms
57,584 KB
testcase_07 AC 128 ms
55,668 KB
testcase_08 AC 128 ms
55,476 KB
testcase_09 AC 127 ms
55,804 KB
testcase_10 AC 127 ms
55,612 KB
testcase_11 AC 128 ms
56,056 KB
testcase_12 AC 130 ms
55,700 KB
testcase_13 AC 131 ms
55,728 KB
testcase_14 AC 137 ms
55,720 KB
testcase_15 AC 136 ms
55,732 KB
testcase_16 AC 195 ms
56,484 KB
testcase_17 AC 147 ms
55,892 KB
testcase_18 AC 136 ms
54,000 KB
testcase_19 AC 145 ms
55,620 KB
testcase_20 AC 156 ms
55,956 KB
testcase_21 AC 138 ms
55,968 KB
testcase_22 AC 156 ms
55,752 KB
testcase_23 AC 206 ms
59,272 KB
testcase_24 AC 211 ms
59,016 KB
testcase_25 AC 218 ms
59,516 KB
testcase_26 AC 194 ms
58,568 KB
testcase_27 AC 190 ms
58,164 KB
testcase_28 AC 232 ms
59,732 KB
testcase_29 AC 236 ms
59,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int column = sc.nextInt();
        int row = sc.nextInt();
        int[][] maze = new int[row][column];
        int[] vx = {0,1,0,-1};
        int[] vy={-1,0,1,0};
        int[][] fromx = new int[row][column];
        int[][] fromy = new int[row][column];
        int[][] kai = new int[row][column];
        for(int i=0;i<row;i++){
        	for(int j=0;j<column;j++){
        		maze[i][j]=sc.nextInt();
        	}
        }
        Queue<Integer> quex = new LinkedList<Integer>();
        Queue<Integer> quey = new LinkedList<Integer>();
        Queue<Integer> moto = new LinkedList<Integer>();
        Queue<Integer> hoo = new LinkedList<Integer>();
        quex.offer(1);
        quey.offer(0);
        quex.offer(0);
        quey.offer(1);
        hoo.offer(1);
        hoo.offer(1);
        moto.offer(maze[0][0]);
        moto.offer(maze[0][0]);
        while(!quex.isEmpty()){
        	int nx = quex.poll();
        	int ny = quey.poll();
        	int nowho = hoo.poll();
        	int moto1 = moto.poll();
        	for(int i=0;i<4;i++){
        		int nex = nx+vx[i];
        		int ney = ny+vy[i];
        		if(nex<0 || nex > column-1 || ney<0 || ney > row-1) continue;
        		if( ((moto1 > maze[ny][nx] && maze[ney][nex]>maze[ny][nx]) || (moto1 < maze[ny][nx] && maze[ney][nex]<maze[ny][nx]))&&kai[ney][nex]<4 && (fromx[ney][nex]!=nx||fromy[ney][nex]!=ny)&&moto1!=maze[ny][nx]&&moto1 != maze[ney][nex] && maze[ny][nx]!=maze[ney][nex]){
        			quex.offer(nex);
        			quey.offer(ney);
        			moto.offer(maze[ny][nx]);
        			hoo.offer(nowho+1);
        			fromx[ney][nex]=nx;
        			fromy[ney][nex]=ny;
        			kai[ney][nex]++;
        			if(nex == column-1 && ney == row-1){
                		System.out.println(nowho+1);
                		return;
                	}
        		}
        	}
        }
        System.out.println(-1);
    }
}		
0