結果

問題 No.13 囲みたい!
コンテスト
ユーザー bal4u
提出日時 2019-05-26 09:40:21
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 1,333 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 223 ms
コンパイル使用メモリ 40,556 KB
最終ジャッジ日時 2026-02-22 03:28:48
ジャッジサーバーID
(参考情報)
judge5 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// 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