POJ 3083 Children of the Candy Corn bfs和dfs
|
Children of the Candy Corn
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 Sample Output 37 5 5 |
#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的更多相关文章
- 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)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- 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 (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 (DFS + BFS)
POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...
- 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 ...
随机推荐
- 关于Ajax&初见Ajax
Ajax实现的效果 究竟Ajax能实现什么功能呢?今天下午学习了一下Ajax,现在跟大家分享一下我的学习心得.Ajax是什么?工作机制又是什么?可能不大准确,只是我个人看了视频学习后的一点点看法. A ...
- 统计功能和子对象的大小信息查询Bug
I hava below two statement sql: 0. not in subquery select a.schemaname, pg_size_pretty(pg_total_rela ...
- 最简单的基于FFMPEG的视频编码器(YUV编码为H.264)
本文介绍一个最简单的基于FFMPEG的视频编码器.该编码器实现了YUV420P的像素数据编码为H.264的压缩编码数据.编码器代码十分简单,可是每一行代码都非常重要,适合好好研究一下.弄清楚了本代码也 ...
- Java中PreparedStatement和Statement的用法区别(转)
1. PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象. 2.作为 ...
- android 66 sharedperference的使用
package com.itheima.qqlogin; import java.io.BufferedReader; import java.io.File; import java.io.File ...
- 查看LINUX发行商版本与LINUX内核版本
查看LINUX发行商版本:[root@server-mysql ~]# cat /etc/issue Red Hat Enterprise Linux Server release 6.3 (Sant ...
- Android开发之ViewPager实现多页面切换及动画效果(仿Android的Launcher效果)
Android开发中经常会有引导页或者切换页面等效果,本文采用ViewPager结合动画效果来实现仿Launcher以及页面切换的效果.源码地址在文章最后给出下载. 效果图如下: 1.Vi ...
- Tomcat 的 SSL 配置
本教程使用 JDK 6 和 Tomcat 7,其他版本类似. 基本步骤: 使用 java 创建一个 keystore 文件 配置 Tomcat 以使用该 keystore 文件 测试 配置应用以便使用 ...
- Bootstrap--全局CSS样式之排版
Bootstrap的排版样式大致和html基本元素一样,没什么大的区别,就是对元素加了样式. (1)标题 HTML 中的所有标题标签,<h1> 到 <h6> 均可使用.另外,还 ...
- Actioncontext跟ServletActionContext的区别---未完待续
//public class BaseAction extends ActionSupport{ public static HttpServletRequest getRequest(){ retu ...