POJ-3083 Children of the Candy Corn (BFS+DFS)
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
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
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)的更多相关文章
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- poj 3083 Children of the Candy Corn(DFS+BFS)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
- poj 3083 Children of the Candy Corn (广搜,模拟,简单)
题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...
- POJ3083 Children of the Candy Corn(Bfs + Dfs)
题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S.E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以 ...
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- 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 ...
- POJ 3083 Children of the Candy Corn (DFS + BFS)
POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...
- POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
随机推荐
- ACM题目———— 一种排序(STL之set)
描述 输入 第一行有一个整数 0<n<10000,表示接下来有n组测试数据:每一组第一行有一个整数 0<m<1000,表示有m个长方形:接下来的m行,每一行有三个数 ,第一个数 ...
- P2322 [HNOI2006]最短母串问题
P2322 [HNOI2006]最短母串问题 AC自动机+bfs 题目要求:在AC自动机建的Trie图上找到一条最短链,包含所有带结尾标记的点 因为n<12,所以我们可以用二进制保存状态:某个带 ...
- 01: tornado基础篇
目录:Tornado其他篇 01: tornado基础篇 02: tornado进阶篇 03: 自定义异步非阻塞tornado框架 04: 打开tornado源码剖析处理过程 目录: 1.1 Torn ...
- HTTP If-Modified-Since引发的浏览器缓存汇总
在看Spring中HttpServlet的Service方法时,对于GET请求,代码逻辑如下: if (method.equals(METHOD_GET)) { long lastModified = ...
- AS不能在手机上现在调试软件
这两天遇到的一个问题,(android studio2.0以上的版本),在在线调试应用的时候,将手机上的此程序卸载了,然后准备重新再AS中将这个程序推送到手机上,可是这时候发现不能推送,Log显示什么 ...
- 苹果电脑macbook 安装 Burp Suite pro_v1.7.37破解版
1.先去官网下载最新版本 Burp Suite Community Edition v1.7.36安装完成 https://portswigger.net/burp/communitydownload ...
- compile vi from source code
1.install ncurses library (vi depend on ncurses library) ./configure --prefix=/usr --with-termlib m ...
- POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)
http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...
- Ubuntu 16.04 kinetic 编译指定包
编译指定包 catkin_make -DCATKIN_WHITELIST_PACKAGES=baoming 使用上述命令后catkin_make会一直编译上面那个包,想要编译全部包,使用 catkin ...
- Index.cshtml”处的视图必须派生自 WebViewPage 或 WebViewPage<TModel>。
解决方案: 1,在每个视图上面添加 @inherits System.Web.Mvc.WebViewPage 2,将views中的web.config COPY到新的视图模版文件夹下,就可以了