結果

問題 No.2064 Smallest Sequence on Grid
ユーザー tentententen
提出日時 2022-09-05 14:23:06
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,910 bytes
コンパイル時間 2,152 ms
コンパイル使用メモリ 79,052 KB
実行使用メモリ 758,752 KB
最終ジャッジ日時 2024-04-30 17:19:04
合計ジャッジ時間 50,614 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
51,284 KB
testcase_01 AC 88 ms
51,292 KB
testcase_02 AC 52 ms
50,348 KB
testcase_03 AC 82 ms
51,452 KB
testcase_04 AC 80 ms
51,244 KB
testcase_05 AC 82 ms
51,388 KB
testcase_06 AC 82 ms
51,228 KB
testcase_07 AC 90 ms
51,188 KB
testcase_08 AC 97 ms
53,256 KB
testcase_09 AC 94 ms
53,056 KB
testcase_10 AC 147 ms
56,544 KB
testcase_11 AC 144 ms
56,248 KB
testcase_12 AC 141 ms
56,624 KB
testcase_13 AC 155 ms
56,920 KB
testcase_14 AC 152 ms
56,736 KB
testcase_15 AC 138 ms
56,384 KB
testcase_16 AC 152 ms
56,332 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 MLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 MLE -
testcase_30 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static char[][] field;
    static int h;
    static int w;
    static String[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        h = sc.nextInt();
        w = sc.nextInt();
        field = new char[h][];
        for (int i = 0; i < h; i++) {
            field[i] = sc.next().toCharArray();
        }
        dp = new String[h][w];
        dp[h - 1][w - 1] = String.valueOf(field[h - 1][w - 1]);
        System.out.println(dfw(0, 0));
    }
    
    static String dfw(int r, int c) {
        if (dp[r][c] == null) {
            if (r == h - 1) {
                dp[r][c] = field[r][c] + dfw(r, c + 1);
            } else if (c == w - 1) {
                dp[r][c] = field[r][c] + dfw(r + 1, c);
            } else {
                String s1 = dfw(r, c + 1);
                String s2 = dfw(r + 1, c);
                if (s1.compareTo(s2) < 0) {
                    dp[r][c] = field[r][c] + s1;
                } else {
                    dp[r][c] = field[r][c] + s2;
                }
            }
        }
        return dp[r][c];
    }
    
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0