'commit'
This commit is contained in:
52
TangDou/AcWing/Dfs/1113_bfs.cpp
Normal file
52
TangDou/AcWing/Dfs/1113_bfs.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
const int N = 30;
|
||||
struct Node {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
char g[N][N];
|
||||
int n, m;
|
||||
int dx[] = {0, 1, 0, -1};
|
||||
int dy[4] = {1, 0, -1, 0};
|
||||
bool st[N][N];
|
||||
int cnt;
|
||||
|
||||
void bfs(int x, int y) {
|
||||
queue<Node> q;
|
||||
q.push({x, y});
|
||||
|
||||
while (q.size()) {
|
||||
Node t = q.front();
|
||||
q.pop();
|
||||
cnt++;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int tx = t.x + dx[i], ty = t.y + dy[i];
|
||||
if (tx < 0 || tx >= n || ty < 0 || ty >= m) continue;
|
||||
if (st[tx][ty]) continue;
|
||||
if (g[tx][ty] != '.') continue;
|
||||
st[tx][ty] = true;
|
||||
q.push({tx, ty});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
while (cin >> m >> n, n || m) {
|
||||
memset(st, 0, sizeof st);
|
||||
cnt = 0;
|
||||
|
||||
int x, y;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
cin >> g[i][j];
|
||||
if (g[i][j] == '@') x = i, y = j;
|
||||
}
|
||||
}
|
||||
bfs(x, y);
|
||||
printf("%d\n", cnt);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user