BFS - 水题
来源:互联网 发布:ping的端口是多少 编辑:程序博客网 时间:2024/06/05 12:50
#include <iostream>#include <queue>#include <string.h>using namespace std;/**************************************************************************************************************** 题意:S为起点,D为终点,X为墙壁, '.'为空路,问是否可以从S走到D 思路: 1,广搜找路径 注意: 1,每次要从队首元素的邻接点开始遍历 2,定义了三个队列,一个存元素,另外两个存坐标 Input: Output: 4 4 No S.X. ..X. ..XD ..X.****************************************************************************************************************/char map[10][10];int visit[10][10]={0};int exist=0;queue <char> Q1;queue <int> Q2,Q3;void bfs(int X,int Y){ visit[X][Y]=1; Q1.push(map[X][Y]); Q2.push(X); Q3.push(Y); while(!Q1.empty()) { char c=Q1.front(); int x=Q2.front(); int y=Q3.front(); //cout<<"c= "<<c<<" x= "<<x<<" y= "<<y<<endl; Q1.pop(); Q2.pop(); Q3.pop(); if(c == 'D') {exist=1;} if(!visit[x-1][y] && map[x-1][y] != 'X') {visit[x-1][y]=1; Q1.push(map[x-1][y]);Q2.push(x-1);Q3.push(y);} if(!visit[x+1][y] && map[x+1][y] != 'X') {visit[x+1][y]=1; Q1.push(map[x+1][y]);Q2.push(x+1);Q3.push(y);} if(!visit[x][y-1] && map[x][y-1] != 'X') {visit[x][y-1]=1; Q1.push(map[x][y-1]);Q2.push(x);Q3.push(y-1);} if(!visit[x][y+1] && map[x][y+1] != 'X') {visit[x][y+1]=1; Q1.push(map[x][y+1]);Q2.push(x);Q3.push(y+1);} }}int main(){ int n,m,T; while(cin>>n>>m>>T) { memset(map,'X',sizeof(map)); for(int i = 1;i <= n;i ++) for(int j = 1;j <= m;j ++) cin>>map[i][j]; for(int i = 1;i <= n;i ++) for(int j = 1;j <= m;j ++) if(map[i][j] == 'S') bfs(i,j); if(!exist) cout<<"No"<<endl; else cout<<"Yes"<<endl; } return 0;}
0 0
- BFS - 水题
- POJ3278 BFS 水题
- POJ 3170 BFS 水题
- POJ 3278 BFS水题
- hrbust 2159【BFS水题】
- BFS迷宫,水题
- HDOJ2717 BFS水题
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- PAT乙级 1005.继续(3n+1)猜想 (25)
- 使用HIVE注意点
- 状态栏 导航栏 tabbar背景色&字体颜色设置
- 彻底理解KMP算法
- hive启动报错 hive.metastore.HiveMetaStoreClient
- BFS - 水题
- 多线程(4):阻止线程运行的几种常见方式
- hbase问题 slf4j-log4j12
- Hadoop添加删除节点
- 中小规模Hadoop集群优化
- 设置Hive Map和Reduce的数量增加分析速度
- Java并发编程:Thread类的使用(详解)
- Linux LVM 扩容当前分区
- T4模板调试方法