Submission #57896546


Source Code Expand

Copy
#include <stdio.h>
#include <stdlib.h>
#define MOD_BY 49130563
#define FACTOR 48195181LL
#define FACTOR2 (FACTOR * FACTOR % MOD_BY)
struct yx_t {
int y, x;
};
struct hashtable_data {
int key_y, key_x, key_d;
struct yx_t value;
struct hashtable_data* next;
};
struct hashtable_data* hashtable[MOD_BY];
struct yx_t* get_hashtable(int y, int x, int d) {
int hash = (int)((y * FACTOR2 + x * FACTOR + d) % MOD_BY);
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <stdio.h>
#include <stdlib.h>

#define MOD_BY 49130563
#define FACTOR 48195181LL
#define FACTOR2 (FACTOR * FACTOR % MOD_BY)

struct yx_t {
	int y, x;
};

struct hashtable_data {
	int key_y, key_x, key_d;
	struct yx_t value;
	struct hashtable_data* next;
};

struct hashtable_data* hashtable[MOD_BY];

struct yx_t* get_hashtable(int y, int x, int d) {
	int hash = (int)((y * FACTOR2 + x * FACTOR + d) % MOD_BY);
	struct hashtable_data** p = &hashtable[hash];
	while (*p != NULL) {
		if ((*p)->key_y == y && (*p)->key_x == x && (*p)->key_d == d) return &(*p)->value;
		p = &(*p)->next;
	}
	*p = malloc(sizeof(**p));
	if (*p == NULL) exit(2);
	**p = (struct hashtable_data){ y, x, d, {-1, -1}, NULL };
	return &(*p)->value;
}

const int dyx[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; /* {dy, dx} */

int H, W;

struct yx_t get_next_kabe(int y, int x, int d) {
	if (y < 1 || H < y || x < 1 || W < x) {
		/* 盤外 */
		return (struct yx_t){ y, x };
	} else {
		struct yx_t* hd = get_hashtable(y, x, d);
		if (hd->y < 0 && hd->x < 0) {
			/* 壁がある */
			return (struct yx_t){ y, x };
		} else {
			/* 壁が無い → 探索を続行し、縮約 */
			struct yx_t res = get_next_kabe(y + dyx[d][0], x + dyx[d][1], d);
			*hd = res;
			return res;
		}
	}
}

int main(void) {
	int Q;
	int i, j;
	int ans;
	if (scanf("%d%d%d", &H, &W, &Q) != 3) return 1;
	ans = H * W;
	for (i = 0; i < Q; i++) {
		int R, C;
		struct yx_t* hd;
		if (scanf("%d%d", &R, &C) != 2) return 1;
		hd = get_hashtable(R, C, 0);
		if (hd->y < 0 && hd->x < 0) {
			/* 壁がある (破壊されていない) */
			ans--;
			for (j = 0; j < 4; j++) {
				*get_hashtable(R, C, j) = get_next_kabe(R + dyx[j][0], C + dyx[j][1], j);
			}
		} else {
			/* 壁が無い (破壊されている) */
			for (j = 0; j < 4; j++) {
				struct yx_t pos = get_next_kabe(R, C, j);
				if (1 <= pos.y && pos.y <= H && 1 <= pos.x && pos.x <= W) {
					/* 見た先に壁がある */
					int k;
					ans--;
					for (k = 0; k < 4; k++) {
						*get_hashtable(pos.y, pos.x, k) = get_next_kabe(pos.y + dyx[k][0], pos.x + dyx[k][1], k);
					}
				}
			}
		}
	}
	printf("%d\n", ans);
}

/*

各マスについて、それぞれの方向に見て最初に現れる壁の位置をハッシュテーブル (連想配列) で管理
Union-Find的に縮約する

*/

Submission Info

Submission Time
Task D - Cross Explosion
User mikecat
Language C (gcc 12.2.0)
Score 0
Code Size 2416 Byte
Status TLE
Exec Time 4460 ms
Memory 461324 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 15
TLE × 11
Set Name Test Cases
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_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_all_break_00.txt, 03_hack_00.txt, 03_hack_01.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 1664 KB
00_sample_01.txt AC 1 ms 1796 KB
00_sample_02.txt AC 0 ms 1672 KB
01_random_00.txt TLE 4453 ms 446472 KB
01_random_01.txt AC 416 ms 433036 KB
01_random_02.txt AC 473 ms 442768 KB
01_random_03.txt AC 911 ms 458632 KB
01_random_04.txt AC 3765 ms 459208 KB
01_random_05.txt TLE 4460 ms 461244 KB
01_random_06.txt TLE 4457 ms 461324 KB
01_random_07.txt AC 485 ms 447992 KB
01_random_08.txt AC 481 ms 429796 KB
01_random_09.txt TLE 4457 ms 459940 KB
01_random_10.txt AC 511 ms 449256 KB
01_random_11.txt AC 692 ms 457296 KB
01_random_12.txt TLE 4457 ms 455420 KB
01_random_13.txt TLE 4457 ms 460724 KB
01_random_14.txt AC 644 ms 454896 KB
01_random_15.txt TLE 4454 ms 460392 KB
01_random_16.txt AC 789 ms 457940 KB
01_random_17.txt AC 1195 ms 458968 KB
01_random_18.txt TLE 4456 ms 460200 KB
01_random_19.txt AC 603 ms 454132 KB
02_all_break_00.txt TLE 4459 ms 431416 KB
03_hack_00.txt TLE 4418 ms 56192 KB
03_hack_01.txt TLE 4418 ms 50676 KB


2024-09-19 (Thu)
08:32:27 +09:00