题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=513
题目类型: 搜索
样例输入:
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
样例输出:
0
1
2
2
#include
#include
const int maxn=100+5;
char pic[maxn][maxn];
int m,n,idx[maxn][maxn];
void dfs(int r,int c,int id){
if(r<0||r>=m||c<0||c>=n) return;
if(idx[r][c]>0||pic[r][c]!='@') return;
idx[r][c]=id;
for(int dr=-1;dr<=1;dr++)
for(int dc=-1;dc<=1;dc++)
if(dr||dc) dfs(r+dr,c+dc,id);
}
int main(){
while(scanf("%d%d",&m,&n)==2&&m&&n){
for(int i=0;i
#include
#include
const int maxn=100+5;
char pic[maxn][maxn];
int m,n,idx[maxn][maxn];
int dx[8]={-1,1,0,0,-1,1,-1,1};
int dy[8]={0,0,-1,1,-1,1,1,-1};
void dfs(int r,int c,int id){
if(r<0||r>=m||c<0||c>=n) return;
if(idx[r][c]>0||pic[r][c]!='@') return;
idx[r][c]=id;
for(int i=0;i<8;i++) dfs(r+dx[i],c+dy[i],id);
}
int main(){
while(scanf("%d%d",&m,&n)==2&&m&&n){
for(int i=0;i
#include
#include
using namespace std;
const int maxn=100+5;
struct N{
int x,y;
};
char pic[maxn][maxn];
int m,n,mark[maxn][maxn];
const int dx[]={1,-1,0,0,1,-1,1,-1};
const int dy[]={0,0,1,-1,1,-1,-1,1};
queue Q;
void bfs(int x,int y){
N tmp;
tmp.x=x;tmp.y=y;
mark[x][y]=true;
Q.push(tmp);
while(!Q.empty()){
N now=Q.front();Q.pop();
for(int i=0;i<8;i++){
int nx=now.x+dx[i];
int ny=now.y+dy[i];
if(nx<0||nx>=m||ny<0||ny>=n) continue;
if(mark[nx][ny]==true||pic[nx][ny]=='*') continue;
N tmp;
tmp.x=nx;tmp.y=ny;
mark[nx][ny]=true;
Q.push(tmp);
}
}
}
int main(){
while(scanf("%d%d",&m,&n)==2&&m&&n){
for(int i=0;i