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)地图 ...
随机推荐
- 转摘: CSDN linxianliang5201314 的 blog ------sql解释执行顺序
我们做软件开发的,大部分人都离不开跟数据库打交道,特别是erp开发的,跟数据库打交道更是频繁,存储过程动不动就是上千行,如果数据量大,人员流动 大,那么我么还能保证下一段时间系统还能流畅的运行吗?我么 ...
- JavaScript常见问题整合
一. 基本变化<SCRIPT LANGUAGE="javascript"> <!-- window.open ('page.html', 'newwindow', ...
- SqlServer索引及优化详解
实际上,您可以把索引理解为一种特殊的目录.微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引.簇集索引)和非聚集索引(nonclustered index ...
- c/c++面试题(4)字符串翻转/打印任意进制格式/类型转换
1.字符串的翻转,这里一般是字符数组.不包括字符串字面值. char* reversal_str(char* str,size_t size); 翻转之后的字符串是原来的字符串的翻转. #includ ...
- springmvc 向页面传值
- 关于VS2013中Win32程序怎么修改图标
首先向资源文件上加上你要添加的资源(把你要添加的图标放在你的工程的下面,然后右击资源文件选中添加资源,然后选择导入你要添加的图标),下面你只要打开你项目的.rc文件要用查看代码形式打开,然后只要把系统 ...
- 通过j-interop访问WMI实例代码
代码: import java.net.UnknownHostException; import java.util.logging.Level; import org.jinterop.dcom.c ...
- CSS篇章
页面的组成:页面=数据(后台技术jsp|asp|.net|php|python)+Html(显示)+CSS(样式)+js(动效) CSS:层叠样式表 特点:①CSS和HTML分离 ...
- Linux线程-pthread_kill
该函数可以用于向指定的线程发送信号: int pthread_kill(pthread_t threadId,int signal); 如果线程内不对信号进行处理,则调用默认的处理程式,如SIGQUI ...
- Python 手写数字识别-knn算法应用
在上一篇博文中,我们对KNN算法思想及流程有了初步的了解,KNN是采用测量不同特征值之间的距离方法进行分类,也就是说对于每个样本数据,需要和训练集中的所有数据进行欧氏距离计算.这里简述KNN算法的特点 ...