|
常用链接
留言簿(4)
随笔分类
随笔档案
搜索
最新评论
![](/images/xml.gif)
阅读排行榜
评论排行榜
Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
类似走迷宫。。。简单的深度搜索,递归就行了
#include <iostream>
#include <string>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
char tiles[101][101];
int used[101][101];
int count,w,h;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void countstep(int i,int j)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
count++;
if (i>1&&used[i-1][j]==0) //向上
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
used[i-1][j]=1;countstep(i-1,j);
}
if (i<h&&used[i+1][j]==0) //向下
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
used[i+1][j]=1;countstep(i+1,j);
}
if (j>0&&used[i][j-1]==0) //向左
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
used[i][j-1]=1;countstep(i,j-1);
}
if (j<w-1&&used[i][j+1]==0) //向右
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
used[i][j+1]=1;countstep(i,j+1);
}
}
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int starti,startj;
while (scanf("%d %d",&w,&h)!=EOF)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (w==0||h==0)
break;
count=0;
memset(used,0,sizeof(used));
for (int i=1;i<=h;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf("%s",&tiles[i]);
}
for(int i=1;i<=h;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for (int j=0;j<w;j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (tiles[i][j]=='#')
used[i][j]=1;
else if (tiles[i][j]=='@')
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
used[i][j]=1;
startj=j;
starti=i;
}
}
}
countstep(starti,startj);
printf("%d\n",count);
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
|
|