提出 #45514043
ソースコード 拡げる
#include <stdio.h>
#include <string.h>
#define INF 1010101010
int H, W, T;
char A[512][512];
int mc[512][512];
int qy[512 * 512], qx[512 * 512];
const int d[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
void bfs(int starty, int startx) {
int i, j;
int qs, qe;
for (i = 0; i < H; i++) {
for (j = 0; j < W; j++) mc[i][j] = INF;
}
mc[starty][startx] = 0;
qy[0] = starty;
qx[0] = startx;
qs = 0;
qe = 1;
while (qs < qe) {
int cy = qy[qs], cx = qx[qs];
qs++;
for (i = 0; i < 4; i++) {
int ny = cy + d[i][0], nx = cx + d[i][1];
if (0 <= ny && ny < H && 0 <= nx && nx < W && A[ny][nx] != '#') {
int nc = mc[cy][cx] + 1;
if (mc[ny][nx] > nc) {
mc[ny][nx] = nc;
qy[qe] = ny;
qx[qe] = nx;
qe++;
}
}
}
}
}
int sy = -1, sx = -1, gy = -1, gx = -1;
int kc = 0;
int ky[20], kx[20];
int dists[20][20];
int numBits[1 << 18];
int query;
int memo[18][1 << 20];
int calc(int pos, int visited) {
int ans = INF;
int i;
if (numBits[visited] >= query) return dists[pos][kc + 1];
if (memo[pos][visited]) return ~memo[pos][visited];
for (i = 0; i < kc; i++) {
if (!((visited >> i) & 1)) {
int candidate = calc(i, visited | (1 << i)) + dists[pos][i];
if (candidate < ans) ans = candidate;
}
}
return ~(memo[pos][visited] = ~ans);
}
int main(void) {
int i, j;
int ok, ng;
if (scanf("%d%d%d", &H, &W, &T) != 3) return 1;
for (i = 0; i < H; i++) {
if (scanf("%511s", A[i]) != 1) return 1;
for (j = 0; j < W; j++) {
if (A[i][j] == 'S') { sy = i; sx = j; }
if (A[i][j] == 'G') { gy = i; gx = j; }
if (A[i][j] == 'o') {
ky[kc] = i; kx[kc] = j; kc++;
if (kc > 18) {
puts("paiza");
return 99;
}
}
}
}
if (sy < 0 || gy < 0) {
puts("paiza");
return 99;
}
ky[kc] = sy; kx[kc] = sx;
ky[kc + 1] = gy; kx[kc + 1] = gx;
for (i = 0; i <= kc + 1; i++) {
bfs(ky[i], kx[i]);
for (j = 0; j <= kc + 1; j++) {
dists[i][j] = mc[ky[j]][kx[j]];
}
}
for (i = 0; i < (1 << kc); i++) {
int cnt = 0, cur = i;
while (cur > 0) {
cnt += cur & 1;
cur >>= 1;
}
numBits[i] = cnt;
}
ok = -1;
ng = kc + 1;
while (ok + 1 < ng) {
int mid = ok + (ng - ok) / 2;
memset(memo, 0, sizeof(memo));
query = mid;
if (calc(kc, 0) <= T) ok = mid; else ng = mid;
}
printf("%d\n", ok);
return 0;
}
/*
1. 各ポイント間の移動回数を幅優先探索で求める
2. お菓子マスを何個訪れるかを決め打ちし、ビットDP(メモ化探索)で最短移動回数を求める
3. それがT以下かで二分探索する
*/
提出情報
| 提出日時 | |
|---|---|
| 問題 | E - Pac-Takahashi |
| ユーザ | mikecat |
| 言語 | C (gcc 12.2.0) |
| 得点 | 0 |
| コード長 | 2666 Byte |
| 結果 | WA |
| 実行時間 | 303 ms |
| メモリ | 77972 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 0 / 475 | ||||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random1_00.txt, 01_random1_01.txt, 01_random1_02.txt, 01_random1_03.txt, 01_random1_04.txt, 01_random1_05.txt, 01_random1_06.txt, 01_random1_07.txt, 01_random1_08.txt, 01_random1_09.txt, 01_random1_10.txt, 01_random1_11.txt, 01_random1_12.txt, 01_random1_13.txt, 01_random1_14.txt, 01_random1_15.txt, 01_random1_16.txt, 01_random1_17.txt, 01_random1_18.txt, 01_random1_19.txt, 01_random1_20.txt, 01_random1_21.txt, 01_random1_22.txt, 01_random1_23.txt, 01_random1_24.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 04_random4_00.txt, 04_random4_01.txt, 04_random4_02.txt, 04_random4_03.txt, 04_random4_04.txt, 04_random4_05.txt, 04_random4_06.txt, 04_random4_07.txt, 04_random4_08.txt, 04_random4_09.txt, 05_handmade_00.txt, 05_handmade_01.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| 00_sample_00.txt | AC | 38 ms | 75336 KiB |
| 00_sample_01.txt | AC | 39 ms | 75376 KiB |
| 00_sample_02.txt | AC | 57 ms | 76384 KiB |
| 01_random1_00.txt | WA | 57 ms | 77240 KiB |
| 01_random1_01.txt | WA | 58 ms | 77172 KiB |
| 01_random1_02.txt | WA | 58 ms | 77240 KiB |
| 01_random1_03.txt | WA | 59 ms | 77184 KiB |
| 01_random1_04.txt | AC | 72 ms | 76596 KiB |
| 01_random1_05.txt | WA | 59 ms | 77168 KiB |
| 01_random1_06.txt | WA | 59 ms | 77272 KiB |
| 01_random1_07.txt | WA | 59 ms | 77172 KiB |
| 01_random1_08.txt | WA | 58 ms | 77244 KiB |
| 01_random1_09.txt | AC | 65 ms | 76400 KiB |
| 01_random1_10.txt | WA | 58 ms | 77072 KiB |
| 01_random1_11.txt | WA | 58 ms | 77228 KiB |
| 01_random1_12.txt | WA | 77 ms | 77372 KiB |
| 01_random1_13.txt | AC | 109 ms | 77144 KiB |
| 01_random1_14.txt | WA | 93 ms | 77664 KiB |
| 01_random1_15.txt | AC | 132 ms | 76876 KiB |
| 01_random1_16.txt | AC | 103 ms | 77104 KiB |
| 01_random1_17.txt | WA | 88 ms | 77692 KiB |
| 01_random1_18.txt | WA | 83 ms | 77884 KiB |
| 01_random1_19.txt | WA | 78 ms | 77708 KiB |
| 01_random1_20.txt | WA | 75 ms | 77784 KiB |
| 01_random1_21.txt | WA | 78 ms | 77772 KiB |
| 01_random1_22.txt | AC | 74 ms | 77912 KiB |
| 01_random1_23.txt | AC | 75 ms | 77752 KiB |
| 01_random1_24.txt | AC | 76 ms | 77972 KiB |
| 02_random2_00.txt | AC | 33 ms | 76068 KiB |
| 02_random2_01.txt | AC | 34 ms | 76220 KiB |
| 02_random2_02.txt | AC | 33 ms | 76096 KiB |
| 02_random2_03.txt | AC | 37 ms | 76712 KiB |
| 02_random2_04.txt | AC | 35 ms | 76788 KiB |
| 03_random3_00.txt | WA | 57 ms | 76500 KiB |
| 03_random3_01.txt | WA | 57 ms | 76372 KiB |
| 03_random3_02.txt | WA | 57 ms | 76496 KiB |
| 03_random3_03.txt | WA | 58 ms | 77224 KiB |
| 04_random4_00.txt | WA | 60 ms | 77144 KiB |
| 04_random4_01.txt | AC | 102 ms | 76228 KiB |
| 04_random4_02.txt | AC | 208 ms | 76468 KiB |
| 04_random4_03.txt | WA | 60 ms | 77268 KiB |
| 04_random4_04.txt | AC | 78 ms | 76516 KiB |
| 04_random4_05.txt | AC | 92 ms | 76764 KiB |
| 04_random4_06.txt | AC | 65 ms | 76508 KiB |
| 04_random4_07.txt | AC | 303 ms | 76636 KiB |
| 04_random4_08.txt | WA | 60 ms | 77244 KiB |
| 04_random4_09.txt | WA | 60 ms | 77272 KiB |
| 05_handmade_00.txt | AC | 39 ms | 75496 KiB |
| 05_handmade_01.txt | AC | 38 ms | 75480 KiB |