結果

問題 No.13 囲みたい!
ユーザー ttkkggwwttkkggww
提出日時 2022-03-09 03:24:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,073 bytes
コンパイル時間 4,658 ms
コンパイル使用メモリ 264,196 KB
実行使用メモリ 4,392 KB
最終ジャッジ日時 2023-10-09 23:28:05
合計ジャッジ時間 5,101 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
int h,w;
vector<vector<int>> G;
vector<vector<int>> visited;
using P = pair<int,int>;
bool is = false;

int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};

void dfs(int x,int y,int px,int py,int color){
	visited[x][y] = color;
	for(int i = 0;i<4;i++){
		int nx = x+dx[i],ny = y+dy[i];
		if(nx<0||nx>=h||ny<0||ny>=w)continue;
		if(nx==px&&ny==py)continue;
		if(G[x][y]==G[nx][ny]){
			if(visited[nx][ny]==0){
				dfs(nx,ny,x,y,color);
			}else{
				is = true;
			}
		}
	}
}

void solve(){
	stack<P> Q;
	stack<P> Qp;
	is = false;
	visited = vector<vector<int>>(h,vector<int>(w));
	for(int i = 0;i<h;i++){
		for(int j = 0;j<w;j++){
			if(visited[i][j]==0){
				dfs(i,j,-1,-1,-1);
			}
		}
	}

	if(is)cout<<"possible"<<endl;
	else cout<<"impossible"<<endl;
}

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> w >> h;
	G = vector<vector<int>>(h,vector<int>(w));
	for(int i = 0;i<h;i++){
		for(int j = 0;j<w;j++){
			cin >> G[i][j];
		}
	}
	solve();
}
0