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. 2016NOI冬令营day2

    早上起来发现头不痛了(还是咳) : | 上午先讲自然语言处理!完全不考!完全不涉及!一开始挺有兴趣,后面就完全听不懂了 : | 后来又讲了几道IOI题目(自称只是op),然后就是  从信息熵到数据压缩 ...

  2. 全面理解Java内存模型(转)

    转自:http://blog.csdn.net/suifeng3051/article/details/52611310 Java内存模型即Java Memory Model,简称JMM.JMM定义了 ...

  3. cursor图标自定义

    cursor: url(./images/favicon.ico), auto; 首先auto必须加上,其次必须使用ico文件,目前来说ico文件没有兼容性问题,ico格式怎么转? 传送门:http: ...

  4. margin显示怪异,外边距合并问题

    很多时候我们使用两个div,内层的div设置文字,需要垂直居中与上层div,但是怎么设置样式都不行,vertical-align:middle也不行. 代码: <div style=" ...

  5. 0xc0000005:读取位置时发生访问冲突

    这是空指针,比如: A* a=NULL; a->fun();//会提示标题错误,因为a没有分配空间

  6. swift设计模式学习 - 代理模式

    移动端访问不佳,请访问我的个人博客 设计模式学习的demo地址,欢迎大家学习交流 代理模式 代理模式为其他对象提供一种代理以控制对这个对象的访问,在某些情况下,一个对象不适合或者不能直接引用另一个对象 ...

  7. 【安装防火墙】没有iptables时的解决办法

    一.检查iptables服务状态 首先检查iptables服务的状态 [root@woxplife ~]# service iptables status iptables: Firewall is ...

  8. 51nod 1242 斐波那契数列的第N项

    之前一直没敢做矩阵一类的题目 其实还好吧 推荐看一下 : http://www.cnblogs.com/SYCstudio/p/7211050.html 但是后面的斐波那契 推导不是很懂  前面讲的挺 ...

  9. 51Nod 1091 线段的重叠(贪心+区间相关

    1091 线段的重叠 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 20]和[12 25]的重叠部分为[12 20]. 给出N条线段的起点和终点,从中选出2条线段,这两 ...

  10. ASP.NET MVC之Bundle压缩JS和CSS

    介绍Bundle之前先引用<淘宝技术这十年>中一段话,对Web前端稍微有点常识的人都应该知道,浏览器下一步会加载页面中用到的CSS.JS(JavaScript).图片等样式.脚本和资源文件 ...