博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3083 相对位置的DFS的变形和BFS
阅读量:7218 次
发布时间:2019-06-29

本文共 4231 字,大约阅读时间需要 14 分钟。

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

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

28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########

Sample Output

37 5 517 17 9
#include
#include
#include
#include
#include
using namespace std;#define N 45struct P{ int x; int y; int step;};int t[N][N];int w,h,a,b,sum;char c[N][N];int d1[4]={-1,0,1,2};int d2[4]={1,0,-1,2};int dir[4][2]={
{0,-1},{-1,0},{0,1},{1,0}};bool dfs(int x1,int y1,int t,int *d){ for(int i=0;i<4;i++) { int t1=(t+4+d[i])%4; int xx=x1+dir[t1][0]; int yy=y1+dir[t1][1]; if(xx==a&&yy==b) return true; if(xx>=0&&xx
=0&&yy
q; P p1,p2; p1.x=x1; p1.y=y1; p1.step=1; t[x1][y1]=1; q.push(p1); while(!q.empty()) { p1=q.front(); q.pop(); for(int i=0;i<4;i++) { int xx=p1.x+dir[i][0]; int yy=p1.y+dir[i][1]; if(xx==a&&yy==b) { printf("%d\n",p1.step+1); break; } if(xx>=0&&xx
=0&&yy
>t1; while(t1--) { memset(t,0,sizeof(t)); scanf("%d%d",&w,&h); for(i=0;i

我觉得这道题需要极强的方向感,出门就忘记路的大路痴我哭晕在厕所,用两个数组来存储左边优先搜索和右边优先搜索的位置,因为下一个位置的方向和上一个位置的方向有关,相当于模拟一个人的走路,比如右拐后,则在二维数组上,此时人面向右边,左边即是二维地图的上方。

#include
#include
#include
#include
#include
using namespace std;#define N 45struct P{ int x; int y; int step;};int t[N][N];int w,h,a,b,sum;char c[N][N];int d1[4]={-1,0,1,2};int d2[4]={1,0,-1,2};int dir[4][2]={
{0,-1},{-1,0},{0,1},{1,0}};bool dfs(int x1,int y1,int t,int *d){ for(int i=0;i<4;i++) { int t1=(t+4+d[i])%4; int xx=x1+dir[t1][0]; int yy=y1+dir[t1][1]; if(xx==a&&yy==b) return true; if(xx>=0&&xx
=0&&yy
q; P p1,p2; p1.x=x1; p1.y=y1; p1.step=1; t[x1][y1]=1; q.push(p1); while(!q.empty()) { p1=q.front(); q.pop(); for(int i=0;i<4;i++) { int xx=p1.x+dir[i][0]; int yy=p1.y+dir[i][1]; if(xx==a&&yy==b) { printf("%d\n",p1.step+1); break; } if(xx>=0&&xx
=0&&yy
>t1; while(t1--) { memset(t,0,sizeof(t)); scanf("%d%d",&w,&h); for(i=0;i
最短路直接用广搜即可

转载于:https://www.cnblogs.com/zhangmingzhao/p/7256412.html

你可能感兴趣的文章
第十篇、自定义UIBarButtonItem和UIButton block回调
查看>>
复分析学习1
查看>>
Java虚拟机笔记(四):垃圾收集器
查看>>
计算机运行命令全集
查看>>
WebSocket 实战
查看>>
二次排序
查看>>
CSS:如何清除a标签之间的默认留白间距
查看>>
selenium随笔
查看>>
leetcode599
查看>>
String类中“==”和“equals()”的区别
查看>>
leetcode--883
查看>>
the application could not be verified
查看>>
[转]Centos配置国内yum源
查看>>
redis数据类型和应用场景
查看>>
Spring IOC
查看>>
Fragment的onCreateView和onActivityCreate之间的区别(转)
查看>>
AC日记——统计难题 hdu 1251
查看>>
在仿真器中运行时跳过Windows Azure Startup任务
查看>>
android 获取路径目录方法以及判断目录是否存在,创建目录
查看>>
数列问题[HAOI2004模拟]
查看>>