poj-3083-烦人复杂搜索+bfs,dfs模板
来源:互联网 发布:mac app store 很慢 编辑:程序博客网 时间:2024/05/21 07:52
仅仅记录,本题无意义仅仅是烦人,当以后模板吧
ps:其实可以不写这么多的从终点沿墙往右走跟从起点沿墙往左走是一个意思的。。。AC后想通的。。。哎思路还是太窄。
求条件是否成立用dfs较好,求最优解bfs最好。
#include<cstdio>#include<queue>using namespace std;char mp[50][50];struct node{ int x,y,dis;}no1,no2,star,en;int bu[4][2] = {0,1,0,-1,1,0,-1,0};int viewleft[4][2] = {-1,0,0,1,1,0,0,-1};int left[4][2] = {0,-1,-1,0,0,1,1,0};int viewright[4][2] = {-1,0,0,1,1,0,0,-1};int right[4][2] = {0,1,1,0,0,-1,-1,0};int m,n;int dfsl(node pre,int view){ if(pre.x==en.x&&pre.y==en.y)return pre.dis; int x = pre.x+left[view][0],y = pre.y+left[view][1]; if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]!='#'){ no1.x = x; no1.y = y; no1.dis = pre.dis+1; return dfsl(no1,(view-1+4)%4); } else { int x = pre.x+viewleft[view][0],y = pre.y+viewleft[view][1]; if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]!='#'){ no1.x = x; no1.y = y; no1.dis = pre.dis+1; return dfsl(no1,view); } else if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]=='#') return dfsl(pre,(view+1)%4); }}//沿着左走 int dfsr(node pre,int view){ if(pre.x==en.x&&pre.y==en.y)return pre.dis; int x = pre.x+right[view][0],y = pre.y+right[view][1]; if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]!='#'){ no1.x = x; no1.y = y; no1.dis = pre.dis+1; return dfsr(no1,(view+1)%4); } else { int x = pre.x+viewright[view][0],y = pre.y+viewright[view][1]; if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]!='#'){ no1.x = x; no1.y = y; no1.dis = pre.dis+1; return dfsr(no1,view); } else if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]=='#') return dfsr(pre,(view-1+4)%4); }}//沿着右边 int bfs(){ int co[50][50] ={0}; queue<node>qu; star.dis = 0; qu.push(star); co[star.x][star.y] = 1; while(!qu.empty()) { no1 = qu.front(); qu.pop(); if(no1.x==en.x&&no1.y==en.y)return no1.dis; for(int i=0;i<4;i++) { no2.x = no1.x+bu[i][0]; no2.y = no1.y+bu[i][1]; if(no2.x>=0&&no2.x<n&&no2.y>=0&&no2.y<m&&!co[no2.x][no2.y]&&mp[no2.x][no2.y]!='#') { no2.dis = no1.dis+1; qu.push(no2); co[no2.x][no2.y] = 1; } } }}//求最短路径。。。 int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); for(int i=0;i<n;i++) scanf("%s",mp[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(mp[i][j]=='S'){star.x = i;star.y = j;} if(mp[i][j]=='E'){en.x = i;en.y = j;} } int view; if(star.x==0)view = 2; else if(star.x==n-1)view = 0; else if(star.y==0)view = 1; else view = 3; printf("%d ",dfsl(star,view)+1); printf("%d ",dfsr(star,view)+1); printf("%d\n",bfs()+1); } return 0;}
0 0
- poj-3083-烦人复杂搜索+bfs,dfs模板
- poj 3083 搜索 DFS,BFS
- poj 3083 DFS+DFS+BFS
- POJ 3083 BFS+DFS
- poj 3083 dfs+bfs
- poj 3083 bfs+dfs
- poj 3083 DFS+BFS
- POJ 1979 基础搜索 DFS\BFS 一
- POJ 3083图搜 bfs+dfs
- POJ 3083(DFS+BFS)
- POJ 3083 (bfs + dfs)
- POJ 3083 Children of the Candy Corn DFS及BFS搜索
- Dearboy's Puzzle (poj 2308 搜索 dfs+bfs)
- POJ 3414 Pots——bfs搜索+dfs输出
- [搜索]DFS+BFS
- DFS、BFS搜索+题目
- DFS/BFS 搜索训练
- DFS BFS 搜索基础
- Android Studio NDK环境搭建与最简单例子编译执行
- SPOJ SUBST1 New Distinct Substrings 后缀数组-子串个数
- POJ 1661Help Jimmy(dp)
- strong 和 weak 指针
- Hibernate实验手册
- poj-3083-烦人复杂搜索+bfs,dfs模板
- MFC MDI 响应键盘事件
- JAVA中URL类的常用方法
- Atom设置震撼的编辑效果
- bzoj1208【HNOI2004】宠物收养所
- 【Linux内核层】深入netfilter编程
- 为何MySQL有了partition分区后不能用Geometry类???
- 对线性回归,logistic回归和一般回归的认识
- 中文乱码问题