結果

問題 No.13 囲みたい!
ユーザー ttkkggwwttkkggww
提出日時 2022-03-09 02:53:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,477 bytes
コンパイル時間 4,282 ms
コンパイル使用メモリ 265,388 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-09 22:43:46
合計ジャッジ時間 5,372 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,356 KB
testcase_03 AC 3 ms
4,352 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 WA -
testcase_07 AC 3 ms
4,352 KB
testcase_08 AC 3 ms
4,356 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 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;
using P = pair<int,int>;

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

void solve(){
	stack<P> Q;
	stack<P> Qp;
	bool is = false;
	vector<vector<int>> visited(h,vector<int>(w));
	for(int i = 0;i<h;i++){
		for(int j = 0;j<w;j++){
			if(visited[i][j])continue;
			int curIdx = i*w+j+1;
			visited[i][j] = curIdx;
			Q.emplace(i,j);
			Qp.emplace(-1,-1);
			while(Q.size()){
				auto [x,y] = Q.top();
				auto [px,py] = Qp.top();
				//if(px!=-1)cout<<G[x][y]<<' '<<G[px][py]<<endl;
				//cout<<x<<' '<<y<<endl;
				Q.pop();
				Qp.pop();
				for(int k = 0;k<4;k++){
					int nx = x+dx[k],ny = y+dy[k];
					if(nx<0||nx>=h||ny<0||ny>=w)continue;
					if(G[x][y]==G[nx][ny]){
						if(visited[nx][ny]==curIdx&&nx!=px&&ny!=py){
							//cout<<nx<<' '<<ny<<' '<<px<<' '<<py<<endl;
							is = true;
						}
						if(visited[nx][ny]==0){
							visited[nx][ny] = curIdx;
							Q.emplace(nx,ny);
							Qp.emplace(x,y);
						}
					}
				}
			}
		}
	}
	/*
	for(int i = 0;i<h;i++){
		for(int j = 0;j<w;j++){
			cout<<visited[i][j]<<' ';
		}
		cout<<endl;
	}
	*/
	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