結果

問題 No.697 池の数はいくつか
ユーザー kyuna
提出日時 2019-07-19 16:32:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 850 bytes
コンパイル時間 750 ms
コンパイル使用メモリ 76,776 KB
実行使用メモリ 814,720 KB
最終ジャッジ日時 2024-11-25 15:02:27
合計ジャッジ時間 21,459 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 TLE * 1 MLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int ALIVE = 1, DEAD = 2;
int h, w;
vector<vector<int>> field;
bool out_field(int i, int j) { return i < 0 || i >= h || j < 0 || j >= w; }

void dfs(int i, int j) {
    field[i][j] = DEAD;
    for (int di: {-1, 0, 1}) for (int dj: {-1, 0, 1}) {
        if (di * di + dj * dj != 1) continue;
        int ni = i + di, nj = j + dj;
        if (out_field(ni, nj) || field[ni][nj] != ALIVE) continue;
        dfs(ni, nj);
    }
}

int main() {
    cin >> h >> w;
    field.assign(h, vector<int>(w));
    for (auto &row: field) for (auto &c: row) cin >> c;
    int cnt = 0;
    for (int si = 0; si < h; si++) for (int sj = 0; sj < w; sj++) {
        if (field[si][sj] != ALIVE) continue;
        cnt++;
        dfs(si, sj);
    }
    cout << cnt << endl;
    return 0;
}
0