結果

問題 No.13 囲みたい!
コンテスト
ユーザー hogeover30
提出日時 2015-02-24 20:07:48
言語 C++11(old_compat)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -include bits/stdc++.h -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 921 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,083 ms
コンパイル使用メモリ 171,580 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-03-08 16:01:44
合計ジャッジ時間 1,777 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int dr[]={-1, 0, 1, 0}, dc[]={0, -1, 0, 1};
int visited[110][110];

bool dfs(const vector<vector<int>>& a, int r, int c, int pr, int pc)
{
    if (visited[r][c]) return true;
    visited[r][c]++;
    for(int i=0;i<4;++i) {
        int nr=r+dr[i], nc=c+dc[i];
        if ((nr==pr and nc==pc) or a[nr][nc]!=a[r][c]) continue;
        if (dfs(a, nr, nc, r, c)) return true;
    }
    return false;
}

int main()
{
    int w, h;
    while (cin>>w>>h) {
        vector<vector<int>> a(h+2, vector<int>(w+2, -1));
        for(int i=0;i<h;++i) for(int j=0;j<w;++j) cin>>a[i+1][j+1];

        bool possible=false;
        memset(visited, 0, sizeof(visited));
        for(int i=1;i<=h;++i) for(int j=1;j<=w;++j) if (!visited[i][j])
            if (possible=dfs(a, i, j, -1, -1)) goto END;
END:
        cout<<("impossible"+2*possible)<<endl;
    }
}
0