結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2014-12-11 14:11:19 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 5,000 ms |
| コード長 | 1,056 bytes |
| コンパイル時間 | 1,123 ms |
| コンパイル使用メモリ | 50,688 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-13 10:40:10 |
| 合計ジャッジ時間 | 1,340 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
コンパイルメッセージ
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]);
| ~~~~~^~~~~~~~~~~~~~~
ソースコード
#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");
}