forked from yogykwan/acm-challenge-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoj1979.cpp
54 lines (48 loc) · 1.08 KB
/
poj1979.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* POJ 1979: Red and Black
* 题意:m*n矩阵中,给出初始位置和每个点是否允许踩的状态,向上下左右4个方向移动,求多少点能到达。
* 类型:DFS+记忆化搜索(/BFS/并查集)
* 算法:从某点出发,若该点可踩且未标记,结果+1,将该点标记(记忆化),并向其4个方向的点继续递归搜索。
*/
#include <cstdio>
#include <iostream>
using namespace std;
char mat[100][100];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, 1, -1, 0};
int m, n, ans;
void dfs(int x,int y) {
if(mat[x][y] != '.') return;
++ans;
mat[x][y] = 'x';
int xx, yy;
for(int i = 0; i < 4; ++i) {
xx = x + dx[i];
yy = y + dy[i];
if(xx >= 0 && xx < m && yy >= 0 && yy < n){
dfs(xx, yy);
}
}
}
void solve() {
ans = 0;
for(int i = 0; i < m; ++i) {
cin >> mat[i];
}
for(int i = 0; i < m; ++i) {
for(int j = 0; j< n; ++j) {
if(mat[i][j] == '@') {
mat[i][j] = '.';
dfs(i, j);
return;
}
}
}
}
int main() {
while(cin >> n >> m && m > 0 && n > 0) {
solve();
cout << ans << endl;
}
return 0;
}