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)地图 ...
随机推荐
- 黑马程序员——OC语言 核心语法 (3)
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)分类Category 1) 基本用途 如何在不改变原来类模型的前提下 ...
- Xcode真机测试could not find developer disk image解决方法(支持iOS9.2)
这个问题开发者经常碰到,因为当我们更新手机iOS版本的时候,可能我们开发人员因为项目的需要等原因并一定愿意更新xcode到最新版本.但是老版本的xcode极有可能不支持最新的iOS版本,也有一些旧的i ...
- JVM-类加载机制
虚拟机类加载机制 虚拟机把描述的类的数据从class文件加载到内存后,并对数据进行校验,转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 类加载的时机 类被 ...
- jvm1
字节码常量池: 01开头的说明是一个utf-8编码的常量,那么后面就一定要跟两个字节也就是四位16进制的数,来表示这个常量占几个字节,然后后面再跟与这个字节数对应长度的utf-8编码的字符串.之所以一 ...
- [深入Python]__new__和__init__
class A(object): def __init__(self): print "init" def __new__(cls,*args, **kwargs): print ...
- SVG DOM常用属性和方法介绍
将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析器所特有的.SVG支持DOM2标准. 12.2 ...
- 关于ASP.NET页面打印技术的总结【转】
B/S结构导致了Web应用程序中打印的特殊性. • 程序运行在浏览器中,打印机在本地,而文件确可能在服务器上,导致了打印控制不是很灵活. • 格式如何控制和定制等,是我们开发中可能会面对的问题. 打印 ...
- C#中this在扩展方法的应用
给类添加扩展方法 1.定义一个类Service public class Service { private string _name; public string Name { get { retu ...
- linux命令:more
1.命令介绍: more用来逐页输出文件内容,空格键进入到下一页,b键返回到上一页. 2.命令格式: more [选项] 文件 3.命令参数 +n 从笫n行开始显示 -n 定义屏 ...
- OD调试篇7--笔记及解题报告
MFC:微软基础类库(英语:Microsoft Foundation Classes,简称MFC)是一个微软公司提供的类库(class libraries),以C++类的形式封装了Windows AP ...