結果

問題 No.697 池の数はいくつか
ユーザー tktk_snsn
提出日時 2021-01-12 17:54:24
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 992 ms / 6,000 ms
コード長 1,198 bytes
コンパイル時間 899 ms
コンパイル使用メモリ 77,572 KB
実行使用メモリ 39,168 KB
最終ジャッジ日時 2024-11-08 08:52:05
合計ジャッジ時間 9,377 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <utility>
using namespace std;

//frog
int A[3030][3030];
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };

int main()
{
    int h, w;
    cin >> h >> w;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            scanf("%d", &A[i][j]);
        }
    }

    int ans = 0, nx, ny;
    queue<pair<int, int>> que;
    pair<int, int> P;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (A[i][j] == 1) {
                ans++;
                A[i][j] = 0;
                que.push(make_pair(i, j));
                while (!que.empty()) {
                    P = que.front(); que.pop();
                    for (int k = 0; k < 4; k++){
                        nx = P.first + dx[k];
                        ny = P.second + dy[k];
                        if (0 <= nx && nx < h && 0 <= ny && ny < w && A[nx][ny] == 1) {
                            A[nx][ny] = 0;
                            que.push(make_pair(nx, ny));
                        }
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0