点击打开链接

Children of the Candy Corn
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8288   Accepted: 3635

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit. 



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

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

For 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

题目大意就是给一张地图,先输出左转的步数,再输出右转优先的步数,。最后输出最少步数

比较无脑的题,深搜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的更多相关文章

  1. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  2. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

  3. poj 3083 Children of the Candy Corn(DFS+BFS)

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

  4. 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 ...

  5. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)

    题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...

  6. poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】

    题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...

  7. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

  8. POJ 3083 Children of the Candy Corn 解题报告

    最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...

  9. POJ 3083 Children of the Candy Corn (DFS + BFS)

    POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...

随机推荐

  1. genymotion模拟器相关知识

    最近学react用到了Genymotion模拟器,但是因为墙的原因,遇到了很多阻碍,例如以下几种: 这里分享一个手动下载模拟器的方法. 1.  找到以下路径:C:\Users\Administrato ...

  2. Five More Hacker Tools Every CISO Should Understand

    As we mentioned in the first article, Top Five Hacker Tools Every CISO Should Understand, the role o ...

  3. 【转】ora-00031:session marked for kill处理oracle中杀不掉的锁

    一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...

  4. angular 国际化

    今天mentor说让我看看angular国际化的问题,我看了看,发现这个帖子很有价值,但是手头上还有好几个issue没有解决,所以就先考皮在这将连接,解决完这几个bug我再去细细研究这个问题, htt ...

  5. iOS Xcodebuild

    简介 xcodebuild 是苹果发布自动构建的工具.它在一个Xcode项目下能构建一个或者多个targets ,也能在一个workspace或者Xcode项目上构建scheme,总的来说,用它没错就 ...

  6. 国内较快的maven镜像

    原文网址:http://www.cnblogs.com/dingyingsi/p/3856456.html 国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以 ...

  7. 1-Recyclerview使用系列之Recyclerview的列表数据显示

    使用步骤已经写到我的公众号,二维码在下面,欢迎关注,谢谢. 本人联系方式: 更多精彩分享,可关注我的微信公众号: 若想给予我分享更多知识的动力,请扫描下面的微信打赏二维码,谢谢!: 微信号:Weixi ...

  8. Javascript 中的 && 和 || 使用小结

    准备两个对象用于下面的讨论. var alice = { name: "alice", toString: function () { return this.name; } }; ...

  9. python 杨辉三角

    前提:端点的数为1. 每个数等于它上方两数之和. 每行数字左右对称,由1开始逐渐变大. 第n行的数字有n项. 第n行数字和为2n-1. 第n行的m个数可表示为 C(n-1,m-1),即为从n-1个不同 ...

  10. Java 报表之JFreeChart(第二讲)

    1.利用 JFreeChart 创建按颜色分类的水果销售报表 package com.wcy.chart.bar; import javax.servlet.http.HttpSession; imp ...