結果

問題 No.2064 Smallest Sequence on Grid
ユーザー tenten
提出日時 2022-09-05 14:23:06
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,910 bytes
コンパイル時間 2,587 ms
コンパイル使用メモリ 78,508 KB
実行使用メモリ 759,044 KB
最終ジャッジ日時 2024-11-20 14:28:22
合計ジャッジ時間 49,257 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 15 TLE * 2 MLE * 12
権限があれば一括ダウンロードができます

ソースコード

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