結果
問題 | No.2064 Smallest Sequence on Grid |
ユーザー |
![]() |
提出日時 | 2022-09-05 15:05:12 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,546 ms / 3,000 ms |
コード長 | 2,467 bytes |
コンパイル時間 | 3,077 ms |
コンパイル使用メモリ | 78,596 KB |
実行使用メモリ | 92,372 KB |
最終ジャッジ日時 | 2024-11-20 15:49:35 |
合計ジャッジ時間 | 20,279 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 29 |
ソースコード
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();}}