|
常用链接
留言簿(4)
随笔分类
随笔档案
搜索
最新评论

阅读排行榜
评论排行榜
Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
类似走迷宫。。。简单的深度搜索,递归就行了
#include <iostream>
#include <string>
using namespace std;

char tiles[101][101];
int used[101][101];
int count,w,h;

void countstep(int i,int j)
  {
count++;
if (i>1&&used[i-1][j]==0) //向上
 {
used[i-1][j]=1;countstep(i-1,j);
}
if (i<h&&used[i+1][j]==0) //向下
 {
used[i+1][j]=1;countstep(i+1,j);
}
if (j>0&&used[i][j-1]==0) //向左
 {
used[i][j-1]=1;countstep(i,j-1);
}
if (j<w-1&&used[i][j+1]==0) //向右
 {
used[i][j+1]=1;countstep(i,j+1);
}
}
int main()
  {
int starti,startj;
while (scanf("%d %d",&w,&h)!=EOF)
 {
if (w==0||h==0)
break;
count=0;
memset(used,0,sizeof(used));
for (int i=1;i<=h;i++)
 {
scanf("%s",&tiles[i]);
}
for(int i=1;i<=h;i++)
 {
for (int j=0;j<w;j++)
 {
if (tiles[i][j]=='#')
used[i][j]=1;
else if (tiles[i][j]=='@')
 {
used[i][j]=1;
startj=j;
starti=i;
}
}
}
countstep(starti,startj);
printf("%d\n",count);
}
}


|
|