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. iOS OC开发代码规范

    1.变量.类名.函数名 使用驼峰命名法 2.尽量使用完整的单词命名,尽量不采用 缩写单词 3.类名使用大写字母打头,前缀统一加上HH 例如:HHHomePageController 4.类的成员变量使 ...

  2. mysql学习笔记4---mysql 复制---源代码

    mysql: c:底层 C++:相对上层 主备复制:主库通知备库来取 MYSQL复制源代码代码:SQL文件夹 int start_slave_thread( #ifdef HAVE_PSI_INTER ...

  3. Windows与Linux下文件操作监控的实现

    一.需求分析: 随着渲染业务的不断进行,数据传输渐渐成为影响业务时间最大的因素.究其原因就是因为数据传输耗费较长的时间.于是,依托于渲染业务的网盘开发逐渐成为迫切需要解决的需求.该网盘的实现和当前市场 ...

  4. Yet Another 10 Common Mistakes Java Developers Make When Writing SQL (You Won’t BELIEVE the Last One)--reference

    (Sorry for that click-bait heading. Couldn’t resist ;-) ) We’re on a mission. To teach you SQL. But ...

  5. 【Android】随时随地退出程序

    新建一个 ActivityCollector 类作为活动管理器,代码如下所示:public class ActivityCollector {public static List<Activit ...

  6. mysql select 语法

    格式:select [选项子句] 字段表达式子句 [from子句] [where子句] [group by子句] [having子句] [order by子句] [limit子句]; 提示:子句的顺序 ...

  7. VB------VS2012 IDE

    当编辑器的前面出现很多小点不影响 运行的时候 Ctrl+E+S就可以取消

  8. Canvas保存图片保存到本地

    使用Canvas绘图,将图片保存到本地方法 一.使用HTML5 a标签的download属性,将图片保存到本地,不需要链接服务器 关于download属性:HTML5 <a>标签downl ...

  9. error MSB6006: “CL.exe”已退出

    解决方案之一: 删除 \Windows\System32 目录下 mspdb110.dll. 试试吧.

  10. jquery和js cookie的使用解析

    JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的.而cookie是运行在客户端的,所以可以用JS来设置cookie. 在这里分别通过 ...