結果

問題 No.13 囲みたい!
ユーザー codershifthcodershifth
提出日時 2015-07-20 00:38:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,981 bytes
コンパイル時間 1,255 ms
コンパイル使用メモリ 163,972 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:20:36
合計ジャッジ時間 1,847 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 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 #

#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
0