poj 1979 dfs
水过。注意边界不能超出。
#include <iostream>
using namespace std;
int n, m, sx, sy, dir[4][2] = {0, -1, 0, 1, 1, 0, -1, 0}, count;
char diagram[23][23];
void get_diagram(void) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> diagram[i][j];
if (diagram[i][j] == '@') {
sx = i;
sy = j;
}
}
}
}
void dfs(int x, int y) {
count++;
diagram[x][y] = '#';
for (int i = 0; i < 4; i++) {
if (x + dir[i][0] >=0 && x + dir[i][0] < n && y + dir[i][1] >= 0 && y + dir[i][1] < m && diagram[x + dir[i][0]][y + dir[i][1]] != '#') {
dfs(x + dir[i][0], y + dir[i][1]);
}
}
return;
}
int main(void) {
while (cin >> m >> n, n*n + m*m) {
get_diagram();
count = 0;
dfs(sx, sy);
cout << count << endl;
}
return 0;
}