結果

問題 No.2064 Smallest Sequence on Grid
ユーザー ks2mks2m
提出日時 2022-09-02 22:09:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,763 ms / 3,000 ms
コード長 1,122 bytes
コンパイル時間 3,616 ms
コンパイル使用メモリ 75,724 KB
実行使用メモリ 110,436 KB
最終ジャッジ日時 2023-08-10 04:12:48
合計ジャッジ時間 26,618 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
55,392 KB
testcase_01 AC 119 ms
56,092 KB
testcase_02 AC 119 ms
56,024 KB
testcase_03 AC 125 ms
55,436 KB
testcase_04 AC 127 ms
55,652 KB
testcase_05 AC 124 ms
55,652 KB
testcase_06 AC 122 ms
56,120 KB
testcase_07 AC 120 ms
55,732 KB
testcase_08 AC 132 ms
55,384 KB
testcase_09 AC 125 ms
57,656 KB
testcase_10 AC 166 ms
56,492 KB
testcase_11 AC 162 ms
56,320 KB
testcase_12 AC 151 ms
56,348 KB
testcase_13 AC 166 ms
56,604 KB
testcase_14 AC 166 ms
56,008 KB
testcase_15 AC 160 ms
56,176 KB
testcase_16 AC 171 ms
56,056 KB
testcase_17 AC 855 ms
105,996 KB
testcase_18 AC 878 ms
106,792 KB
testcase_19 AC 900 ms
107,760 KB
testcase_20 AC 1,763 ms
109,132 KB
testcase_21 AC 1,628 ms
107,988 KB
testcase_22 AC 1,634 ms
109,396 KB
testcase_23 AC 1,415 ms
110,008 KB
testcase_24 AC 1,418 ms
110,040 KB
testcase_25 AC 1,401 ms
110,436 KB
testcase_26 AC 1,569 ms
106,164 KB
testcase_27 AC 1,587 ms
106,400 KB
testcase_28 AC 883 ms
106,480 KB
testcase_29 AC 1,536 ms
98,592 KB
testcase_30 AC 806 ms
100,916 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