結果

問題 No.971 いたずらっ子
ユーザー SugihaMSugihaM
提出日時 2020-04-21 17:10:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 566 ms / 2,000 ms
コード長 1,148 bytes
コンパイル時間 1,999 ms
コンパイル使用メモリ 74,808 KB
実行使用メモリ 86,796 KB
最終ジャッジ日時 2023-08-07 08:06:40
合計ジャッジ時間 10,714 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 566 ms
86,624 KB
testcase_01 AC 562 ms
86,124 KB
testcase_02 AC 540 ms
84,696 KB
testcase_03 AC 537 ms
86,796 KB
testcase_04 AC 526 ms
84,296 KB
testcase_05 AC 530 ms
86,308 KB
testcase_06 AC 508 ms
84,872 KB
testcase_07 AC 158 ms
56,340 KB
testcase_08 AC 424 ms
66,608 KB
testcase_09 AC 403 ms
64,776 KB
testcase_10 AC 159 ms
55,832 KB
testcase_11 AC 120 ms
56,092 KB
testcase_12 AC 120 ms
56,272 KB
testcase_13 AC 128 ms
56,148 KB
testcase_14 AC 172 ms
56,328 KB
testcase_15 AC 128 ms
57,944 KB
testcase_16 AC 135 ms
55,788 KB
testcase_17 AC 120 ms
56,104 KB
testcase_18 AC 122 ms
56,128 KB
testcase_19 AC 122 ms
56,144 KB
testcase_20 AC 121 ms
56,220 KB
testcase_21 AC 123 ms
55,808 KB
testcase_22 AC 124 ms
55,536 KB
testcase_23 AC 120 ms
56,256 KB
testcase_24 AC 119 ms
55,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main{
  public static void main(String[] args) throws Exception {
    Scanner sc=new Scanner(System.in);
    int h = sc.nextInt();
    int w = sc.nextInt();
    int[][] a = new int[h][w];

    for(int i = 0;i < h;i++){
      String s = sc.next();
      for(int j = 0;j < w;j++){
        if(s.charAt(j) == '.'){
          a[i][j]=0;
        }else{
          a[i][j]=1;
        }
      }
    }

    int[][] dp =new int[h][w];
    int INM = 1145141919;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        dp[i][j]=INM;
      }
    }

    dp[0][0] = 0;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        if(a[i][j] == 0){
          if (i > 0) {
    				dp[i][j] = Math.min(dp[i][j], dp[i-1][j] + 1);
    			}
    			if (j > 0) {
    				dp[i][j] = Math.min(dp[i][j], dp[i][j-1] + 1);
    			}
        }else{
          if (i > 0){
            dp[i][j] = Math.min(dp[i][j], dp[i-1][j] + 1 + i + j);
          }
          if (j > 0){
            dp[i][j] = Math.min(dp[i][j], dp[i][j-1] + 1 + i + j);
          }
        }
      }
    }
    System.out.println(dp[h - 1][w - 1]);
  }
}
0