結果

問題 No.424 立体迷路
ユーザー yun_appyun_app
提出日時 2016-09-28 16:36:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 2,921 bytes
コンパイル時間 2,285 ms
コンパイル使用メモリ 75,416 KB
実行使用メモリ 52,444 KB
最終ジャッジ日時 2023-09-18 16:56:46
合計ジャッジ時間 4,420 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,460 KB
testcase_01 AC 45 ms
49,520 KB
testcase_02 AC 45 ms
49,412 KB
testcase_03 AC 45 ms
49,628 KB
testcase_04 AC 46 ms
49,632 KB
testcase_05 AC 44 ms
49,528 KB
testcase_06 AC 45 ms
49,584 KB
testcase_07 AC 44 ms
49,544 KB
testcase_08 AC 45 ms
49,472 KB
testcase_09 AC 44 ms
49,492 KB
testcase_10 AC 44 ms
49,572 KB
testcase_11 AC 44 ms
49,472 KB
testcase_12 AC 45 ms
49,576 KB
testcase_13 AC 45 ms
49,976 KB
testcase_14 AC 46 ms
49,552 KB
testcase_15 AC 45 ms
49,812 KB
testcase_16 AC 46 ms
49,508 KB
testcase_17 AC 66 ms
52,436 KB
testcase_18 AC 66 ms
50,616 KB
testcase_19 AC 60 ms
51,584 KB
testcase_20 AC 61 ms
51,436 KB
testcase_21 AC 44 ms
49,468 KB
testcase_22 AC 45 ms
49,456 KB
testcase_23 AC 44 ms
49,552 KB
testcase_24 AC 80 ms
52,444 KB
testcase_25 AC 76 ms
51,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] lines = br.readLine().split(" ");
        int h = Integer.parseInt(lines[0]);
        int w = Integer.parseInt(lines[1]);
        
        lines = br.readLine().split(" ");
        int sh = Integer.parseInt(lines[0])-1;
        int sw = Integer.parseInt(lines[1])-1;
        int gh = Integer.parseInt(lines[2])-1;
        int gw = Integer.parseInt(lines[3])-1;
        
        int[][] map = new int[h][w];
        boolean[][] visit = new boolean[h][w];
        for(int i=0;i<h;i++){
            lines = br.readLine().split("");
            for(int j=0;j<w;j++){
                map[i][j] = Integer.parseInt(lines[j]);
                visit[i][j] = false;
            }
        }
        
        Deque<Pair> deq = new ArrayDeque<Pair>();
        Pair start = new Pair(sh,sw);
        Pair goal  = new Pair(gh,gw);
        deq.add(start);
        visit[sh][sw] = true;
        start:while(!deq.isEmpty()){
            Pair p = deq.removeFirst();
            for(Pair next:getNext(map,p)){
                if(visit[next.h][next.w] == false){
                    visit[next.h][next.w] = true;
                    if(next.equals(goal)){
                        break start;
                    }
                    deq.add(next);
                }
            }
        }
        
        System.out.println(visit[gh][gw]?"YES":"NO");
    }
    
    static private ArrayList<Pair> getNext(int[][] map,Pair p){
        int now_height = map[p.h][p.w];
        ArrayList<Pair> ret = new ArrayList<Pair>();
        
        for(Pair next:Pair.nexts){
            try{
                if(Math.abs(map[p.h+next.h][p.w+next.w] - now_height) <= 1){
                    ret.add(new Pair(p.h+next.h,p.w+next.w));
                }
                if( map[p.h+next.h][p.w+next.w] < now_height
                &&  map[p.h+next.h*2][p.w+next.w*2] == now_height){
                    ret.add(new Pair(p.h+next.h*2,p.w+next.w*2));
                }
            }catch(Exception e){}
        }
        
        return ret;
    }
    
    private static class Pair{
        static Pair[] nexts = new Pair[]{new Pair(1,0),new Pair(0,1),new Pair(-1,0),new Pair(0,-1)};
        public int h;
        public int w;
        public Pair(int h,int w){
            this.h = h;
            this.w = w;
        }
        public boolean equals(Pair p){
            return this.h==p.h && this.w == p.w;
        }
        public String toString(){
            return new StringBuilder().append(h).append(",").append(w).toString();
        }
    }
}
0