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都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...
随机推荐
- 新版福昕阅读器(Foxit Reader)启动速度慢解决办法
新版福昕阅读器(FoxitReader)启动速度慢解决办法之前喜欢使用福昕阅读器的原因就是看中了其小巧,可是最近版本的阅读器打开速度变得慢了很多(不是电脑配置问题),比AdobeReader还要慢,这 ...
- 枚举的基本使用方法 Enumerations
枚举的基本使用方法 Enumerations Enumeration enum SomeEnumeration{ case enumeration1 case enumeration2 case ...
- LSPCI具体解释分析
一.PCI简单介绍 PCI是一种外设总线规范.我们先来看一下什么是总线:总线是一种传输信号的路径或信道.典型情况是,总线是连接于一个或多个导体的电气连线,总 线上连接的全部设备可在同一时间收到 ...
- laravel实现数据库读写分离配置或者多读写分离配置
config\database.php里 读写分离:'mysql' => array( 'read' => array( 'host' => '192.168.1.1', ), 'w ...
- [转] gdb 查看vector, list, map 内容
转:http://blog.chinaunix.net/uid-13982689-id-34282.html先下载gdb_stl_utils.tar.gz, extract it, and run m ...
- 自定义绘制View
Paint(画笔) Canvas(画布) The Canvas class holds the "draw" calls. To draw s ...
- linux常见设备类型及文件系统
As you can see in Table 14.3 , all disk device names end with the letter a. That is because it ...
- linux系统中中断已连接的用户
1.用w命令查看当前系统登录的用户 [root@rhel7 ~]# w :: up :, users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOG ...
- 设置Cacti图形标题能显示中文
1.查看系统是否带有中文字体包 # ls /usr/share/fonts/chinese 如没有则安装 # yum -y install fonts-chinese 2.设置cacti使用的rr ...
- php改写session到数据库
session改写mysql 在调用 session_start();的地方改用实例化本类即可new SessionDB(); session_set_save_handler( array($thi ...