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 题目大意:从起点到终点,求三条路的长度。第一条是以总以左边优先,并且四个方位按顺时针遍历,直到终点的长度;第二条是总以右边优先,并且四个方位按逆时针遍历,直到终点的长度;第三条是最短路。
题目分析:显然,前两条路是DFS出来的,第三条是BFS出来的。 代码如下:
 # include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int x,y,t;
node(int _x,int _y,int _t):x(_x),y(_y),t(_t){}
};
char p[][];
bool flag;
int w,h,ans[],mark[][];
int d[][]={{-,},{,},{,},{,-}};
bool ok(int x,int y)
{
if(x>=&&x<h&&y>=&&y<w)
return true;
return false;
}
void dfs(int x,int y,int pos,int t,int step)
{
if(flag)
return ;
if(p[x][y]=='E'){
flag=true;
ans[(t==)]=step;
return ;
}
int nx,ny,np;
for(int i=,pp=(pos+t+)%;i<=;++i,pp=(pp-t+)%){
nx=x+d[pp][],ny=y+d[pp][];
if(ok(nx,ny)&&p[nx][ny]!='#'){
dfs(nx,ny,pp,t,step+);
if(flag)
return ;
}
}
}
void bfs(int sx,int sy)
{
queue<node>q;
memset(mark,,sizeof(mark));
mark[sx][sy]=;
q.push(node(sx,sy,));
while(!q.empty())
{
node u=q.front();
q.pop();
if(p[u.x][u.y]=='E'){
printf("%d\n",u.t);
return ;
}
for(int i=;i<;++i){
int nx=u.x+d[i][],ny=u.y+d[i][];
if(ok(nx,ny)&&!mark[nx][ny]&&p[nx][ny]!='#'){
mark[nx][ny]=;
q.push(node(nx,ny,u.t+));
}
}
}
}
int main()
{
int T,sx,sy,pos;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&w,&h);
for(int i=;i<h;++i){
scanf("%s",p[i]);
for(int j=;j<w;++j)
if(p[i][j]=='S')
sx=i,sy=j;
}
if(sx==)
pos=;
if(sx==h-)
pos=;
if(sy==)
pos=;
if(sy==w-)
pos=;
ans[]=ans[]=;
flag=false;
dfs(sx,sy,pos,-,);
flag=false;
dfs(sx,sy,pos,,);
printf("%d %d ",ans[],ans[]);
bfs(sx,sy);
}
return ;
}

做后感:这道题既恶心又水!!!

POJ-3083 Children of the Candy Corn (BFS+DFS)的更多相关文章

  1. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  2. poj 3083 Children of the Candy Corn(DFS+BFS)

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

  3. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

  4. POJ3083 Children of the Candy Corn(Bfs + Dfs)

    题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S.E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以 ...

  5. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  6. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

  7. POJ:3083 Children of the Candy Corn(bfs+dfs)

    http://poj.org/problem?id=3083 Description The cornfield maze is a popular Halloween treat. Visitors ...

  8. POJ 3083 Children of the Candy Corn (DFS + BFS)

    POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...

  9. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)

    题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...

随机推荐

  1. QQ群免IDKEY加群PHP源码

    加群链接需要idkey的,该源码自动解析idkey,实现免idkey加群. 该源码来自彩虹秒赞系统. 例如:api.yum6.cn/qqun.php?qun=463631294 <?php /* ...

  2. Python之路----列表推导式和生成器的表达式

    列表推导式 egg_list=['鸡蛋%s'%i for i in range(10)] print(egg_list) 列表推导式 推导过程 egg_list = [] for i in range ...

  3. TED #09# You don't have to be an expert to solve big problems

    Tapiwa Chiwewe: You don't have to be an expert to solve big problems Collection noticed a haze hangi ...

  4. 20145216史婧瑶《网络对抗》Web安全基础实践

    20145216史婧瑶<网络对抗>Web安全基础实践 实验问题回答 (1)SQL注入攻击原理,如何防御 攻击原理: SQL注入攻击指的是通过构建特殊的输入作为参数传入web应用程序,而这些 ...

  5. ms11_050漏洞攻击

    ms11_050漏洞攻击 开启msfconsole 查看针对MS11_050漏洞的攻击模块 进入模块 查看该模块的具体信息 设置paylaods 查看具体参数设置 设置其相关参数 攻击 ie浏览器发生 ...

  6. MacBook PRO蓝牙无法搜索设备

    背景 经常把MacBook合上盖子就塞进包里,用时打开盖子就继续操作,偶尔会出现刚刚还在用的罗技蓝牙鼠标,重新打开笔记本后就连接不上了,而且也无法搜索到周边的蓝牙设备. 解决方案 快捷键:Option ...

  7. Java命令使用 jmap,jps,jstack,jstat,jhat,jinfo

    Jmap:可以获得运行中的jvm的堆的快照,从而可以离线分析堆,以检查内存泄漏,检查一些严重影响性能的大对象的创建,检查系统中什么对象最多,各种对象所占内存的大小等等 Jmap是一个可以输出所有内存中 ...

  8. Maven可用setting.xml

    最简单的可用阿里镜像配置 <?xml version="1.0" encoding="UTF-8"?> <settings> <l ...

  9. hdu4528 小明系列故事——捉迷藏(记录状态的BFS)题解

    思路: 一道BFS题,和以前的BFS有点不同,这里的vis数组需要记录每次走时的状态,所以开了3维,只对该状态下的vis修改. 注意坑点:S的位置是可以走的 代码: #include<queue ...

  10. excel在msdn上的说明文档

    Microsoft.Office.Tools.Excel.Worksheet 对象提供和 Excel 主互操作程序集中的 Microsoft.Office.Interop.Excel.Workshee ...