POJ:3083 Children of the Candy Corn(bfs+dfs)
http://poj.org/problem?id=3083
Description
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
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
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)的更多相关文章
- 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 bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- 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
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- poj3083 Children of the Candy Corn BFS&&DFS
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11215 Acce ...
- 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 ...
随机推荐
- Eclipse配色方案插件
Eclipse配色方案插件 真漂亮! 最近发现了一个Eclipse配色方案插件,这回给Eclipse配色太方便了. 插件主页:http://eclipsecolorthemes.org/ 插件提供了上 ...
- 【mac】php7.1 安装swoole 扩展
环境依赖: php- 或更高版本 gcc-4.4 或更高版本 make autoconf 下载源代码包后,在终端进入源码目录,执行下面的命令进行编译和安装 https://github.com/swo ...
- JS基础---->javascript的基础(一)
记录一些javascript的基础知识.只是一起走过一段路而已,何必把怀念弄的比经过还长. javascript的基础 一.在检测一个引用类型值和 Object 构造函数时, instanceof 操 ...
- JavaScript 正则表达式 通俗解释 快速记忆
1.正则表达式中最重要的三个符号: 1.1 B 在正则表达式中B有3种类型的括号: 1.1.1 方括号 “[“. 方括号"["内是需要匹配的字符.中括号括住的内容只匹配一个单一的字 ...
- angularjs笔记《二》
小颖最近不知怎么了,老是犯困,也许是清明节出去玩,到现在还没缓过来吧,玩回来真的怕坐车了,报了个两日游得团,光坐车了,把人坐的难受得,去了也就是爬山,回来感觉都快瘫了,小颖去的时候还把我家仔仔抱着一起 ...
- howdoi 简单分析
对howdoi的一个简单分析. 曾经看到过下面的这样一段js代码: try{ doSth(); } catch (e){ ask_url = "https://stackoverflow.c ...
- 使用curl进行s3服务操作
最近使用curl对s3进行接口测试,本想写个总结文档,但没想到已有前辈写了,就直接搬过来做个记录吧,原文见: http://blog.csdn.net/ganggexiongqi/article/de ...
- Spring Boot 商城项目
Spring Boot 商城项目 angularJS Demo1 <html> <head> <title>angularJS Demo1</title> ...
- Cglib 与 JDK动态代理的运行性能比较
都说 Cglib 创建的动态代理的运行性能比 JDK 动态代理能高出大概 10 倍,今日抱着怀疑精神验证了一下,发现情况有所不同,遂贴出实验结果,以供参考和讨论. 代码很简单,首先,定义一个 Test ...
- hdu6393Traffic Network in Numazu【树状数组】【LCA】
Traffic Network in Numazu Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...