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 ...
随机推荐
- .Net Framework 4.0安装cmd命令
在安装系统以后和.Net FrameWork 后,通过cmd编译编写的程序时总是提示编译错误.可以通过cmd命令安装相应的.net framework版本. 具体步骤如下: 1.以管理员身份打开cmd ...
- MVC模式下的数据展示:EasyUI的datagrid
我的数据库设计是一张老师表teacher,一张学生表student,一个教师对应多个学生,在学生一方建立外键; 还有一点想清楚,需要展示的数据是根据什么来的,是成功登陆的用户的id?还是直接展示所有的 ...
- android studio上代码编译调试中遇到的一些异常记录
下面是记录的在平时代码编写或编译时的一些异常,答案有自己摸索出来的,也有参考其他程序猿朋友的,参考文章过多,就不一一贴出来了. ① E/JavaBinder: !!! FAILED BINDER TR ...
- 简易google地图api调用
代码如下: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...
- c - 向一个排序好的数组插入一个数,插入后数组依然是排序好的
概述 因为这里只是演示插入1个数,这里我不用malloc再重新分配,而是将原先数组的腾出一个占位符. 完整代码如下: #include <stdio.h> #define LEN 6 // ...
- linux File Handling commands 'ls'.
ref:Linux / Unix Command: ls NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DES ...
- UIBarButtonItem-添加自定义Left或者Right按钮 <总结>
为UINavigationController添加UINavigationItem 1.添加返回导航按钮backBarButtonItem 1.用系统自带的返回按钮 UIBarButtonIt ...
- hibernate_validator_09
创建自己的约束规则 尽管Bean Validation API定义了一大堆标准的约束条件, 但是肯定还是有这些约束不能满足我们需求的时候, 在这种情况下, 你可以根据你的特定的校验需求来创建自己的约束 ...
- [转载]delete指针之后应该赋值NULL
首先,C++标准规定:delete空指针是合法的,没有副作用.但是,delete p后,只是释放了指针指向的内存空间.p并不会自动被置为NULL,而且指针还在,同时还指向了之前的地址. 问题来了,对一 ...
- 你好,C++(34)有一只叫做多利的羊 6.2.4 拷贝构造函数
6.2.4 拷贝构造函数 在C++世界中,除了需要使用构造函数直接创建一个新的对象之外,有时还需要根据已经存在的某个对象创建它的一个副本,就像那只叫做多利的羊一样,我们希望根据一只羊创建出来另外一只 ...