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. JavaScript apply函数小案例

    //回调函数1 function callback(a,b,c) { alert(a+b+c); } //回调函数2 function callback2(a,b) { alert(a+b); } / ...

  2. Kerberos认证流程详解

    Kerberos是诞生于上个世纪90年代的计算机认证协议,被广泛应用于各大操作系统和Hadoop生态系统中.了解Kerberos认证的流程将有助于解决Hadoop集群中的安全配置过程中的问题.为此,本 ...

  3. [React] React Fundamentals: Build a JSX Live Compiler

    we want to have the ability to write JSX and see the output live in the browser. <!doctype html&g ...

  4. 偷偷mark下一个

    java书单 thinking in java java战 Effective Java 深入了解JVM虚拟机 java性能优化权威指南 JSR133 Google Guava官方教程 版权声明:本文 ...

  5. ActivityGroup相关--getLocalActivityManager()

    ActivityGroup简介 1.ActivityGroup的核心就是继承了该类,能够通过getLocalActivityManager()得到一个LocalActivityManager 如,Lo ...

  6. 使用QEMU调试Linux内核代码

    http://blog.chinaunix.net/uid-20729583-id-1884617.html http://www.linuxidc.com/Linux/2014-08/105510. ...

  7. 随便说说removeFromSuperview方法

    之前写过一篇关于removeFromSuperview方法处理的文章,写完后一直就没怎么更新这篇文章.这两天回过头来看看,感觉这篇文章有些地方写的不够严谨,而且还有一些自己理解错的地方,所以打算重写这 ...

  8. Java基础知识强化之IO流笔记26:FileInputStream / FileOutputStream 复制mp4视频的案例

    1.  需求:把D:\\English.mp4 复制到当前项目目录下copy.mp4 代码示例: package com.himi.filecopy; import java.io.FileInput ...

  9. scp文件到远端机器问题总结及解决方法

    今天在download服务器日志时遇到了很多问题, 顺便把相应的解决步骤记录下方便以后查看. #把文件copy到192.168.1.102的服务器上 scp -r local_dir readonly ...

  10. ASP.NET 资料下载

    public void downloadfile(string s_fileName) { HttpContext.Current.Response.ContentType = "appli ...