結果

問題 No.13 囲みたい!
ユーザー IL_mstaIL_msta
提出日時 2015-07-11 13:51:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,128 bytes
コンパイル時間 934 ms
コンパイル使用メモリ 97,472 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:19:05
合計ジャッジ時間 1,658 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 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 4 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>

#include <algorithm>
#include <cmath>

#include <string>
#include <array>
#include <list>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>

/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;

#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////

int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    cout << setprecision(10);//

	int W,H;
	cin>>W>>H;
	int field[100][100];
	bool usefield[100][100];
	rep(j,H)rep(i,W){
		cin>>field[i][j];
		usefield[i][j] = false;
	}

	queue< pair<int,pair<int,int> > > que;
	bool useNum[1001];
	rep(i,1001)useNum[i] = false;
	int dx[4]={1,0,-1,0};
	int dy[4]={0,1,0,-1};

for(int wi=0;wi<W;++wi){
for(int hi=0;hi<H;++hi){
if( usefield[wi][hi] == false){
	{
		usefield[wi][hi] = true;
		int terNum = field[wi][hi];
		set< pair<int,int> > hit;
		hit.insert( pair<int,int>(wi,hi) );
		for(int d=0;d<4;++d){
			if(0 <= wi+dx[d] && wi+dx[d] < W && 0 <= hi+dy[d] && hi+dy[d] < H){
				if( terNum == field[ wi+dx[d] ][ hi+dy[d] ] ){
					usefield[ wi+dx[d] ][ hi+dy[d] ] = true;
					hit.insert( pair<int,int>(wi+dx[d],hi+dy[d]) );
					que.push( pair<int,pair<int,int> >(d,pair<int,int>(wi+dx[d],hi+dy[d]) ) );
				}
			}
			
		}
		////////////////
		pair<int,pair<int,int> > tque;
		int tx,ty;
		while( !que.empty() ){
			tque = que.front();
			tx = tque.second.first;
			ty = tque.second.second;
			que.pop();
			for(int d=0;d<4;++d){
				if( (tque.first+2)%4 != d &&
					0 <= tx+dx[d] && tx+dx[d] < W && 0 <= ty+dy[d] && ty+dy[d] < H){
					if( terNum == field[ tx+dx[d] ][ ty+dy[d] ] ){
						usefield[ tx+dx[d] ][ ty+dy[d] ] = true;
						if(hit.insert( pair<int,int>(tx+dx[d],ty+dy[d]) ).second == false ){
							P("possible");
							return 0;
						}
						que.push( pair<int,pair<int,int> >(d,pair<int,int>(tx+dx[d],ty+dy[d]) ) );
					}
				}
			}
		}
	}
}}}
	P("impossible");
    return 0;
}
0