http://poj.org/problem?id=3083

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

题意:有一个迷宫,#代表墙,. 代表能走,S是起点,E是终点,M为宽,列数N为高;

先输出左转优先时,从S到E的步数(DFS)

再输出右转优先时,从S到E的步数(DFS)

最后输出S到E的最短步数(BFS)

题目解析:

之前一直没读懂题意,之后读懂了,却一直不会写,看了一下大神写的题解(汗颜啊),这题就是代码有点长,

但数据水,0MS,282kb。一遍就A了,如果用dfs求最短路,估计会超时,没有尝试。

左优先时,

依左上右下的顺时针方向走。根据上一个来的方向判断当前坐标开始走的方向

右优先时,
依右上左下的逆时针方向走。根据上一个来的方向判断当前坐标开始走的方向,可以走四个方向,最后才走当前位置的对面方向。
我是假设向下是0,向上是1,向右是2,向左是3,
左优先时,当前位置如果是向左,那么下一步依次走下,左,上,右。

(*)左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的格

分析:

最短路好办,关键是沿着墙走不太好想。

但只要弄懂如何转,这题就容易了。

单就沿着左走看一下:

当前方向    检索顺序

↑ :   ← ↑ → ↓

→ :   ↑ → ↓ ←

↓ :   → ↓ ← ↑

← :   ↓ ← ↑ →

如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。

