結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
codershifth
|
| 提出日時 | 2015-07-20 00:38:42 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 1,981 bytes |
| コンパイル時間 | 1,839 ms |
| コンパイル使用メモリ | 163,860 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-13 10:46:00 |
| 合計ジャッジ時間 | 2,525 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()
using namespace std;
class TryToSurround {
public:
void solve(void) {
int W,H;
cin>>W>>H;
vector<vector<int>> tile(H,vector<int>(W,0));
REP(i,H)
{
REP(j,W)
cin>>tile[i][j];
}
vector<vector<int>> vis(H,vector<int>(W,false));
// 直前のマスに後戻りせずに同じ数字のマスへ移動できるなら囲える
function<bool(int,int,int,int)> dfs = [&](int x, int y, int prex, int prey) {
vis[y][x] = true;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
bool ok = false;
REP(d, 4)
{
int nx = x+dx[d];
int ny = y+dy[d];
if (prex==nx && prey==ny)
continue;
if (nx < 0 || W <= nx || ny < 0 || H <= ny)
continue;
if (tile[ny][nx]!=tile[y][x])
continue;
if (vis[ny][nx])
return true;
ok = ok || dfs(nx,ny,x,y);
}
return ok;
};
REP(i, H)
REP(j, W)
{
if (vis[i][j])
continue;
if ( dfs(j,i,-1,-1) )
{
cout<<"possible"<<endl;
return;
}
}
cout<<"impossible"<<endl;
}
};
#if 1
int main(int argc, char *argv[])
{
ios::sync_with_stdio(false);
auto obj = new TryToSurround();
obj->solve();
delete obj;
return 0;
}
#endif
codershifth