题目分析:
代码:
#include<bits/stdc++.h>
#include<iostream>
#include<windows.h>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
char arr[30][30] ;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int n,m;
int bfs(int sx,int sy){
queue<PII> q;
q.push({sx,sy});
arr[sx][sy] = '#';
int res = 0;
while(q.size()){
auto t = q.front();
q.pop();
res++;
for(int i = 0;i < 4;i++){
int x = t.x + dx[i];
int y = t.y + dy[i];
if(x<1||x>m||y<1||y>n||arr[x][y] != '.')
continue;
arr[x][y] = '#';
q.push({x,y});
}
}
return res;
}
int dfs(int sx,int sy){
arr[sx][sy] = '#';
int res = 1;
for(int i = 0;i < 4;i++){
int a = sx + dx[i];
int b = sy + dy[i];
if(a>=1)
if(a<=m)
if(b>=1)
if(b<=n)
if(arr[a][b] == '.')
res += dfs(a,b);
}
return res;
}
int main(){
int x,y;
while(cin>>n>>m,n||m){
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++){
cin>>arr[i][j];
if(arr[i][j]=='@'){
x = i;
y = j;
}
}
//cout<<bfs(x,y)<<endl;
cout<<dfs(x,y)<<endl;
}
system("pause");
return 0;
}