結果

問題 No.13 囲みたい!
ユーザー cielciel
提出日時 2014-12-11 14:11:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,056 bytes
コンパイル時間 717 ms
コンパイル使用メモリ 50,560 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:15:49
合計ジャッジ時間 1,841 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 4 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 3 ms
5,376 KB
testcase_12 AC 1 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
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:33:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   33 |         scanf("%d%d",&w,&h);
      |         ~~~~~^~~~~~~~~~~~~~
main.cpp:37:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   37 |                 for(int x=0;x<w;x++)scanf("%d",&m[y][x]);
      |                                     ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

#include <vector>
#include <set>
#include <cstdio>
using namespace std;
typedef pair<int,int> pii;
typedef struct{
	int x;
	int y;
}dir;
dir dirs[]={
	{1,0},
	{0,-1},
	{-1,0},
	{0,1},
};
bool dfs(pii &cur,pii &prev,set<pii>&se,const vector<vector<int>>&m){
	for(auto &d:dirs){
		pii nxt={cur.first+d.x,cur.second+d.y};
		if(
			nxt==prev ||
			nxt.first<0||m[0].size()<=nxt.first ||
			nxt.second<0||m.size()<=nxt.second ||
			m[cur.second][cur.first]!=m[nxt.second][nxt.first]
		)continue;
		if(se.find(nxt)!=se.end())return true;
		se.insert(nxt);
		if(dfs(nxt,cur,se,m))return true;
	}
	return false;
}
int main(){
	int w,h;
	scanf("%d%d",&w,&h);
	vector<vector<int>>m(h);
	for(int y=0;y<h;y++){
		m[y].resize(w);
		for(int x=0;x<w;x++)scanf("%d",&m[y][x]);
	}
	for(int y=0;y<h;y++)for(int x=0;x<w;x++)if(m[y][x]){
		set<pii>se;
		pii cur={x,y};
		se.insert(cur);
		if(dfs(cur,cur,se,m)){
			puts("possible");
			return 0;
		}
		for(auto &e:se){
			m[e.second][e.first]=0;
		}
	}
	puts("impossible");
}
0