poj3083 Children of the Candy Corn BFS&&DFS
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11215 | Accepted: 4841 |
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
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
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
Source
题目大意:从入口S进到出口E,如果一直按照走左边的路(碰到墙后返回来继续),或一直选择向右走,各自需要多少步;和求最短路(简单BFS)
这道题虽说是一道题,却同时考察了DFS和BFS
当然,这道题的困难之处不在于DFS有多复杂,而在于如何一直向左走或向右走却不受位置的影响,解决办法是设置两个数组顺时针与逆时针,方法如下:
设左上右下为 0, 1, 2, 3
顺时针时,假设当前的前进方向为d, 那么从(d+2)%4,也就是相反方向开始循环,每次
(d+1)%4,遇到第一个能走的就前进。
逆时针时同理,不同的是每次(d-1+4)%4。
下面附上本人比较挫代码
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
char str[maxn][maxn];
bool vis[maxn][maxn];
int stepl=,stepr=,stepm=;
int n,m;
int sx,sy,sd;
int dirl[][]={-,,,,,,,-};
int dirr[][]={,,,,-,,,-};
struct node{
int x,y,dis;
};
node u,v;
int step;
int dfs(int x,int y,int dir,int a[][]){
if(str[x][y]=='E')
return ;//特别注意,返回1
for(int i=;i<;i++){
int tdir=(dir+i+)%;//tdir需要重新定义
int tx=x+a[tdir][];
int ty=y+a[tdir][];
if(tx>=&&tx<n&&ty>=&&ty<m&&str[tx][ty]!='#'&&!vis[tx][ty]){
step =dfs(tx,ty,tdir,a)+;//+1
break;
}
}
return step;
} int bfs(){
memset(vis,false,sizeof(vis));
u.x=sx,u.y=sy,u.dis=;
vis[sx][sy]=true;
queue<node>q;
q.push(u);
while(!q.empty()){
u=q.front();
q.pop();
if(str[u.x][u.y]=='E')
return u.dis;
for(int i=;i<;i++){
v.x=u.x+dirl[i][];
v.y=u.y+dirl[i][];
if(v.x>=&&v.x<n&&v.y>=&&v.y<m&&
!vis[v.x][v.y]&&str[v.x][v.y]!='#'){
vis[v.x][v.y]=true;
v.dis=u.dis+;
q.push(v);
}
}
}
} int main(){
int t;
scanf("%d",&t);
while(t--){ memset(str,,sizeof(str));
memset(vis,false,sizeof(vis));
scanf("%d%d",&m,&n);
for(int i=;i<n;i++){
scanf("%s",str[i]);
for(int j=;j<m;j++){
if(str[i][j]=='S')
sx=i,sy=j;
}
}
int tx,ty; for(int i=;i<;i++){//判断左走方向
tx=sx+dirl[i][];
ty=sy+dirl[i][];
if(str[tx][ty]=='.'){
sd=i;
break;
}
}
step=;//每次向左或向右需要重新赋值为9
stepl=dfs(sx,sy,sd,dirl);//对于左走进行dfs for(int i=;i<;i++){//判断左走方向
tx=sx+dirr[i][];
ty=sy+dirr[i][];
if(str[tx][ty]=='.'){
sd=i;
break;
}
}
step=;
stepr=dfs(sx,sy,sd,dirr);//对于左走进行dfs stepm=bfs();//最短路进行bfs
printf("%d %d %d\n",stepl,stepr,stepm);
}
return ;
}
poj3083 Children of the Candy Corn BFS&&DFS的更多相关文章
- Children of the Candy Corn (bfs+dfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8120 Accepted: 3547 Description The c ...
- 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的步数 ...
- POJ3083——Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...
- 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)
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...
- POJ3083 Children of the Candy Corn(Bfs + Dfs)
题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S.E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以 ...
- POJ-3083 Children of the Candy Corn (BFS+DFS)
Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and mus ...
- poj 3083 Children of the Candy Corn(DFS+BFS)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
随机推荐
- 最好的 6 个 HTML5 的多媒体播放器
是 HTML5 中新引入的标签,用来在 Web 网页中嵌入视频播放功能,无需 Flash 和其他嵌入式插件的支持,是浏览器内建的功能,不过 旨在一些高级浏览器中支持,例如 Firefox, Sa ...
- squid隐藏squid的版本号
reply_header_access Via deny all reply_header_access Cache-Control deny all reply_header_access Serv ...
- 2018.5.28 Oracle数据库补充
select * from (select rownum rn,e2.* from (select e1.* from emp e1)e2 where rownum<=10)e3 where e ...
- Hadoop完全分布式集群环境搭建
1. 在Apache官网下载Hadoop 下载地址:http://hadoop.apache.org/releases.html 选择对应版本的二进制文件进行下载 2.解压配置 以hadoop-2.6 ...
- pymysql 简单操作数据库
#!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/4/24 15:22 # software: PyCh ...
- 记Tea使用中遇到的问题及解决过程
学习Markdown时,在小众软件看到一个叫Tea的软件.UI设计是简约风格:"所见即所得"的Markdown:支持插件等原因让我选择去尝试这杯"茶". 最近一 ...
- 阿里云服务器下安装LAMP环境(CentOS Linux 6.3) 安装与配置 Apache 服务
想让我们的阿里云服务器成为一台 Web 服务器,我们需要安装一个 Web 服务器软件,比如 Apache ,或者 Nginx 等等.下面我们就一起来安装一个 Apache 服务. 我们可以使用 yum ...
- 1074: [SCOI2007]折纸origami
Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 372 Solved: 229[Submit][Status][Discuss] Descriptio ...
- Nginx+proxy_cache图片缓存
搭建图片缓存机制的原理在于减少数据库的负担并加快静态资源的响应. 步骤: 1. vim /usr/local/nginx/conf/nginx.conf 2. http{ ... .. ...
- 开发监测keepalived裂脑的脚本
检测思路:在备节点上执行脚本,如果可以ping通主节点并且备节点有VIP就报警,让人员介入检查是否裂脑. 在LB02备节点上开发脚本并执行: [root@lb02 ~]# cat /server/sc ...