結果

問題 No.2673 A present from B
ユーザー ks2mks2m
提出日時 2024-03-15 22:43:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 260 ms / 2,000 ms
コード長 1,758 bytes
コンパイル時間 2,579 ms
コンパイル使用メモリ 79,228 KB
実行使用メモリ 61,972 KB
最終ジャッジ日時 2024-03-15 22:43:18
合計ジャッジ時間 8,485 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
57,684 KB
testcase_01 AC 137 ms
57,700 KB
testcase_02 AC 137 ms
57,832 KB
testcase_03 AC 139 ms
57,836 KB
testcase_04 AC 142 ms
57,972 KB
testcase_05 AC 137 ms
57,964 KB
testcase_06 AC 260 ms
61,676 KB
testcase_07 AC 249 ms
61,560 KB
testcase_08 AC 241 ms
61,608 KB
testcase_09 AC 160 ms
57,836 KB
testcase_10 AC 183 ms
58,192 KB
testcase_11 AC 163 ms
58,220 KB
testcase_12 AC 208 ms
59,972 KB
testcase_13 AC 228 ms
60,692 KB
testcase_14 AC 227 ms
61,972 KB
testcase_15 AC 171 ms
60,268 KB
testcase_16 AC 169 ms
59,988 KB
testcase_17 AC 179 ms
58,220 KB
testcase_18 AC 180 ms
60,260 KB
testcase_19 AC 145 ms
57,808 KB
testcase_20 AC 197 ms
60,112 KB
testcase_21 AC 249 ms
61,764 KB
testcase_22 AC 139 ms
57,940 KB
testcase_23 AC 139 ms
57,948 KB
testcase_24 AC 152 ms
57,828 KB
testcase_25 AC 138 ms
57,808 KB
testcase_26 AC 142 ms
57,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[] a = new int[m];
		for (int i = 0; i < m; i++) {
			a[m - 1 - i] = sc.nextInt() - 1;
		}
		sc.close();

		int inf = 100000000;
		int[][] d = new int[m + 1][n];
		for (int j = 0; j < d.length; j++) {
			Arrays.fill(d[j], inf);
		}
		d[0][0] = 0;
		Deque<Obj> que = new ArrayDeque<>();
		que.add(new Obj(0, 0, 0));
		while (!que.isEmpty()) {
			Obj o = que.poll();
			if (o.d != d[o.x][o.y]) {
				continue;
			}
			if (o.x < m) {
				if (a[o.x] == o.y) {
					if (o.d < d[o.x + 1][o.y + 1]) {
						d[o.x + 1][o.y + 1] = o.d;
						que.addFirst(new Obj(o.x + 1, o.y + 1, o.d));
					}
				} else if (a[o.x] == o.y - 1) {
					if (o.d < d[o.x + 1][o.y - 1]) {
						d[o.x + 1][o.y - 1] = o.d;
						que.addFirst(new Obj(o.x + 1, o.y - 1, o.d));
					}
				} else {
					if (o.d < d[o.x + 1][o.y]) {
						d[o.x + 1][o.y] = o.d;
						que.addFirst(new Obj(o.x + 1, o.y, o.d));
					}
				}
			}
			if (o.y < n - 1 && o.d + 1 < d[o.x][o.y + 1]) {
				d[o.x][o.y + 1] = o.d + 1;
				que.addLast(new Obj(o.x, o.y + 1, o.d + 1));
			}
			if (o.y > 0 && o.d + 1 < d[o.x][o.y - 1]) {
				d[o.x][o.y - 1] = o.d + 1;
				que.addLast(new Obj(o.x, o.y - 1, o.d + 1));
			}
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i < n; i++) {
			sb.append(d[m][i]).append(' ');
		}
		sb.deleteCharAt(sb.length() - 1);
		System.out.println(sb.toString());
	}

	static class Obj {
		int x, y, d;

		public Obj(int x, int y, int d) {
			this.x = x;
			this.y = y;
			this.d = d;
		}
	}
}
0