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的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
随机推荐
- Linux下Oracle常用命令
1. 备份表 exp database_user/pass tables='(table1,table2)' file=filename.dmp(例如:exp ismrenbao/iflytek ta ...
- c++第五天:默认初始化
1.算数类型.(整型和浮点型) 类型决定了数据所占的比特数以及该如何解释这些比特的内容. 练习2.1... 各种类型在计算机中所占的比特数不同,解释方法不同.有符号要花费一个比特存储符号,最大正值要比 ...
- Python3.x(windows系统)安装requests库
Python3.x(windows系统)安装requests库 cmd命令: pip install requests 执行结果:
- c++中类似于java jprofiler/eclispe memoryanalysis的性能以及内存分析工具
visual studio有自带的,可以看MSDN,不过一般来说,我们比较关注linux下的,搜了下,比较好用的应该有gprof和valgrind,先记录,可参考如下: http://blog.csd ...
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元)题解
题意:5*6的格子,你翻一个地方,那么这个地方和上下左右的格子都会翻面,要求把所有为1的格子翻成0,输出一个5*6的矩阵,把要翻的赋值1,不翻的0,每个格子只翻1次 思路:poj 1222 高斯消元详 ...
- SxsTrace
https://troubleshooter.xyz/wiki/fix-the-application-has-failed-to-start-because-the-side-by-side-con ...
- Stream API
引例: 1 List<String> strList = Arrays.asList("zhaojigang","nana","tiany ...
- [bzoj 1260][CQOI 2007]涂色paint
Description 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符串表示这个目标:RGBGR. 每次你可以把一段连续 ...
- 03_Kafka集群操作
1.集群配置思路 1)每台节点上要启动一个broker进程,因此要配置每台的server.properties broker id, log.dirs, zookeeper.connect 2) 每台 ...
- UVa 10801 电梯换乘
https://vjudge.net/problem/UVA-10801 题意:有多个电梯,每个电梯只能到达指定楼层,每个电梯有速度,如果中途换乘电梯,需要额外加60s,求从0层到达k层的最少时间. ...