結果

問題 No.2064 Smallest Sequence on Grid
ユーザー ks2mks2m
提出日時 2022-09-02 22:09:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,474 ms / 3,000 ms
コード長 1,122 bytes
コンパイル時間 2,880 ms
コンパイル使用メモリ 79,792 KB
実行使用メモリ 109,752 KB
最終ジャッジ日時 2024-04-27 21:44:08
合計ジャッジ時間 23,169 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
54,184 KB
testcase_01 AC 118 ms
54,216 KB
testcase_02 AC 122 ms
54,120 KB
testcase_03 AC 119 ms
53,940 KB
testcase_04 AC 112 ms
53,956 KB
testcase_05 AC 110 ms
53,896 KB
testcase_06 AC 107 ms
54,244 KB
testcase_07 AC 114 ms
54,084 KB
testcase_08 AC 127 ms
53,892 KB
testcase_09 AC 117 ms
52,896 KB
testcase_10 AC 137 ms
53,916 KB
testcase_11 AC 140 ms
54,180 KB
testcase_12 AC 144 ms
54,160 KB
testcase_13 AC 144 ms
54,344 KB
testcase_14 AC 150 ms
54,040 KB
testcase_15 AC 142 ms
53,832 KB
testcase_16 AC 147 ms
54,400 KB
testcase_17 AC 833 ms
108,944 KB
testcase_18 AC 752 ms
108,860 KB
testcase_19 AC 813 ms
108,800 KB
testcase_20 AC 1,449 ms
109,708 KB
testcase_21 AC 1,381 ms
109,376 KB
testcase_22 AC 1,324 ms
109,352 KB
testcase_23 AC 1,283 ms
109,752 KB
testcase_24 AC 1,347 ms
109,752 KB
testcase_25 AC 1,277 ms
109,684 KB
testcase_26 AC 1,445 ms
109,488 KB
testcase_27 AC 1,474 ms
109,252 KB
testcase_28 AC 806 ms
108,692 KB
testcase_29 AC 1,463 ms
99,488 KB
testcase_30 AC 721 ms
103,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

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();
		char[][] s = new char[h][w];
		for (int i = 0; i < h; i++) {
			s[i] = sc.next().toCharArray();
		}
		sc.close();

		StringBuilder sb = new StringBuilder();
		int hw = h + w - 1;
		Set<Integer> set = new HashSet<>();
		set.add(0);
		for (int i = 0; i < hw; i++) {
			Map<Character, Set<Integer>> map = new HashMap<>();
			for (char j = 'a'; j <= 'z'; j++) {
				map.put(j, new HashSet<>());
			}
			int x0 = set.iterator().next();
			int y0 = i - x0;
			sb.append(s[x0][y0]);
			for (int x : set) {
				int y = i - x;
				if (x < h - 1) {
					map.get(s[x + 1][y]).add(x + 1);
				}
				if (y < w - 1) {
					map.get(s[x][y + 1]).add(x);
				}
			}
			for (char j = 'a'; j <= 'z'; j++) {
				Set<Integer> wk = map.get(j);
				if (!wk.isEmpty()) {
					set = wk;
					break;
				}
			}
		}
		System.out.println(sb.toString());
	}
}
0