結果

問題 No.13 囲みたい!
ユーザー bal4ubal4u
提出日時 2019-05-26 09:40:21
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,333 bytes
コンパイル時間 634 ms
コンパイル使用メモリ 30,932 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 17:51:05
合計ジャッジ時間 1,575 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:6:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    6 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:9:24: note: in expansion of macro 'gc'
    9 |         int n = 0, c = gc();
      |                        ^~
main.c: In function 'outs':
main.c:7:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
    7 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:14:33: note: in expansion of macro 'pc'
   14 | void outs(char *s) { while (*s) pc(*s++); pc('\n'); }
      |                                 ^~

ソースコード

diff #

// yukicoder: No.13 囲みたい!
// 2019.5.26 bal4u

#include <stdio.h>

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
int in() {    // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void outs(char *s) { while (*s) pc(*s++); pc('\n'); }

typedef struct { int r, c, pr, pc; } Q;
Q q[10005]; int top;
char vis[103][103];
int W, H;
int map[103][103];
int mv[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};

int dfs(int rr, int cc, int val) {
	int i, r, c, pr, pc;
	
	q[0].r = rr, q[0].c = cc, pr = -1, top = 1;
	while (top) {
		r = q[--top].r, c = q[top].c, pr = q[top].pr, pc = q[top].pc;
		if (vis[r][c]) return 1;
		vis[r][c] = 1;
		for (i = 0; i < 4; i++) {
			rr = r + mv[i][0], cc = c + mv[i][1];
			if (rr < 0 || rr >= H || cc < 0 || cc >= W) continue;
			if (rr == pr && cc == pc) continue;
			if (map[rr][cc] == val) {
				q[top].r = rr, q[top].c = cc, q[top].pr = r, q[top++].pc = c;
			}
		}
	}
	return 0;
}

int main()
{
	int r, c;

	W = in(), H = in();
	if (W == 1 || H == 1) { outs("impossible"); return 0; }
	for (r = 0; r < H; r++) for (c = 0; c < W; c++) map[r][c] = in();
	for (r = 0; r < H; r++) for (c = 0; c < W; c++) if (!vis[r][c]) {
		if (dfs(r, c, map[r][c])) { outs("possible"); return 0; }
	}
	outs("impossible");
	return 0;
}
0