結果

問題 No.971 いたずらっ子
ユーザー Mishan5055Mishan5055
提出日時 2020-04-04 04:23:55
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,492 bytes
コンパイル時間 1,971 ms
コンパイル使用メモリ 76,512 KB
実行使用メモリ 90,608 KB
最終ジャッジ日時 2023-09-16 05:19:02
合計ジャッジ時間 13,301 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 662 ms
88,612 KB
testcase_06 AC 635 ms
86,756 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 169 ms
56,624 KB
testcase_11 AC 122 ms
56,316 KB
testcase_12 AC 123 ms
56,240 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 124 ms
55,668 KB
testcase_18 AC 124 ms
56,112 KB
testcase_19 AC 124 ms
56,248 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 121 ms
55,988 KB
testcase_24 AC 123 ms
55,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static int INF=1000000;
    public static int min2(int a,int b){
        return (a<b)?a:b;
    }
    
    public static boolean mining2(int a,int b){
        if(a<b){
            a=b;
            return true;
        }else{
            return false;
        }
    }
    
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        int H=sc.nextInt();
        int W=sc.nextInt();
        int[][] map=new int[H][W];
        
        for(int i=0;i<H;i++){
            String tmp=sc.next();
            for(int j=0;j<W;j++){
                if(tmp.charAt(j)=='.'){
                    map[i][j]=0;
                }else{
                    map[i][j]=1;
                }
            }
        }
        
        int[][] dist=new int[H][W];
        
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                dist[i][j]=INF;
            }
        }

        dist[0][0]=0;
        boolean change=true;
        
        while(change){
            change=false;
            for(int i=0;i<H;i++){
                for(int j=0;j<W;j++){
                    if(i!=0){
                        if(map[i][j]==0){
                            if(dist[i][j]>dist[i-1][j]+1){
                                dist[i][j]=dist[i-1][j]+1;
                                change=true;
                            }
                        }else{
                            if(dist[i][j]>dist[i-1][j]*2+2){
                                dist[i][j]=dist[i-1][j]*2+2;
                                change=true;
                            }
                        }
                    }
                    if(i!=H-1){
                        if(map[i][j]==0){
                            if(dist[i][j]>dist[i+1][j]+1){
                                dist[i][j]=dist[i+1][j]+1;
                                change=true;
                            }
                        }else{
                            if(dist[i][j]>dist[i+1][j]*2+2){
                                dist[i][j]=dist[i+1][j]*2+2;
                                change=true;
                            }
                        }
                    }
                    if(j!=0){
                        if(map[i][j]==0){
                            if(dist[i][j]>dist[i][j-1]+1){
                                dist[i][j]=dist[i][j-1]+1;
                                change=true;
                            }
                        }else{
                            if(dist[i][j]>dist[i][j-1]*2+2){
                                dist[i][j]=dist[i][j-1]*2+2;
                                change=true;
                            }
                        }
                    }
                    if(j!=W-1){
                        if(map[i][j]==0){
                            if(dist[i][j]>dist[i][j+1]+1){
                                dist[i][j]=dist[i][j+1]+1;
                                change=true;
                            }
                        }else{
                            if(dist[i][j]>dist[i][j+1]*2+2){
                                dist[i][j]=dist[i][j+1]*2+2;
                                change=true;
                            }
                        }
                    }
                }
            }
            
            if(!change){
                break;
            }
        }
        
        System.out.println(dist[H-1][W-1]);
    }
}
0