Children of the Candy Corn (bfs+dfs)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8120 | Accepted: 3547 |
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
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
Sample Input
2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########
Sample Output
37 5 5
17 17 9
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std; struct node
{
int x,y;
int step;
};
int n,m;
char map[][];
int vis[][];
int dir[][] = {{-,},{,-},{,},{,}};//上左下右顺时针;
queue <node> que;
int ans3; int dfs(int sx,int sy, int ex, int ey, int d, int ans)
{
if(sx == ex && sy == ey)
return ans;
d = (d+)%;//左转;
while(map[sx+dir[d][]][sy+dir[d][]] == '#')
d = (d+)%;//当有墙的时候就右转直到没有墙;
return dfs(sx+dir[d][],sy+dir[d][],ex,ey,d,ans+);
} void bfs(int sx,int sy, int ex, int ey)
{
while(!que.empty())
que.pop();
memset(vis,,sizeof(vis)); que.push((struct node){sx,sy,});
vis[sx][sy] = ; while(!que.empty())
{
struct node u = que.front();
que.pop();
if(u.x == ex && u.y == ey)
{
ans3 = u.step;
return;
}
if(u.x- >= && !vis[u.x-][u.y] && map[u.x-][u.y] != '#')
{
vis[u.x-][u.y] = ;
que.push((struct node){u.x-,u.y,u.step+});
}
if(u.x+ <= n && !vis[u.x+][u.y] && map[u.x+][u.y] != '#')
{
vis[u.x+][u.y] = ;
que.push((struct node){u.x+,u.y,u.step+});
}
if(u.y- >= && !vis[u.x][u.y-] && map[u.x][u.y-] != '#')
{
vis[u.x][u.y-] = ;
que.push((struct node){u.x,u.y-,u.step+});
}
if(u.y+ <= m && !vis[u.x][u.y+] && map[u.x][u.y+] != '#')
{
vis[u.x][u.y+] = ;
que.push((struct node){u.x,u.y+,u.step+});
}
}
}
int main()
{
int t;
int sx,sy,ex,ey;
scanf("%d",&t);
while(t--)
{
memset(map,'#',sizeof(map));//外面加一道墙,从s进去之后只有一条路可走;
scanf("%d %d",&m,&n);
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
cin>>map[i][j];
if(map[i][j] == 'S')
{
sx = i;
sy= j;
}
if(map[i][j] == 'E')
{
ex = i;
ey = j;
}
}
}
printf("%d %d ",dfs(sx,sy,ex,ey,,),dfs(ex,ey,sx,sy,,));
bfs(sx,sy,ex,ey);
printf("%d\n",ans3);
}
return ;
}
Children of the Candy Corn (bfs+dfs)的更多相关文章
- poj3083 Children of the Candy Corn BFS&&DFS
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11215 Acce ...
- 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)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- 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(DFS+BFS)
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...
- poj 3083 Children of the Candy Corn(DFS+BFS)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
- POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- Children of the Candy Corn 分类: POJ 2015-07-14 08:19 7人阅读 评论(0) 收藏
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10933 Acce ...
随机推荐
- java 新建文件夹保存
String Txtname = field.getText(); File file3 =new File("D:\\MATP_robot"+&qu ...
- Qt 属性
Qt提供了一套和一些编译器提供商也提供的属性系统类似的完善的属性系统.然而,作为一个不依赖编译器和平台的库,Qt不能依赖像__property或者[property]那样的非标准编译器特征.我们的 ...
- 关于 java.util.concurrent 您不知道的 5 件事--转
第 1 部分 http://www.ibm.com/developerworks/cn/java/j-5things4.html Concurrent Collections 是 Java™ 5 的巨 ...
- Java基础知识强化之IO流笔记06:有return的情况下try catch finally的执行顺序
1. 给出结论: (1)不管有木有出现异常,finally块中代码都会执行:(2)当try和catch中有return时,finally仍然会执行:(3)finally是在return后面的表达式运算 ...
- Android中的Surface和SurfaceView
一.什么是Surface 简单的说Surface对应了一块屏幕缓冲区,每个window对应一个Surface,任何View都要画在Surface的Canvas上(后面有原因解释).传统的view共享一 ...
- JavaScript调用后台的三种方法实例(包含两种Ajax)
方法一:直接使用<%=%>调用(ASPX页面) 前台JS,代码如下: <script type="text/javascript"> var methodS ...
- codevs2492上帝造题的七分钟 2(线段树)
/* 区间修改 区间查询 可以用线段树搞 但是一般的标记下放对这个题好像不合适 只能改叶子 然后更新父亲(虽然跑的有点慢) 小优化:如果某个点是1 就不用再开方了 所以搞一个f[i]标记 i 这个点还 ...
- 解决CSS中float:left后需要clear:both清空的繁琐步骤(转)
之前,因为公司专门有CSS+DIV的切片设计师,所以我一直都是注重程序的设计与开发.现在,因为接了一些Web网站的项目需要制作,就在空闲时间学习起了CSS.Jquery. 现在,大部分的横排导航都 ...
- python面对对象编程---------6:抽象基类
抽象基本类的几大特点: 1:要定义但是并不完整的实现所有方法 2:基本的意思是作为父类 3:父类需要明确表示出那些方法的特征,这样在写子类时更加简单明白 用抽象基本类的地方: 1:用作父类 2:用作检 ...
- (转)PHP模板smarty简单入门教程
转之--http://blog.163.com/zf_2011@126/blog/static/166861361201062595057962/ 如何在smarty中开始我们程序设计.PHP代码:- ...