Submission #70669896


Source Code Expand

Copy
#include <stdio.h>
#include <string.h>
int H, W;
char S[16][16];
int gomi_cnt[16][16];
/* [offset_y][offset_x][alive_y][dead_y][alive_x][dead_x] */
int mincost[32][32][16][16][16][16];
struct status_s {
int offset_y, offset_x, alive_y, dead_y, alive_x, dead_x;
};
int* status_to_mc(const struct status_s* s) {
return &mincost[s->offset_y + 16][s->offset_x + 16][s->alive_y][s->dead_y][s->alive_x][s->dead_x];
}
int sy = -1, sx = -1;
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <stdio.h>
#include <string.h>

int H, W;
char S[16][16];

int gomi_cnt[16][16];

/* [offset_y][offset_x][alive_y][dead_y][alive_x][dead_x] */
int mincost[32][32][16][16][16][16];

struct status_s {
	int offset_y, offset_x, alive_y, dead_y, alive_x, dead_x;
};

int* status_to_mc(const struct status_s* s) {
	return &mincost[s->offset_y + 16][s->offset_x + 16][s->alive_y][s->dead_y][s->alive_x][s->dead_x];
}

int sy = -1, sx = -1;

/* 指定の方向にゴミを移動した後の状態を求める */
struct status_s next_status(const struct status_s* s, int dy, int dx) {
	struct status_s res = *s;
	res.offset_y += dy;
	res.offset_x += dx;
	if (res.alive_y < -res.offset_y) res.alive_y = -res.offset_y;
	if (H - res.offset_y < res.dead_y) res.dead_y = H - res.offset_y;
	if (res.alive_x < -res.offset_x) res.alive_x = -res.offset_x;
	if (W - res.offset_x < res.dead_x) res.dead_x = W - res.offset_x;
	return res;
}

/* 指定の状態で高橋君が汚れていないかを求める */
int is_ok(const struct status_s* s) {
	int ty = sy - s->offset_y, tx = sx - s->offset_x;
	if (ty < s->alive_y || s->dead_y <= ty || tx < s->alive_x || s->dead_x <= tx) return 1;
	return S[ty][tx] != '#';
}

/* 指定の状態ですべてのゴミが消滅したかを求める */
int is_goal(const struct status_s* s) {
	if (s->alive_y >= s->dead_y || s->alive_x >= s->dead_x) return 1;
	return (
		gomi_cnt[s->dead_y][s->dead_x] -
		gomi_cnt[s->dead_y][s->alive_x] -
		gomi_cnt[s->alive_y][s->dead_x] +
		gomi_cnt[s->alive_y][s->alive_x]
	) == 0;
}

struct status_s q[sizeof(mincost) / sizeof(int)];

int main(void) {
	int i, j;
	int qs = 0, qe = 1;
	if (scanf("%d%d", &H, &W) != 2) return 1;
	for (i = 0; i < H; i++) {
		if (scanf("%15s", S[i]) != 1) return 1;
		for (j = 0; j < W; j++) {
			if (S[i][j] == '#') gomi_cnt[i + 1][j + 1] = 1;
			if (S[i][j] == 'T') {
				sy = i;
				sx = j;
			}
		}
	}
	if (sy < 0) {
		puts("takahasi-kun not found!");
		return 42;
	}
	for (i = 1; i <= H; i++) {
		for (j = 1; j <= W; j++) gomi_cnt[i][j] += gomi_cnt[i][j - 1];
		for (j = 1; j <= W; j++) gomi_cnt[i][j] += gomi_cnt[i - 1][j];
	}
	memset(mincost, 0x7f, sizeof(mincost));
	q[0] = (struct status_s){ 0, 0, 0, H, 0, W };
	*status_to_mc(&q[0]) = 0;
	while (qs < qe) {
		struct status_s cur = q[qs++];
		int curcost = *status_to_mc(&cur);
		if (is_goal(&cur)) {
			printf("%d\n", curcost);
			return 0;
		}
		for (i = 0; i < 4; i++) {
			int dy = i < 2 ? (i & 1) * 2 - 1 : 0;
			int dx = i < 2 ? 0 : (i & 1) * 2 - 1;
			struct status_s next = next_status(&cur, dy, dx);
			if (is_ok(&next)) {
				int *nextcost = status_to_mc(&next);
				if (*nextcost > curcost + 1) {
					*nextcost = curcost + 1;
					q[qe++] = next;
				}
			}
		}
	}
	puts("-1");
	return 0;
}

Submission Info

Submission Time
Task E - Wind Cleaning
User mikecat
Language C (gcc 12.2.0)
Score 500
Code Size 2859 Byte
Status AC
Exec Time 118 ms
Memory 264044 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 43
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt
Case Name Status Exec Time Memory
sample00.txt AC 112 ms 263856 KiB
sample01.txt AC 112 ms 263640 KiB
sample02.txt AC 111 ms 263796 KiB
testcase00.txt AC 114 ms 263800 KiB
testcase01.txt AC 113 ms 263768 KiB
testcase02.txt AC 112 ms 263856 KiB
testcase03.txt AC 112 ms 263728 KiB
testcase04.txt AC 111 ms 263772 KiB
testcase05.txt AC 110 ms 263732 KiB
testcase06.txt AC 111 ms 263764 KiB
testcase07.txt AC 111 ms 263688 KiB
testcase08.txt AC 113 ms 263860 KiB
testcase09.txt AC 113 ms 263904 KiB
testcase10.txt AC 112 ms 263844 KiB
testcase11.txt AC 112 ms 263872 KiB
testcase12.txt AC 110 ms 263804 KiB
testcase13.txt AC 110 ms 263708 KiB
testcase14.txt AC 113 ms 263868 KiB
testcase15.txt AC 113 ms 263780 KiB
testcase16.txt AC 112 ms 263768 KiB
testcase17.txt AC 113 ms 263900 KiB
testcase18.txt AC 113 ms 263836 KiB
testcase19.txt AC 114 ms 263852 KiB
testcase20.txt AC 112 ms 263804 KiB
testcase21.txt AC 112 ms 263836 KiB
testcase22.txt AC 113 ms 263748 KiB
testcase23.txt AC 112 ms 264044 KiB
testcase24.txt AC 112 ms 263664 KiB
testcase25.txt AC 112 ms 263828 KiB
testcase26.txt AC 118 ms 263944 KiB
testcase27.txt AC 113 ms 263872 KiB
testcase28.txt AC 117 ms 263876 KiB
testcase29.txt AC 116 ms 263940 KiB
testcase30.txt AC 114 ms 263840 KiB
testcase31.txt AC 113 ms 263888 KiB
testcase32.txt AC 113 ms 263884 KiB
testcase33.txt AC 112 ms 263800 KiB
testcase34.txt AC 112 ms 263688 KiB
testcase35.txt AC 113 ms 263704 KiB
testcase36.txt AC 112 ms 263820 KiB
testcase37.txt AC 113 ms 263640 KiB
testcase38.txt AC 112 ms 263872 KiB
testcase39.txt AC 114 ms 263832 KiB


2025-11-03 (Mon)
23:40:41 +09:00