結果

問題 No.2064 Smallest Sequence on Grid
ユーザー tentententen
提出日時 2022-09-05 15:05:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,475 ms / 3,000 ms
コード長 2,467 bytes
コンパイル時間 2,604 ms
コンパイル使用メモリ 78,612 KB
実行使用メモリ 92,504 KB
最終ジャッジ日時 2024-04-30 18:23:48
合計ジャッジ時間 20,591 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
50,228 KB
testcase_01 AC 55 ms
50,400 KB
testcase_02 AC 58 ms
50,356 KB
testcase_03 AC 56 ms
49,976 KB
testcase_04 AC 58 ms
50,128 KB
testcase_05 AC 55 ms
50,044 KB
testcase_06 AC 55 ms
50,392 KB
testcase_07 AC 56 ms
50,468 KB
testcase_08 AC 58 ms
49,996 KB
testcase_09 AC 58 ms
50,388 KB
testcase_10 AC 61 ms
49,988 KB
testcase_11 AC 61 ms
50,356 KB
testcase_12 AC 58 ms
49,940 KB
testcase_13 AC 62 ms
50,508 KB
testcase_14 AC 74 ms
50,980 KB
testcase_15 AC 67 ms
50,744 KB
testcase_16 AC 72 ms
50,948 KB
testcase_17 AC 402 ms
77,068 KB
testcase_18 AC 400 ms
77,292 KB
testcase_19 AC 399 ms
77,308 KB
testcase_20 AC 1,397 ms
92,208 KB
testcase_21 AC 1,460 ms
91,940 KB
testcase_22 AC 1,207 ms
92,504 KB
testcase_23 AC 944 ms
86,492 KB
testcase_24 AC 975 ms
86,580 KB
testcase_25 AC 939 ms
86,464 KB
testcase_26 AC 1,466 ms
88,500 KB
testcase_27 AC 1,475 ms
88,424 KB
testcase_28 AC 393 ms
77,336 KB
testcase_29 AC 1,305 ms
80,892 KB
testcase_30 AC 383 ms
74,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int h = sc.nextInt();
        int w = sc.nextInt();
        char[][] field = new char[h][];
        for (int i = 0; i < h; i++) {
            field[i] = sc.next().toCharArray();
        }
        StringBuilder ans = new StringBuilder();
        HashSet<Integer> current = new HashSet<>();
        HashSet<Integer> next = new HashSet<>();
        current.add(0);
        for (int i = 0; i < h + w - 1; i++) {
            char min = (char)('z' + 1);
            for (int x : current) {
                int r = x / w;
                int c = x % w;
                if (ans.length() == i) {
                    ans.append(field[r][c]);
                }
                if (r < h - 1) {
                    if (field[r + 1][c] < min) {
                        min = field[r + 1][c];
                        next.clear();
                        next.add((r + 1) * w + c);
                    } else if (field[r + 1][c] == min) {
                        next.add((r + 1) * w + c);
                    }
                }
                if (c < w - 1) {
                    if (field[r][c + 1] < min) {
                        min = field[r][c + 1];
                        next.clear();
                        next.add(r * w + c + 1);
                    } else if (field[r][c + 1] == min) {
                        next.add(r * w + c + 1);
                    }
                }
            }
            HashSet<Integer> tmp = next;
            next = current;
            current = tmp;
            next.clear();
        }
        System.out.println(ans);
    }
}
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