結果

問題 No.2673 A present from B
ユーザー ks2mks2m
提出日時 2024-03-15 22:27:22
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,866 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 79,620 KB
実行使用メモリ 77,796 KB
最終ジャッジ日時 2024-03-15 22:27:34
合計ジャッジ時間 7,248 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
64,636 KB
testcase_01 AC 136 ms
57,844 KB
testcase_02 AC 136 ms
57,800 KB
testcase_03 AC 137 ms
55,944 KB
testcase_04 AC 243 ms
62,676 KB
testcase_05 AC 135 ms
57,704 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

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[i] = sc.nextInt() - 1;
		}
		sc.close();

		int inf = 100000000;
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i < n; i++) {
			int[][] d = new int[m + 1][n];
			for (int j = 0; j < d.length; j++) {
				Arrays.fill(d[j], inf);
			}
			d[0][i] = 0;
			Deque<Obj> que = new ArrayDeque<>();
			que.add(new Obj(0, i, 0));
			while (!que.isEmpty()) {
				Obj o = que.poll();
				if (o.x == m && o.y == 0) {
					sb.append(o.d).append(' ');
					break;
				}
				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 {
						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));
				}
			}
//			for (int j = 0; j < d.length; j++) {
//				System.out.println(Arrays.toString(d[i]));
//			}
//			System.out.println();
		}
		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