Submission #74420710
Source Code Expand
Copy
#include <stdio.h>int H, W, N;char kukaku[1024][1024];int pos[10][2];int mincost[1024][1024];int gen[1024][1024];int q[1024 * 1024][2];int main(void) {int i, j;int ans = 0;if (scanf("%d%d%d", &H, &W, &N) != 3) return 1;for (i = 0; i < H; i++) {if (scanf("%1023s", kukaku[i]) != 1) return 1;for (j = 0; j < W; j++) {if (kukaku[i][j] == 'S') {pos[0][0] = i;pos[0][1] = j;} else if ('1' <= kukaku[i][j] && kukaku[i][j] <= '9') {int idx = kukaku[i][j] - '0';pos[idx][0] = i;pos[idx][1] = j;}}}for (i = 1; i <= N; i++) {int qs = 0, qe = 1;q[0][0] = pos[i - 1][0];q[0][1] = pos[i - 1][1];mincost[q[0][0]][q[0][1]] = 0;gen[q[0][0]][q[0][1]] = i;while (qs < qe) {int cy = q[qs][0];int cx = q[qs][1];qs++;for (j = 0; j < 4; j++) {int d = (j & 1) * 2 - 1;
#include <stdio.h>
int H, W, N;
char kukaku[1024][1024];
int pos[10][2];
int mincost[1024][1024];
int gen[1024][1024];
int q[1024 * 1024][2];
int main(void) {
int i, j;
int ans = 0;
if (scanf("%d%d%d", &H, &W, &N) != 3) return 1;
for (i = 0; i < H; i++) {
if (scanf("%1023s", kukaku[i]) != 1) return 1;
for (j = 0; j < W; j++) {
if (kukaku[i][j] == 'S') {
pos[0][0] = i;
pos[0][1] = j;
} else if ('1' <= kukaku[i][j] && kukaku[i][j] <= '9') {
int idx = kukaku[i][j] - '0';
pos[idx][0] = i;
pos[idx][1] = j;
}
}
}
for (i = 1; i <= N; i++) {
int qs = 0, qe = 1;
q[0][0] = pos[i - 1][0];
q[0][1] = pos[i - 1][1];
mincost[q[0][0]][q[0][1]] = 0;
gen[q[0][0]][q[0][1]] = i;
while (qs < qe) {
int cy = q[qs][0];
int cx = q[qs][1];
qs++;
for (j = 0; j < 4; j++) {
int d = (j & 1) * 2 - 1;
int ny = cy + (j < 2 ? d : 0);
int nx = cx + (j < 2 ? 0 : d);
if (0 <= ny && ny < H && 0 <= nx && nx < W && kukaku[ny][nx] != 'X') {
if (gen[ny][nx] != i || mincost[cy][cx] + 1 < mincost[ny][nx]) {
gen[ny][nx] = i;
mincost[ny][nx] = mincost[cy][cx] + 1;
q[qe][0] = ny;
q[qe][1] = nx;
qe++;
}
}
}
}
ans += mincost[pos[i][0]][pos[i][1]];
}
printf("%d\n", ans);
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - チーズ (Cheese) |
| User | mikecat |
| Language | C23 (GCC 14.2.0) |
| Score | 100 |
| Code Size | 1351 Byte |
| Status | AC |
| Exec Time | 50 ms |
| Memory | 13344 KiB |
Judge Result
| Set Name | set01 | set02 | set03 | set04 | set05 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 20 / 20 | 20 / 20 | 20 / 20 | 20 / 20 | 20 / 20 | ||||||||||
| Status |
|
|
|
|
|
| Set Name | Test Cases |
|---|---|
| set01 | data1 |
| set02 | data2 |
| set03 | data3 |
| set04 | data4 |
| set05 | data5 |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| data1 | AC | 1 ms | 1824 KiB |
| data2 | AC | 1 ms | 1944 KiB |
| data3 | AC | 2 ms | 2420 KiB |
| data4 | AC | 50 ms | 13344 KiB |
| data5 | AC | 27 ms | 11504 KiB |