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. MongoDB 复制集 (一) 成员介绍

       一 MongoDB 复制集简介          MongoDB的复制机制主要分为两种:          Master-Slave    (主从复制)      这个已经不建议使用       ...

  2. 【转】java.lang.StackOverflowError

    http://blog.csdn.net/g19920917/article/details/8765638 出现一个java.lang.StackOverflowError异常.弄了半天,又是问高手 ...

  3. FragmentStatePagerAdapter.notifyDataSetChanged不刷新页面的解决的方法

    公司做医疗产品的,显示操作用的是android.所以我就用上下两个部分大致是固定的,仅仅有中间会有6个页面的切换,当中会有两个用户的切换.即普通用户和管理员用户,图片能够大致展示一下 其他页面是同样的 ...

  4. HDOJ--4869--Turn the pokers【组合数学+高速幂】

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869 题意:有m张扑克.開始时所有正面朝下.你能够翻n次牌,每次能够翻xi张.翻拍规则就是正面朝下变背面朝 ...

  5. UVA - 10239 The Book-shelver&#39;s Problem

    Description Problem D The Book-shelver's Problem Input: standard input Output: standard output Time ...

  6. MYSQL 学习笔记1 -----mysqladmin -uroot -p status|extended-status

    root@server1 ~]# mysqladmin -uroot -p status -i -r extended-status|grep Handler_commit Enter passwor ...

  7. mysql wait_timeout和interactive_timeout总结

      (1)interactive_timeout:参数含义:服务器关闭交互式连接前等待活动的秒数.交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE ...

  8. Day02 - Python 基本数据类型

    1 基本数据类型 Python有五个标准的数据类型: Numbers(数字) String(字符串) List(列表) Tuple(元组) Dictionary(字典) 1.1 数字 数字数据类型用于 ...

  9. springMVC工作原理图

  10. bash登录式shell(完全切换)与非登陆式shell(不完全切换)区别

    1.以登录式shell切换用户 su - username 登录式shell读取配置文件及其顺序: /etc/profile /etc/profile.d/*.sh ~/.bash_profile ~ ...