結果

問題 No.697 池の数はいくつか
ユーザー roarisroaris
提出日時 2019-11-06 19:54:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,697 ms / 6,000 ms
コード長 1,183 bytes
コンパイル時間 1,773 ms
コンパイル使用メモリ 170,668 KB
実行使用メモリ 152,864 KB
最終ジャッジ日時 2024-04-25 20:36:24
合計ジャッジ時間 15,163 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
79,368 KB
testcase_01 AC 20 ms
79,780 KB
testcase_02 AC 20 ms
79,092 KB
testcase_03 AC 20 ms
79,312 KB
testcase_04 AC 21 ms
79,260 KB
testcase_05 AC 20 ms
79,180 KB
testcase_06 AC 21 ms
80,540 KB
testcase_07 AC 21 ms
80,364 KB
testcase_08 AC 21 ms
79,256 KB
testcase_09 AC 20 ms
79,696 KB
testcase_10 AC 21 ms
79,108 KB
testcase_11 AC 20 ms
79,840 KB
testcase_12 AC 21 ms
80,052 KB
testcase_13 AC 21 ms
79,408 KB
testcase_14 AC 20 ms
78,896 KB
testcase_15 AC 22 ms
80,052 KB
testcase_16 AC 21 ms
79,164 KB
testcase_17 AC 21 ms
79,124 KB
testcase_18 AC 20 ms
79,396 KB
testcase_19 AC 20 ms
78,984 KB
testcase_20 AC 21 ms
80,220 KB
testcase_21 AC 20 ms
80,052 KB
testcase_22 AC 21 ms
79,076 KB
testcase_23 AC 20 ms
79,752 KB
testcase_24 AC 215 ms
103,272 KB
testcase_25 AC 212 ms
104,096 KB
testcase_26 AC 214 ms
103,408 KB
testcase_27 AC 208 ms
102,980 KB
testcase_28 AC 213 ms
103,392 KB
testcase_29 AC 1,639 ms
152,092 KB
testcase_30 AC 1,697 ms
151,724 KB
testcase_31 AC 1,581 ms
152,472 KB
testcase_32 AC 1,658 ms
152,172 KB
testcase_33 AC 1,653 ms
152,864 KB
testcase_34 AC 1,690 ms
151,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int H, W;
int A[3100][3100];
int visited[3100][3100];

void bfs(int sx, int sy) {
    visited[sx][sy] = 1;
    queue<P> q;
    q.push(P(sx, sy));
    
    while (q.size()) {
        P p = q.front(); q.pop();
        int cx = p.first;
        int cy = p.second;
        
        for (int i=0; i<4; i++) {
            int nx = cx + dx[i];
            int ny = cy + dy[i];
            
            if (0<=nx && nx<H && 0<=ny && ny<W && A[nx][ny]==1 && visited[nx][ny]==0) {
                visited[nx][ny] = 1;
                q.push(P(nx, ny));
            }
        }
    }
    
    return;
}

signed main() {
    cin >> H >> W;
    
    for (int i=0; i<H; i++) {
        for (int j=0; j<W; j++) {
            cin >> A[i][j];
        }
    }
    
    int ans = 0;
    memset(visited, 0, sizeof(visited));
    
    for (int i=0; i<H; i++) {
        for (int j=0; j<W; j++) {
            if (A[i][j]==1 && visited[i][j]==0) {
                ans++;
                bfs(i, j);
            }
        }
    }
    
    cout << ans << endl;
}
0