hdu1312
来源:互联网 发布:seo 点击域名跳转 编辑:程序博客网 时间:2024/05/20 00:36
这道题是搜索的水题,拿来做入门的题挺好的。
题目:要求从‘@’点开始,走遍‘.’,不能越过‘#’点,要求输出走‘.’的数目。
自己在做题的时候有两个代码,第一种是递归的思想,第二种是递推的思想,两种都可,但感觉后者要快那么一点点。
如图,第二种快15ms,OvO~。
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;const int maxn=25;int n,m;int x1,y1;int step;char map[maxn][maxn];int x[4]={1,0,0,-1},y[4]={0,-1,1,0};int bfs(int a,int b){ if(a<0||a>=n||b<0||b>=m) return 0; if(map[a][b]=='#') return 0; if(map[a][b]!='#') { map[a][b]='#'; return 1+bfs(a+1,b)+bfs(a,b+1)+bfs(a-1,b)+bfs(a,b-1); }}int main(){ while(~scanf("%d%d",&m,&n),m&&n) { step=0; for(int i=0;i<n;i++) { scanf("%s",map[i]); for(int j=0;j<m;j++) { if(map[i][j]=='@') { x1=i; y1=j; } } } step=bfs(x1,y1); printf("%d\n",step); } return 0;}
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;const int maxn=25;int n,m;int x1,y1;int step;char map[maxn][maxn];int x[4]={1,0,0,-1},y[4]={0,-1,1,0};void bfs(int a,int b){ if(a<0||a>=n||b<0||b>=m) return ; if(map[a][b]=='#') return ; if(map[a][b]!='#') { map[a][b]='#'; step++; for(int i=0;i<4;i++) { bfs(a+x[i],b+y[i]); } }}int main(){ while(~scanf("%d%d",&m,&n),m&&n) { step=0; for(int i=0;i<n;i++) { scanf("%s",map[i]); for(int j=0;j<m;j++) { if(map[i][j]=='@') { x1=i; y1=j; } } } bfs(x1,y1); printf("%d\n",step); } return 0;}
0 0
- hdu1312
- hdu1312
- HDU1312
- hdu1312
- hdu1312
- hdu1312
- hdu1312
- hdu1312
- hdu1312
- hdu1312
- hdu1312
- HDU1312
- HDU1312
- hdu1312
- hdu1312
- HDU1312
- hdu1312
- hdu1312深搜!!!
- KNN算法matlab代码实现
- 漫步数学分析三十三——可微的条件
- 集成学习(一)
- Spring源码
- Android 之窗口小部件详解--App Widget
- hdu1312
- Eclipse-用Eclipse搭建一个maven项目
- Java 访问权限控制:你真的了解 protected 关键字吗?
- 利用PHP产生随机验证码
- python 安装lxml 问题与办法
- Java核心技术卷一(四)
- 51nod-1422:沙拉酱前缀
- vs2010 文件多 新建类似头文件和源文件
- Json数据解析在Unity3d中的应用