在DFS时,加一个参数,用来保存当前的方向(d)。

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
using namespace std;
int n,m;
char map[][];
int v[][];
struct node
{
int ans,x,y;
};
struct node t,f;
int tx,ty,maxx,maxy;
int fx[]= {,-,,};
int fy[]= {,,,-};
void bfs()
{
queue<node>q;
memset(v,,sizeof(v));
t.x=tx;
t.y=ty;
t.ans=;
q.push(t);
v[t.x][t.y]=;
while(!q.empty())
{
t=q.front();
q.pop();
if(map[t.x][t.y]=='E')
{
printf("%d\n",t.ans);
return ;
}
for(int i=; i<; i++)
{
f.x=t.x+fx[i];
f.y=t.y+fy[i];
if(f.x>=&&f.x<n&&f.y>=&&f.y<m&&v[f.x][f.y]==&&map[f.x][f.y]!='#')
{
f.ans=t.ans+;
v[f.x][f.y]=;
q.push(f);
}
}
}
}
void dfs1(int xx,int yy,int d,int ans)//向左转
{
if(map[xx][yy]=='E')
{
maxx=ans;
return ;
}
if(d==)//0是下1是上2是右3是左
{
if(xx>=&&xx<n&&yy+>=&&yy+<m&&map[xx][yy+]!='#')
{
dfs1(xx,yy+,,ans+);
}
else if(xx+>=&&xx+<n&&yy>=&&yy<m&&map[xx+][yy]!='#')
{
dfs1(xx+,yy,,ans+);
}
else if(xx>=&&xx<n&&yy->=&&yy-<m&&map[xx][yy-]!='#')
{
dfs1(xx,yy-,,ans+);
}
else if(xx->=&&xx-<n&&yy>=&&yy<m&&map[xx-][yy]!='#')
{
dfs1(xx-,yy,,ans+);
}
}
else if(d==)
{
if(xx>=&&xx<n&&yy->=&&yy-<m&&map[xx][yy-]!='#')
{
dfs1(xx,yy-,,ans+);
}
else if(xx->=&&xx-<n&&yy>=&&yy<m&&map[xx-][yy]!='#')
{
dfs1(xx-,yy,,ans+);
}
else if(xx>=&&xx<n&&yy+>=&&yy+<m&&map[xx][yy+]!='#')
{
dfs1(xx,yy+,,ans+);
}
else if(xx+>=&&xx+<n&&yy>=&&yy<m&&map[xx+][yy]!='#')
{
dfs1(xx+,yy,,ans+);
}
}
else if(d==)
{
if(xx->=&&xx-<n&&yy>=&&yy<m&&map[xx-][yy]!='#')
{
dfs1(xx-,yy,,ans+);
}
else if(xx>=&&xx<n&&yy+>=&&yy+<m&&map[xx][yy+]!='#')
{
dfs1(xx,yy+,,ans+);
}
else if(xx+>=&&xx+<n&&yy>=&&yy<m&&map[xx+][yy]!='#')
{
dfs1(xx+,yy,,ans+);
}
else if(xx>=&&xx<n&&yy->=&&yy-<m&&map[xx][yy-]!='#')
{
dfs1(xx,yy-,,ans+);
}
}
else if(d==)
{
if(xx+>=&&xx+<n&&yy>=&&yy<m&&map[xx+][yy]!='#')
{
dfs1(xx+,yy,,ans+);
}
else if(xx>=&&xx<n&&yy->=&&yy-<m&&map[xx][yy-]!='#')
{
dfs1(xx,yy-,,ans+);
}
else if(xx->=&&xx-<n&&yy>=&&yy<m&&map[xx-][yy]!='#')
{
dfs1(xx-,yy,,ans+);
}
else if(xx>=&&xx<n&&yy+>=&&yy+<m&&map[xx][yy+]!='#')
{
dfs1(xx,yy+,,ans+);
}
}
return ;
}
void dfs2(int xx,int yy,int d,int ans)//向右转
{
if(map[xx][yy]=='E')
{
maxy=ans;
return ;
}
if(d==)//0是下1是上2是右3是左
{
if(xx>=&&xx<n&&yy->=&&yy-<m&&map[xx][yy-]!='#')
{
dfs2(xx,yy-,,ans+);
}
else if(xx+>=&&xx+<n&&yy>=&&yy<m&&map[xx+][yy]!='#')
{
dfs2(xx+,yy,,ans+);
}
else if(xx>=&&xx<n&&yy+>=&&yy+<m&&map[xx][yy+]!='#')
{
dfs2(xx,yy+,,ans+);
}
else if(xx->=&&xx-<n&&yy>=&&yy<m&&map[xx-][yy]!='#')
{
dfs2(xx-,yy,,ans+);
}
}
else if(d==)
{
if(xx>=&&xx<n&&yy+>=&&yy+<m&&map[xx][yy+]!='#')
{
dfs2(xx,yy+,,ans+);
}
else if(xx->=&&xx-<n&&yy>=&&yy<m&&map[xx-][yy]!='#')
{
dfs2(xx-,yy,,ans+);
}
else if(xx>=&&xx<n&&yy->=&&yy-<m&&map[xx][yy-]!='#')
{
dfs2(xx,yy-,,ans+);
}
else if(xx+>=&&xx+<n&&yy>=&&yy<m&&map[xx+][yy]!='#')
{
dfs2(xx+,yy,,ans+);
}
}
else if(d==)
{
if(xx+>=&&xx+<n&&yy>=&&yy<m&&map[xx+][yy]!='#')
{
dfs2(xx+,yy,,ans+);
}
else if(xx>=&&xx<n&&yy+>=&&yy+<m&&map[xx][yy+]!='#')
{
dfs2(xx,yy+,,ans+);
}
else if(xx->=&&xx-<n&&yy>=&&yy<m&&map[xx-][yy]!='#')
{
dfs2(xx-,yy,,ans+);
}
else if(xx>=&&xx<n&&yy->=&&yy-<m&&map[xx][yy-]!='#')
{
dfs2(xx,yy-,,ans+);
}
}
else if(d==)
{
if(xx->=&&xx-<n&&yy>=&&yy<m&&map[xx-][yy]!='#')
{
dfs2(xx-,yy,,ans+);
}
else if(xx>=&&xx<n&&yy->=&&yy-<m&&map[xx][yy-]!='#')
{
dfs2(xx,yy-,,ans+);
}
else if(xx+>=&&xx+<n&&yy>=&&yy<m&&map[xx+][yy]!='#')
{
dfs2(xx+,yy,,ans+);
}
else if(xx>=&&xx<n&&yy+>=&&yy+<m&&map[xx][yy+]!='#')
{
dfs2(xx,yy+,,ans+);
}
}
return ;
}
int main()
{
int T,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=; i<n; i++)
{
scanf("%*c%s",map[i]);
}
for(int i=; i<n; i++)
{
for(j=; j<m; j++)
{
if(map[i][j]=='S')
{
tx=i;
ty=j;
if(i==)//开始方向向下
{
dfs1(tx+,ty,,);
dfs2(tx+,ty,,);
}
else if(i==n-)//开始方向向上
{
dfs1(tx-,ty,,);
dfs2(tx-,ty,,);
}
else if(j==)//开始方向向右
{
dfs1(tx,ty+,,);
dfs2(tx,ty+,,);
}
else if(j==m-)//开始方向向左
{
dfs1(tx,ty-,,);
dfs2(tx,ty-,,);
}
break;
}
}
if(j!=m) break;
}
printf("%d %d ",maxx,maxy);
bfs();
}
return ;
}

