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 ...
随机推荐
- World Wind Java开发之四——搭建本地WMS服务器(转)
在提供地理信息系统客户端时,NASA还为用户提供了开源的WMS Server 服务器应用:World Wind WMS Server.利用这个应用,我们可以架设自己的WMS服务并使用自己的数据(也支持 ...
- C语言异常处理编程的三个境界
http://blog.csdn.net/treefish2012/article/details/17466487 这是上一次看完Herb Sutter的<Exceptional C++> ...
- python_73_pickle序列化(接72)
# json(为字符串形式)用于不同语言之间的数据交互,只适用于简单的数据交互,字典之类可以,函数就不行了,如下例 ''' import json def say(name):print('Hi!', ...
- JS中的async/await的执行顺序详解
虽然大家知道async/await,但是很多人对这个方法中内部怎么执行的还不是很了解,本文是我看了一遍技术博客理解 JavaScript 的 async/await(如果对async/await不熟悉 ...
- js数据结构处理--------扁平化数组处理为树结构数据
将扁平化的数组处理为树结构数据,我们可以利用对象来处理,对象的复制是浅拷贝,指向相同的内存地址: var arr = [ { id: 0, pid: -1, name: 'sadas' }, { id ...
- PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)
http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...
- Oracle小知识_长期总结
更新时间:2018年7月16日 11:22:28 一. 系统 1. 打开防火墙后 Oracle 无法链接 新建1521端口规则. 二.知识 A. 序列 1. nextval ------------- ...
- 按格式读取csv文件内容
string path = @"C:\Users\keen_\Downloads\upload\upload\Upload\20140701141934_export.csv"; ...
- RPC - 麻雀虽小,五脏俱全
说起 RPC (远程过程调用),大家应该不陌生.随着微服务.分布式越来越流行,RPC 应用越来越普遍.常见的 RPC 框架如:Dubbo.gRPC.Thrift 等.本篇文章不是介绍各种 RPC 的使 ...
- XCode5 使用AutoLayout情况下改变控件的 方法
[self.viewButtonsetTranslatesAutoresizingMaskIntoConstraints:NO]; //[self.view addConstraint:[NSLayo ...