結果

問題 No.13 囲みたい!
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-09-01 16:07:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 543 ms / 5,000 ms
コード長 1,280 bytes
コンパイル時間 662 ms
コンパイル使用メモリ 71,884 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-27 14:13:55
合計ジャッジ時間 1,988 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 543 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 43 ms
5,376 KB
testcase_08 AC 19 ms
5,376 KB
testcase_09 AC 20 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 16 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 8 ms
5,376 KB
testcase_14 AC 8 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <queue>
typedef long long ll;
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

int w, h;
int b[110][110];
bool d[110][110];//一度調べた部分を記録
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main(void){
	cin >> w >> h;
	rep(i, h)rep(j, w) cin >> b[i][j];
	rep(i, 110)rep(j, 110) d[i][j] = false;
	rep(i, h)rep(j, w){
		if(d[i][j]) continue;//すでに調べている
		int used[110][110];//今回の記録用
		rep(a, 110)rep(b, 110)used[a][b] = 0;
		queue<pair<int, int> > q;
		queue<int> v;
		int num = b[i][j];
		used[i][j] = 1; d[i][j] = true;
		q.push(make_pair(i, j));//スタート
		v.push(-1);
		while(!q.empty()){
			auto now = q.front(); q.pop();
			auto from = v.front(); v.pop();
			rep(k, 4){
				if(from == (k + 2) % 4 && from != -1) continue;//来た方向に戻るのを防ぐ
				int y = now.first + dy[k], x = now.second + dx[k];
				if(y < 0 || h <= y || x < 0 || w <= x) continue;
				if(used[y][x] == 1){
					printf("possible\n");
					return 0;
				}
				if(b[y][x] == num && used[y][x] == 0){
					q.push(make_pair(y, x));
					v.push(k);
					used[y][x] = 1; d[i][j] = true;
				}

			}
		}
		
	}
	printf("impossible\n");
	return 0;
}
0