POJ_3083——贴左右墙DFS,最短路径BFS
Description
One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)
As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.
Input
Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').
You may assume that the maze exit is always reachable from the start point.
Output
Sample Input
2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########
Sample Output
37 5 5
17 17 9
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; struct node
{
int x, y;
int step;
}start,end; int T, w, h, L_step, R_step, step; int dir[][]={-,,,-,,,,}; char map[][], *p; //左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的格
bool DFS_Left(int x,int y,int d)
{
int _d, _dx, _dy;
if(map[x][y]=='E')
{
return true;
} L_step++; //根据上一次走的方向,用公式推出下一次要走的方向 //往左走
_d = (d+)%;
_dx = x + dir[_d][];
_dy = y + dir[_d][];
if(map[_dx][_dy]!='#' && _dx>= && _dx<h && _dy>= && _dy<w)
{
if(DFS_Left(_dx,_dy,_d))
return true;
} //往原始方向走
_d = d;
_dx = x + dir[_d][];
_dy = y + dir[_d][];
if(map[_dx][_dy]!='#' && _dx>= && _dx<h && _dy>= && _dy<w)
{
if(DFS_Left(_dx,_dy,_d))
return true;
} //往右边走
_d = (d+)%;
_dx = x + dir[_d][];
_dy = y + dir[_d][];
if(map[_dx][_dy]!='#' && _dx>= && _dx<h && _dy>= && _dy<w)
{
if(DFS_Left(_dx,_dy,_d))
return true;
} //往回走
_d = (d+)%;
_dx = x + dir[_d][];
_dy = y + dir[_d][];
if(map[_dx][_dy]!='#' && _dx>= && _dx<h && _dy>= && _dy<w)
{
if(DFS_Left(_dx,_dy,_d))
return true;
} L_step--;
return false;
} bool DFS_Right(int x,int y,int d)
{
int _d, _dx, _dy;
if(map[x][y]=='E')
{
return true;
} R_step++; //根据上一次走的方向,用公式推出下一次要走的方向 //往右走
_d = (d+)%;
_dx = x + dir[_d][];
_dy = y + dir[_d][];
if(map[_dx][_dy]!='#' && _dx>= && _dx<h && _dy>= && _dy<w)
{
if(DFS_Right(_dx,_dy,_d))
return true;
} //往原始方向走
_d = d;
_dx = x + dir[_d][];
_dy = y + dir[_d][];
if(map[_dx][_dy]!='#' && _dx>= && _dx<h && _dy>= && _dy<w)
{
if(DFS_Right(_dx,_dy,_d))
return true;
} //往左边走
_d = (d+)%;
_dx = x + dir[_d][];
_dy = y + dir[_d][];
if(map[_dx][_dy]!='#' && _dx>= && _dx<h && _dy>= && _dy<w)
{
if(DFS_Right(_dx,_dy,_d))
return true;
} //往回走
_d = (d+)%;
_dx = x + dir[_d][];
_dy = y + dir[_d][];
if(map[_dx][_dy]!='#' && _dx>= && _dx<h && _dy>= && _dy<w)
{
if(DFS_Right(_dx,_dy,_d))
return true;
} R_step--;
return false;
} //寻找最短路只能用BFS
void BFS()
{
node temp,next;
queue<node>p;
p.push(start);
while(!p.empty())
{
temp=p.front();
p.pop(); if(temp.x==end.x && temp.y==end.y)
{
step = temp.step;
break;
} next.step = temp.step + ;
for(int i=;i<;i++)
{
next.x = temp.x + dir[i][];
next.y = temp.y + dir[i][];
if(map[next.x][next.y]!='#' && next.x>= && next.x<h && next.y>= && next.y<w)
{
//BFS中在原始地图中记录走过的路径,会把结束标记给覆盖掉,所以要预先存储
//终点坐标,或者另开一个数组记录路径
map[next.x][next.y]='#';
p.push(next);
}
}
}
return;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&w,&h);
for(int i=;i<h;i++)
{
scanf("%s",map[i]); //获取起点坐标
p=strchr(map[i],'S');
if(p!=NULL)
{
start.x=i;
start.y=p-map[i];
} p=strchr(map[i],'E'); if(p!=NULL)
{
end.x=i;
end.y=p-map[i];
}
} L_step=R_step=; start.step=; DFS_Left(start.x,start.y,);
DFS_Right(start.x,start.y,);
BFS();
printf("%d %d %d\n",L_step,R_step,step);
}
return ;
}
POJ_3083——贴左右墙DFS,最短路径BFS的更多相关文章
- 图论相关知识(DFS、BFS、拓扑排序、最小代价生成树、最短路径)
图的存储 假设是n点m边的图: 邻接矩阵:很简单,但是遍历图的时间复杂度和空间复杂度都为n^2,不适合数据量大的情况 邻接表:略微复杂一丢丢,空间复杂度n+m,遍历图的时间复杂度为m,适用情况更广 前 ...
- Clone Graph leetcode java(DFS and BFS 基础)
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- 在DFS和BFS中一般情况可以不用vis[][]数组标记
开始学dfs 与bfs 时一直喜欢用vis[][]来标记有没有访问过, 现在我觉得没有必要用vis[][]标记了 看代码 用'#'表示墙,'.'表示道路 if(所有情况都满足){ map[i][j]= ...
- 列出连通集(DFS及BFS遍历图) -- 数据结构
题目: 7-1 列出连通集 (30 分) 给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递 ...
- DFS与BFS题解:[kaungbin]带你飞 简单搜索 解题报告
DFS and BFS 在解题前我们还是大致讲一下dfs与bfs的.(我感觉我不会bfs) 1.DFS dfs(深度优先算法) 正如其名,dfs是相当的深度,不走到最深处绝不回头的那种. 深度优先搜 ...
- 数据结构(12) -- 图的邻接矩阵的DFS和BFS
//////////////////////////////////////////////////////// //图的邻接矩阵的DFS和BFS ////////////////////////// ...
- 数据结构(11) -- 邻接表存储图的DFS和BFS
/////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...
- 图论中DFS与BFS的区别、用法、详解…
DFS与BFS的区别.用法.详解? 写在最前的三点: 1.所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次. 2.实现bfs和dfs都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...
- 图论中DFS与BFS的区别、用法、详解?
DFS与BFS的区别.用法.详解? 写在最前的三点: 1.所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次. 2.实现bfs和dfs都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...
随机推荐
- c# linq的一些运用
最近在学习xml.linq 网上也找了一些资料都不大全面,因此在这写了一点东西和大家分享,由于本人知识有限,如有错误请指证 可扩展标记语言,标准通用标记语言的子集,一种用于标记电子文件使其具有结构性的 ...
- STL之Errors and Exceptions
Error Handling STL设计的目标是性能最优化,而不是最安全. 错误检查是极其浪费时间的,因此,STL对于错误处理几乎没有做处理,因此,这对STL的使用者的要求就非常高. 为什么不采取错误 ...
- INFORMATION_SCHEMA.COLUMNS 查询表字段语句
INFORMATION_SCHEMA.COLUMNS 视图以 sysobjects.spt_data type_info.systypes.syscolumns.syscomments.sysconf ...
- 3_Linux_文件搜索指令
.3文件搜索命令 1)which 查找一个命令所在的路径 whereis 提供命令的帮助文件的信息 whatis 显示命令的概要信息whatis ls which提供命令的别名信息 2)find,基本 ...
- 【转】app瘦身
iPhone经过这几年的发展,已经发生了很大的变化,例如屏幕变得更加多样,尺寸更多,内存变得更大,CPU的架构也在变化.伴随着iPhone的变化,iOS也在变化,例如AutoLayout.size c ...
- mysql02
-- 查询课程名称 和年级的名称 -- 非等值连接查询 SELECT subjectname,gradeName FROM `subject`,grade -- 等值连接查询 SELECT subje ...
- C#总结项目《影院售票系统》编写总结完结篇
回顾:昨天总结了影院售票系统核心部分-售票,整个项目也就完成了2/3了,需求中也要求了对销售信息的保存,今天就继续总结销售信息的保存以及加载销售信息. 分析:退出程序时将已售出票集合中的对象循环写入到 ...
- webform 简单的服务器控件。
服务器基本控件: 1 textbox text:获取或设置文本 textmode:单行/多行/密码... wrap:是否换行 rows:行数 ...
- HTML5和CSS3实例教程[总结一]
关于onclick的行为与内容分离 通过链接触发弹出窗口方式 (不推荐使用此方法!!!) <a href='#' onclcik = "window.open('holiday_pay ...
- Examples_08_07
http://yarin.iteye.com/?page=4 Activity01.java package com.yarin.android.Examples_08_07; import andr ...