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 ...
随机推荐
- axf、elf文件转换成bin、hex脚本工具
在嵌入式开发过程中常常遇到将axf或elf文件转换成bin的情况,大家都知道通过gnu toolchain中的objcopy和keil中的fromelf能做到.可是为了这么一个小事而记住复杂的选项以及 ...
- Java使用Protocol Buffers入门四步骤
Protocol Buffers(简称protobuf)是谷歌的一项技术.用于将结构化的数据序列化.反序列化.经经常使用于网络传输. 这货实际上相似于XML生成和解析.但protobuf的效率高于XM ...
- 工厂模式[3] 抽象工厂 Abstract Factory
简介 1.简单工厂,或静态工厂,产品接口 定义:专门定义一个类来负责创建其他类的实例,被创建的实例通常具有共同的父类或实现同一接口 优点:客户端可以直接消费产品,而不必关心具体产品的实现(不关心对象的 ...
- XML 序列化与PULL解析
简介 Pull解析XML XmlPullParser解析器的运行方式与SAX解析器相似.它提供了类似的事件(开始元素和结束元素),但需要使用parser.next()方法来提取它们.事件将作为数值代码 ...
- MySQL被Oracle并购后的409个日日夜夜
2009年4月20日,Oracle并购了Sun,这也意味着MySQL归属到甲骨文的旗下.四百多天过去了,究竟这场并购结局如何?请看本文. 去年对Sun的收购,让甲骨文顺利的将一个潜在的对手MySQL收 ...
- 2.常用快捷键.md
[toc] 1.mian函数补全 在IntelJ中和Eclipse中稍有不同,在Eclipse中,输入main再按Alt+/即可自动补全main函数,但是在IntellJ中则是输入psvm,选中即可 ...
- uva 11529 Strange Tax Calculation (几何+计数)
题目链接: http://vjudge.net/problem/viewProblem.action?id=18277 这题暴力n^4妥妥的TLE!即使n^3也可能会T 正确的姿势应该是:枚举每个点作 ...
- 【USACO 2.2.3】循环数
[题目描述] 循环数是那些不包括0且没有重复数字的整数(比如81362)并且还应同时具有一个有趣的性质, 就像这个例子: 如果你从最左边的数字开始(在这个例子中是8)向右数最左边这个数(如果数到了最右 ...
- When Colon Scripting is comming (脚本最佳体验)
当冒号脚本来临-- 脚本最佳体验 冒号指派 说明; 冒号替代等号指派赋值,当命名声明指派时指定.相当于声明当前作用域的一个名字指派. 当对指定对象的属性赋值时候,依旧请使用等号.即不废弃等号赋值功用, ...
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...