POJ:3083 Children of the Candy Corn(bfs+dfs)的更多相关文章

  1. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  2. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

  3. poj 3083 Children of the Candy Corn(DFS+BFS)

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

  4. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  5. poj3083 Children of the Candy Corn BFS&&DFS

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Acce ...

  6. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)

    题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...

  7. poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】

    题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...

  8. POJ 3083 Children of the Candy Corn (DFS + BFS)

    POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...

  9. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

随机推荐

  1. 虚拟机可以ping同宿主机,宿主机ping不通虚拟机

    虚拟机里能ping同本机,而本机却ping不通虚拟机,或者虚拟机不能ping通本机,可能有如下原因: 如果是桥接模式,那么可能性1:虚拟机防火墙禁ping,请关闭虚拟机防火墙重试:root 状态下se ...

  2. RAID在数据库存储上的应用

    随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列(Redundant Arrays of Inexpensive/Independent Disks,RAID)出现了,RAID把多块独立的磁 ...

  3. html2canvas - 项目中遇到的那些坑点汇总(更新中...)

    截图模糊    原理就是讲canvas画布的width和height放大两倍. 后来学习canvas的时候,才了解到这种写法不同于css的宽高设置, 因为css里的只是展示画布显示的大小,不像这样是c ...

  4. Excel中countif函数的使用方法

    1.countif函数的含义 在指定区域中按指定条件对单元格进行计数(单条件计数) 建议和countifs函数结合起来学习,可以观看小编的经验Excel中countifs函数的使用方法. END 2. ...

  5. mac下升级terminal/终端的subversion版本方法

    雖然現在程式碼管理已經以 Git 為主了,不過偶爾需要維護一些舊案子還是會用 SVN,懶得轉了. Mac OS 本身有內建 SVN,不過卻是 1.6 版,最近修改一個舊案子就有碰到 project 已 ...

  6. sendfile Linux函数

    现在流行的 web 服务器里面都提供sendfile 选项用来提高服务器性能,那到底 sendfile 是什么,怎么影响性能的呢? sendfile 实际上是 Linux 2.0+ 以后的推出的一个系 ...

  7. mrtg 和 rrdtools

    mrtg可能很多人都用过,但那已经是n久以前的事了,现在在国内很多IDC,ISP都还用这个,因为我们这有个Linux科学家,在Linux工作n 久,我也就沾点光,学了不少东西,现在给大家介绍一个rrd ...

  8. Unity3D 边缘高光Shader

    Shader "Custom/NewShader" { Properties { _MainTex ("Base (RGB)", 2D) = "whi ...

  9. AutoMapper queryable extensions 只找需要的字段

    http://jahav.com/blog/automapper-queryable-extensions/ How to generate a LINQ query for your DTOs Au ...

  10. CentOS上传下载查看命令

    之前往CentOS中上传都用ftp软件,这里介绍一种另外的上传下载方式,两个命令轻松搞定.这两个命令目前只针对Xshell和SecureCRT等远程终端软件才支持,并且还会有时间的限制.大概30秒不上 ...