回溯法与dfs
/*
* POJ_1979.cpp
*
* Created on: 2013年10月24日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 25;
int n,m;
char map[maxn][maxn];//瓷砖图
int visited[maxn][maxn];//用来标记是否访问过
int ans;
//利用回溯法来扫图
void search(int row,int col){
if(row < 0 || row >= m || col < 0 || col >= n || map[row][col] == '#' || visited[row][col]){
return ;
}
visited[row][col] = true;
ans++;
search(row-1,col);
search(row+1,col);
search(row,col-1);
search(row,col+1);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF,n||m){
ans = 0;
memset(visited,false,sizeof(visited));
int i,j;
int row ,col;
for(i = 0 ; i < m ; ++i){
for(j = 0 ; j < n ; ++j){
scanf(" %c",&map[i][j]);
if(map[i][j] == '@'){
row = i;
col = j;
}
}
}
search(row,col);
printf("%d\n",ans);
}
return 0;
}