poj 3083 Children of the Candy Corn
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8288 | Accepted: 3635 |
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
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
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
题目大意就是给一张地图,先输出左转的步数,再输出右转优先的步数,。最后输出最少步数
比较无脑的题,深搜2次广搜一次就0MS AC了,代码挺麻烦的
#include<stdio.h>
#include<string.h>
int step1[4][2] = { {0, -1}, {1, 0}, {0, 1}, {-1, 0} };
int step2[4][2] = { {0, -1}, {-1, 0}, {0, 1}, {1, 0} };
char map[45][45];
int s_x, s_y;
int new_x, new_y;
int dfs_left(int face, int x, int y)
{
if(map[x][y] == 'E')
return 1;
int myface = (face + 1) % 4;
if(map[x + step1[myface][0]][y + step1[myface][1]] != '#')
return dfs_left(myface, x + step1[myface][0], y + step1[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step1[myface][0]][y + step1[myface][1]] != '#')
return dfs_left(myface, x + step1[myface][0], y + step1[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step1[myface][0]][y + step1[myface][1]] != '#')
return dfs_left(myface, x + step1[myface][0], y + step1[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step1[myface][0]][y + step1[myface][1]] != '#')
return dfs_left(myface, x + step1[myface][0], y + step1[myface][1]) + 1; }
int dfs_right(int face, int x, int y)
{
if(map[x][y] == 'E')
return 1;
int myface = (face + 1) % 4;
if(map[x + step2[myface][0]][y + step2[myface][1]] != '#')
return dfs_right(myface, x + step2[myface][0], y + step2[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step2[myface][0]][y + step2[myface][1]] != '#')
return dfs_right(myface, x + step2[myface][0], y + step2[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step2[myface][0]][y + step2[myface][1]] != '#')
return dfs_right(myface, x + step2[myface][0], y + step2[myface][1]) + 1;
myface = (myface + 3) % 4;
if(map[x + step2[myface][0]][y + step2[myface][1]] != '#')
return dfs_right(myface, x + step2[myface][0], y + step2[myface][1]) + 1;
}
int bfs()
{
int queue[2000][3];
int top = 0, tail = 0;
queue[tail][0] = s_x;
queue[tail][1] = s_y;
queue[tail][2] = 1;
tail++;
int x, y, st;
while(top < tail)
{
int i;
x = queue[top][0];
y = queue[top][1];
st = queue[top][2];
top++;
for(i = 0; i < 4; i++)
{
if(map[x + step1[i][0]][y + step1[i][1]] != '#')
{
if(map[x + step1[i][0]][y + step1[i][1]] == 'E')
return st + 1;
queue[tail][0] = x + step1[i][0];
queue[tail][1] = y + step1[i][1];
queue[tail][2] = st + 1;
map[x + step1[i][0]][queue[tail][1] = y + step1[i][1]] = '#';
tail ++;
}
}
}
}
int calculateFace()
{
int i;
for(i = 0; i < 4; i++)
{
if(map[s_x + step1[i][0]][s_y + step1[i][1]] == '.' )
{
new_x = s_x + step1[i][0];
new_y = s_y + step1[i][1];
return i;
}
}
}
int calculateFace2()
{
int i;
for(i = 0; i < 4; i++)
{
if(map[s_x + step2[i][0]][s_y + step2[i][1]] == '.' )
{
new_x = s_x + step2[i][0];
new_y = s_y + step2[i][1];
return i;
}
}
}
int main()
{
int n;
scanf("%d", &n);
while(n--)
{
int w, h;
scanf("%d %d", &w, &h);
getchar();
memset(map, '#', sizeof(map));
int i, j;
for(i = 1; i <= h; i++)
{
for(j = 1; j <= w; j++)
{
scanf("%c", &map[i][j]);
if(map[i][j] == 'S')
{
s_x = i;
s_y = j;
}
}
getchar();
}
int face = calculateFace();
printf("%d ", dfs_left(face, new_x, new_y) + 1);
face = calculateFace2();
printf("%d ", dfs_right(face, new_x, new_y) + 1);
printf("%d\n", bfs());
}
return 0;
}
poj 3083 Children of the Candy Corn的更多相关文章
- 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)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
- 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 + 模拟)
题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...
- poj 3083 Children of the Candy Corn (广搜,模拟,简单)
题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...
- POJ 3083 Children of the Candy Corn 解题报告
最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...
- POJ 3083 Children of the Candy Corn (DFS + BFS)
POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...
随机推荐
- Halcon 10.0 Sample:完整性检查(圆形)
* ball.hdev: Inspection of Ball Bonding * 球接合检查 Comment Time:// *核心思想:.白色区域用作自动ROI,黑色区域是目标 * .Openin ...
- CSS属性之 -- overflow
overflow可以实现隐藏超出对象内容,同时也有显示与隐藏滚动条的作用,overflow属性有四个值:visible (默认), hidden, scroll, 和auto.同样有两个overflo ...
- java面试准备之基础排序——冒泡与选择排序
选择排序: [java] public void select(int[] arr){ for(int i=0;i<arr.length;i++){ ...
- android属性之excludeFromRecents -- clearTaskOnLaunch 隐身意图 启动activity
这个可以 用android 任务中app 隐藏起来 android属性之clearTaskOnLaunch 此属性是 每次启动app时都会进入 根目录中 android:clearTask ...
- 6、httpd服务的安装、配置
.本地yum源安装httpd服务 (必须是已搭建好本地yum源) yum install httpd -y (安装httpd) 2.systemctl restart httpd.service ...
- JS form表单图片上传
// 点击file 类型的input 触发的方法 function changesProvider(){ // fileProvider -> input中的name属性值 var f = do ...
- 数据库连接JDBC和数据库连接池C3P0自定义的java封装类
数据库连接JDBC和数据库连接池C3P0自定义的java封装类 使用以下的包装类都需要自己有JDBC的驱动jar包: 如 mysql-connector-java-5.1.26-bin.jar(5.1 ...
- (转)PhoneGap开发环境搭建
(原)http://www.cnblogs.com/Random/archive/2011/12/28/2305398.html PhoneGap开发环境搭建 项目中要用PhoneGap开发,了解 ...
- (转)解决Android SDK Manager无法更新或下载太慢问题
原帖地址:http://blog.csdn.net/exlsunshine/article/details/22208857 天朝的网络...哎~真是无语...还好最近装了谷歌的chrome浏览器+红 ...
- G - YY's new problem(HUSH算法,目前还不懂什么是HUSH算法)
Time Limit:4000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Pra ...