poj1979【基础bfs/dfs】
来源:互联网 发布:淘宝订单贷款能用多久 编辑:程序博客网 时间:2024/06/05 22:40
挑战习题搜索-1
题意:
给定起点,然后求一个可以到达的数量,位置“.”都可以走。每次应该是上下左右都可以走。
思路:
这题应该DFS更好写,但是BFS也可以写吧。
好久没写了…
dfs挫代码……..
#include<cstdio>#include<iostream>#include<math.h>#include<string.h>#include<algorithm>using namespace std;#define eps 1e-8typedef __int64 LL;const int N=25;int n,m;char ma[N][N];int dx[4]={0,0,-1,1};int dy[4]={1,-1,0,0};int ans;bool Judge(int x,int y){ if(x<0||y<0||x>=n||y>=m||ma[x][y]=='#') return 0; return 1;}void dfs(int x,int y){ ma[x][y]='#';//到了就标记,避免重复; ans++; //计数++; for(int i=0;i<4;i++) { int xx=x+dx[i]; int yy=y+dy[i]; if(Judge(xx,yy))//如果符合就继续搜索; dfs(xx,yy); }}int main(){ while(~scanf("%d%d",&m,&n)) { if(!n&&!m) break; int x,y; for(int i=0;i<n;i++) { scanf("%s",ma[i]); for(int j=0;j<m;j++) { if(ma[i][j]=='@')//起点; { x=i; y=j; } } } ans=0; dfs(x,y);//从起点开始搜索; printf("%d\n",ans); } return 0;}
bfs挫code………..
//利用bfs将可以到达的点塞进队列并标记,当出队列的时间就计数。
#include<cstdio>#include<iostream>#include<queue>#include<math.h>#include<string.h>#include<algorithm>using namespace std;#define eps 1e-8typedef __int64 LL;const int N=25;struct asd{ int x,y;};asd q[N*N];int n,m;char ma[N][N];int dx[4]={0,0,-1,1};int dy[4]={1,-1,0,0};int ans;bool Judge(int x,int y){ if(x<0||y<0||x>=n||y>=m||ma[x][y]=='#') return 0; return 1;}queue<asd>que; //建议还是要多写手写队列void bfs(int x,int y){ while(!que.empty()) que.pop(); asd now,next; now.x=x; now.y=y; ma[x][y]='#'; //标记 que.push(now); while(!que.empty()) { asd now; now=que.front();que.pop(); //出队列 ans++; for(int i=0;i<4;i++) { int xx=now.x+dx[i]; int yy=now.y+dy[i]; if(Judge(xx,yy)) { next.x=xx; next.y=yy; ma[next.x][next.y]='#'; //标记 que.push(next); } } }}int main(){ while(~scanf("%d%d",&m,&n)) { if(!n&&!m) break; int x,y; for(int i=0;i<n;i++) { scanf("%s",ma[i]); for(int j=0;j<m;j++) { if(ma[i][j]=='@') { x=i; y=j; } } } ans=0; bfs(x,y); printf("%d\n",ans); } return 0;}
0 0
- poj1979【基础bfs/dfs】
- poj1979 BFS/DFS
- POJ1979 Red and Black dfs基础题
- poj1979--DFS
- poj1979 DFS
- dfs--poj1979
- POJ1979 DFS
- poj1979 DFS
- POJ1979 DFS
- dfs-poj1979
- POJ1979 dfs
- poj1979 DFS
- poj1979-dfs
- POJ1979 FLIP GAME (DFS&BFS都可以)
- 基础BFS+DFS poj3083
- DFS BFS 搜索基础
- 【bfs与dfs】基础
- poj1979 简单bfs
- 个人项目-过滤代码工程文件注释
- 基于sublime搭建python开发环境(windows)
- [知了开发]“知了”优化 - WebMagic 调优
- 最详细的SVN安装使用手册-Centos6.4系统运行通过
- KCon黑客大会十大神器webzmap的安装与使用
- poj1979【基础bfs/dfs】
- Python 之 WSGI
- UVA - 10557 XYZZY
- Python-产生随机数
- 使用POSIX Threads进行多线程编程(三) ——条件变量
- 混合开发(一)——WebView开发高级技巧之加载网页以及JavaScript,加载进度条
- UVA - 10004 Bicoloring
- |Tyvj|并查集|P1602 团伙
- [BZOJ4523] [CQOI2016] 路由表 - Trie - 单调栈