結果
| 問題 |
No.697 池の数はいくつか
|
| ユーザー |
@abcde
|
| 提出日時 | 2019-03-29 00:25:56 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2,735 ms / 6,000 ms |
| コード長 | 2,234 bytes |
| コンパイル時間 | 2,008 ms |
| コンパイル使用メモリ | 169,068 KB |
| 実行使用メモリ | 101,504 KB |
| 最終ジャッジ日時 | 2024-11-08 08:11:19 |
| 合計ジャッジ時間 | 19,988 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
// bfsの動作確認用.
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
constexpr int MAX = 9e6;
// constexpr int MAX = 64;
constexpr int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int H, W;
int board[MAX];
int memo[MAX];
// 幅優先探索.
// https://ja.wikipedia.org/wiki/幅優先探索
// 迷路を幅優先探索する.
// @param c: 探索地点の迷路の座標.
// @param l: 池の番号(※1以上).
// @param ret: n以上の素数.
void bfs(int c, int l){
// 1. 終了条件設定.
if(memo[c] >= 1) return;
if(board[c] == 0) return;
// 2. 空のキュー.
queue<int> q;
// 3. 訪問済みフラグ設定.
memo[c] = l;
// 4. 探索地点 c をキュー q に追加.
q.push(c);
while(!q.empty()){
// 5. キューから取り出す.
int v = q.front();
q.pop();
// 6. 取り出した要素を処理.
// x: 列方向, y: 行方向 で考える.
int nx, ny, n;
int cx = v % W , cy = v / W;
FOR(i, 0, 4){
nx = cx + dx[i];
ny = cy + dy[i];
n = nx + ny * W;
// cout << "cx=" << cx << " nx=" << nx << " cy=" << cy << " ny=" << ny << " n=" << n << " c=" << c << endl;
// 7. 訪問不可能なマス であれば, 処理をスキップ.
if(n < 0 || nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
if(memo[n] == l) continue;
// 8. 水のマスで, 訪問可能 かつ 未訪問 であれば, 訪問済みを設定.
if(board[n] == 1 && memo[n] == 0) memo[n] = l, q.push(n);
}
}
return;
}
int main() {
// 1. 入力情報取得.
cin >> H >> W;
FOR(i, 0, H * W) cin >> board[i];
// 2. 探索開始の頂点(根)を指定(頂点0番)し, 各頂点までの最短距離を保存.
int counter = 1;
FOR(i, 0, H * W) if(board[i] == 1) bfs(i, counter), counter++;
// 3. 出力.
// FOR(i, 0, H * W) cout << memo[i] << " ";
// cout << endl;
map<int, int> ans;
FOR(i, 0, H * W) if(memo[i] > 0) ans[memo[i]]++;
cout << ans.size() << endl;
return 0;
}
@abcde