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

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
 #include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream>
using namespace std; struct point
{
int x, y, step;
}; struct point start, end;
int dir[][] = {{,-}, {-,}, {,}, {,}};
char maze[][];
bool vis[][]; int walk(int x, int y, int ex, int ey, int d, int num)
{
if(x == ex && y == ey)
return num;
d = (d+) % ;
while(maze[x+dir[d][]][y+dir[d][]] == '#')
d = (d+) % ;
walk(x+dir[d][], y+dir[d][], ex, ey, d, num+);
} void bfs()
{
queue<struct point>q;
memset(vis, , sizeof(vis));
start.step = ;
q.push(start);
while(!q.empty())
{
struct point u = q.front();
q.pop();
if(maze[u.x][u.y] == 'E')
{
printf("%d\n", u.step);
return;//不知道为什么改成return u.step就不对。求教。
}
if(maze[u.x-][u.y] != '#' && !vis[u.x-][u.y])
{
vis[u.x-][u.y] = ;
q.push((struct point){u.x-, u.y, u.step+});
}
if(maze[u.x+][u.y] != '#' && !vis[u.x+][u.y])
{
vis[u.x+][u.y] = ;
q.push((struct point){u.x+, u.y, u.step+});
}
if(maze[u.x][u.y-] != '#' && !vis[u.x][u.y-])
{
vis[u.x][u.y-] = ;
q.push((struct point){u.x, u.y-, u.step+});
}
if(maze[u.x][u.y+] != '#' && !vis[u.x][u.y+])
{
vis[u.x][u.y+] = ;
q.push((struct point){u.x, u.y+, u.step+});
}
}
} int main()
{
int t, n, m;
scanf("%d", &t);
while(t--)
{
memset(maze, '#', sizeof(maze));
scanf("%d %d", &n, &m);
for(int i = ; i <= m; i++)
{
for(int j = ; j <= n; j++)
{
cin >> maze[i][j];
if(maze[i][j] == 'S')
start = (struct point){i, j};
else if(maze[i][j] == 'E')
end = (struct point){i, j};
}
}
printf("%d %d ",
walk(start.x, start.y, end.x, end.y, , ),
walk(end.x, end.y, start.x, start.y, , )
);
bfs();
}
return ;
}

写了个多线程的交上果断缺少windows.h。。。

 #include <windows.h>
#include <stdio.h>
#include <queue>
#include <iostream>
using namespace std; struct point
{
int x, y, step;
}; struct point start, myend;
int dir[][] = {{,-}, {-,}, {,}, {,}};
char maze[][];
bool vis[][];
int bfs_ans; int walk(int x, int y, int ex, int ey, int d, int num)
{
if(x == ex && y == ey)
return num;
d = (d+) % ;
while(maze[x+dir[d][]][y+dir[d][]] == '#')
d = (d+) % ;
walk(x+dir[d][], y+dir[d][], ex, ey, d, num+);
} void thread_bfs()
{
struct point tmp;
queue<struct point>q;
memset(vis, , sizeof(vis));
start.step = ;
q.push(start);
while(!q.empty())
{
struct point u = q.front();
q.pop();
if(maze[u.x][u.y] == 'E')
{
bfs_ans = u.step;
return;
}
for(int i = ; i < ; i++)
{
if(maze[u.x+dir[i][]][u.y+dir[i][]] != '#' && !vis[u.x+dir[i][]][u.y+dir[i][]])
{
vis[u.x+dir[i][]][u.y+dir[i][]] = ;
tmp.x = u.x+dir[i][];
tmp.y = u.y+dir[i][];
tmp.step = u.step+;
q.push(tmp);
}
}
}
} int main()
{
DWORD handle_id_bfs;
HANDLE handle_bfs;
int t, n, m;
scanf("%d", &t);
while(t--)
{
memset(maze, '#', sizeof(maze));
scanf("%d %d", &n, &m);
for(int i = ; i <= m; i++)
{
for(int j = ; j <= n; j++)
{
cin >> maze[i][j];
if(maze[i][j] == 'S')
{
start.x = i;
start.y = j;
}
else if(maze[i][j] == 'E')
{
myend.x = i;
myend.y = j;
}
}
} handle_bfs = CreateThread(
NULL,
NULL,
(LPTHREAD_START_ROUTINE)&thread_bfs,
NULL,
NULL,
&handle_id_bfs
); printf("%d %d ", walk(start.x, start.y, myend.x, myend.y, , ),
walk(myend.x, myend.y, start.x, start.y, , ));
WaitForSingleObject(handle_bfs, INFINITE);
printf("%d\n", bfs_ans);
}
return ;
}

POJ 3083 Children of the Candy Corn bfs和dfs的更多相关文章

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

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

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

  3. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

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

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

  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 (DFS + BFS)

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

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

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

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

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

随机推荐

  1. Hibernate查询之SQL查询

    转自: Hibernate还支持使用SQL查询,使用SQL查询可以利用某些数据库的特性,或者用于将原有的JDBC应用迁移到Hibernate应用上.使用命名的SQL查询还可以将SQL语句放在配置文件中 ...

  2. 一个免费的自动化跨平台测试JavaScript的工具——BrowserSwarm

    BrowserSwarm是一个免费工具,能够自动化跨平台测试JavaScript.

  3. 【JAVA - SSM】之MyBatis输出映射

    MyBatis中的输出映射有两种:resultType和resultMap. 1.resultType 使用resultType进行结果映射时,只有当查询结果中有至少一列的名称和resultType指 ...

  4. C#验证邮件

    public static bool IsEmail(string email) { String strExp = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+( ...

  5. 【转】android错误 aapt.exe已停止工作的解决方法

    http://www.jb51.net/article/57420.htm 在使用eclipse进行安卓java的编程的时候,有时候我们会遇到这样的问题:那就是无故弹出aapt.exe停止工作的提示, ...

  6. (转载)MyEclipse github

           最近Git火得如日中天,而且速度体验和团队模式都很不错.手头正好有个学生实训项目,时间紧任务重,而且学校内网管理太紧,所以就想借助于Internet的分布式开发,因此想到了Github. ...

  7. android开发之单点触摸

    相对于多点触摸,单点触摸还是很简单的. 新建一个工程,先看看布局文件: <RelativeLayout xmlns:android="http://schemas.android.co ...

  8. 关于js当中一些糟糕的特性

    首先,不可否认,js是一门具有许多优秀特性的弱类型语言,但是这门语言在设计之初就投入了工程实践,没有经历严格的实验室测试,以致力于它是如此的粗糙,在相当长的一段时间很不受开发者待见,被视为一门玩具性的 ...

  9. 隐藏元素的宽高无法通过原生js获取的问题

    1.起源:移动app项目中,页面加载时需要加载国家下拉列表,将隐藏的透明浮层和一个显示加载过程中的框 显示出来,隐藏的透明浮层设置宽高都是100%即可,而这个加载提示框需要先得出它的宽高,然后再根据页 ...

  10. 一道JS addEventListener面试题

    在园里看到一道面试题,<div id="test">Click Here</div> var node=document.getElementById('t ...