結果

問題 No.2673 A present from B
ユーザー ks2mks2m
提出日時 2024-03-15 22:28:50
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,911 bytes
コンパイル時間 3,345 ms
コンパイル使用メモリ 78,968 KB
実行使用メモリ 71,788 KB
最終ジャッジ日時 2024-03-15 22:29:14
合計ジャッジ時間 7,214 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
57,972 KB
testcase_01 AC 142 ms
57,600 KB
testcase_02 AC 140 ms
57,724 KB
testcase_03 AC 139 ms
57,816 KB
testcase_04 AC 236 ms
62,464 KB
testcase_05 AC 140 ms
57,856 KB
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 AC 198 ms
57,952 KB
testcase_10 AC 187 ms
58,216 KB
testcase_11 AC 161 ms
56,012 KB
testcase_12 AC 804 ms
63,880 KB
testcase_13 AC 618 ms
63,924 KB
testcase_14 AC 1,192 ms
70,320 KB
testcase_15 AC 532 ms
63,740 KB
testcase_16 AC 472 ms
63,624 KB
testcase_17 AC 340 ms
63,592 KB
testcase_18 AC 579 ms
63,716 KB
testcase_19 AC 277 ms
63,168 KB
testcase_20 AC 1,039 ms
64,036 KB
testcase_21 AC 1,919 ms
71,780 KB
testcase_22 AC 144 ms
57,944 KB
testcase_23 AC 174 ms
57,840 KB
testcase_24 AC 142 ms
57,908 KB
testcase_25 AC 139 ms
57,836 KB
testcase_26 AC 139 ms
57,952 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[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 {
						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));
				}
			}
//			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