题目点我
水题。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
#define maxn 25
char map[maxn][maxn];
int cnt, w, h;
void dfs(int x, int y){
if(map[y][x] == '@' || map[y][x] == '.'){
cnt++;
map[y][x] = 'v';
}
if((y - 1 >= 1) && (map[y - 1][x] == '.'))
dfs(x, y - 1);
if((y + 1 <= h) && (map[y + 1][x] == '.'))
dfs(x, y + 1);
if((x - 1 >= 1) && (map[y][x - 1] == '.'))
dfs(x - 1, y);
if((x + 1 <= w) && (map[y][x + 1] == '.'))
dfs(x + 1, y);
return;
}
int main(){
while(cin >> w >> h){
if(w == 0 && h == 0) break;
int begin_x, begin_y;
cnt = 0;
for(int i = 1; i <= h; i++){
for(int j = 1; j <= w; j++){
cin >> map[i][j];
if(map[i][j] == '@'){
begin_x = j;
begin_y = i;
}
}
}
dfs(begin_x, begin_y);
cout << cnt << endl;
}
return 0;
}