結果

問題 No.2064 Smallest Sequence on Grid
ユーザー ks2mks2m
提出日時 2022-09-02 22:09:19
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,669 ms / 3,000 ms
コード長 1,122 bytes
コンパイル時間 4,216 ms
コンパイル使用メモリ 86,068 KB
実行使用メモリ 107,432 KB
最終ジャッジ日時 2024-11-16 03:46:55
合計ジャッジ時間 26,235 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

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