Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.
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

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'.
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

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

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的更多相关文章

  1. 图论相关知识(DFS、BFS、拓扑排序、最小代价生成树、最短路径)

    图的存储 假设是n点m边的图: 邻接矩阵:很简单,但是遍历图的时间复杂度和空间复杂度都为n^2,不适合数据量大的情况 邻接表:略微复杂一丢丢,空间复杂度n+m,遍历图的时间复杂度为m,适用情况更广 前 ...

  2. 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. ...

  3. 在DFS和BFS中一般情况可以不用vis[][]数组标记

    开始学dfs 与bfs 时一直喜欢用vis[][]来标记有没有访问过, 现在我觉得没有必要用vis[][]标记了 看代码 用'#'表示墙,'.'表示道路 if(所有情况都满足){ map[i][j]= ...

  4. 列出连通集(DFS及BFS遍历图) -- 数据结构

    题目: 7-1 列出连通集 (30 分) 给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递 ...

  5. DFS与BFS题解:[kaungbin]带你飞 简单搜索 解题报告

    DFS and  BFS 在解题前我们还是大致讲一下dfs与bfs的.(我感觉我不会bfs) 1.DFS dfs(深度优先算法) 正如其名,dfs是相当的深度,不走到最深处绝不回头的那种. 深度优先搜 ...

  6. 数据结构(12) -- 图的邻接矩阵的DFS和BFS

    //////////////////////////////////////////////////////// //图的邻接矩阵的DFS和BFS ////////////////////////// ...

  7. 数据结构(11) -- 邻接表存储图的DFS和BFS

    /////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...

  8. 图论中DFS与BFS的区别、用法、详解…

    DFS与BFS的区别.用法.详解? 写在最前的三点: 1.所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次. 2.实现bfs和dfs都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...

  9. 图论中DFS与BFS的区别、用法、详解?

    DFS与BFS的区别.用法.详解? 写在最前的三点: 1.所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次. 2.实现bfs和dfs都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...

随机推荐

  1. SAX PULL解析实例

    XML三种解析方式: SAX解析:基于事件驱动,事件机制基于回调函数的,得到节点和节点之间内容时也会回调事件 PULL解析:相同基于事件驱动,仅仅只是回调时是常量 DOM解析:是先把XML文件装入内存 ...

  2. [Docker] Docker Client in Action

    Pull the docker image: docker pull hello-world Show all the images: docker images Remove the image: ...

  3. [转] JSON for java入门总结

    一.JSON介绍 JSON(JavaScript Object Notation),类似于XML,是一种数据交换格式,比如JAVA产生了一个数据想要给JavaScript,则除了利用XML外,还可以利 ...

  4. 通过源码安装最新版Git

    下载源码 到Git的Github主页上下载最新的源码到本地,解压并进入目录. 编译安装 cd 你的git源码目录 autoconf ./configure make 第一个报错 报错内容: usr/b ...

  5. 【Asp.Net】后台生成控件并绑定事件

    在Asp.Net的Web页面处理流程中,有时候我们会碰到需要动态生成的控件,并为之绑定相应的事件. 接下来我们来动态的生成一个控件 //在用户代码初始化阶段添加控件 protected void Pa ...

  6. boostrap按钮

    bootstrap按钮 对应链接:http://v3.bootcss.com/css/#buttons 使用时添加基础类class:btn 默认样式class=btn-default,控制大小clas ...

  7. VIM打开文件与保存文件

    打开文件 VIM /etc/inittab 默认的安装没有设置ctrl_W为退出.也可以设置::map <C-W> :close<CR> 或:map <C-W> : ...

  8. 利用Format函数格式化时间和日期

    在做机房收费系统的时候,因为需要使用到日期进行查询,所以在数据表中没有使用自动添加日期的功能,而是采用了自定义的格式插入.但由于事先没有对时间转换的格式进行统一,导致后面查询时出现的问题不断. 插入时 ...

  9. MySQL查询执行的基础

    当希望MySQL能够以更高的性能运行查询时,最好的办法就是弄清楚MySQL是如何优化和执行查询的.一旦理解这一点,很多查询优化实际上就是遵循一些原则让优化器能够按照预想的合理的方式运行. 换句话说,是 ...

  10. IRP派遣操作

    IRPTrace工具跟踪IRP 派遣函数(Dispathc Funtion)是windows驱动中的重要概念.驱动程序的主要功能是负责处理I/O请求,其中大部分I/O请求是在派遣函数中处理的.用户模式 ...