POJ-3083

题意:

给一个h*w的地图.

'#'表示墙;

'.'表示空地;

'S'表示起点;

'E'表示终点;

1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角;

2)地图的四周是墙,还有'S'和'E';

3)'S'和'E'之间至少有一个'#'将他们分开;

4)'S'和'E'是可以到达的;

按顺序依次打印出从起点开始靠左行走,靠右行走,最短路径的的数量(包括‘S’和‘E’),仅允许水平或垂直方向。

思路:

这道题最麻烦的就是靠左,靠右,其实也只要弄懂靠左,靠右也就出来了,下面只说一下靠左是怎么走的,靠右自己对应着想想。

我们数字依次代表(左上右下)(0 1 2 3)

当前位置             搜索顺序

1                         0 1 2 3

2                         1 2 3 0

3                         2 3 0 1

0                         3 0 1 2

仔细想想是不是每次都是靠左边最开始搜,其实靠有就是反向想想,靠左是顺时针,靠右就是逆时针(想想是不是)

最后最短路径就bfs就行了,注意数组大小等等,小心RE,我就在这个错了好多次。

AC代码 + 部分测试数据

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 50;
char Map[maxn][maxn]; //地图
int vis[maxn][maxn];//标记数组
int w, h, sx, sy, zx, zy, ans, flag; struct node{
int x, y, step;
}; int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};//方向数组 bool check(int dx, int dy) {
if(dx >= 0 && dx < h && dy >= 0 && dy < w && Map[dx][dy] != '#') {
return true;
}
else return false;
} void dfs_left(int x, int y, int k) {
if(x == zx && y == zy) {
flag = 1;//标记返回
return;
}
else {
k = (k+3) % 4;
for(int i = k; i <= k+3; i++) {
int dx = x + dr[(i+4)%4];//这个是在得出搜索顺序后找规律
int dy = y + dc[(i+4)%4];
if(check(dx, dy)) {
ans++;
dfs_left(dx, dy, i%4);
if(flag == 1)
return;
}
}
}
} void dfs_right(int x, int y, int k) {
if(x == zx && y == zy) {
flag = 1;
return;
}
else {
k = k + 1;
for(int i = k; i >= k-3; i--) {
int dx = x + dr[(i+4)%4];
int dy = y + dc[(i+4)%4];
if(check(dx, dy)) {
ans++;
dfs_right(dx, dy, i%4);
if(flag == 1)
return;
}
}
}
} int bfs(int x, int y) {
queue<node>q;
vis[x][y] = 1;
node st;
st.x = x;st.y = y;st.step = 1;
q.push(st);
while(!q.empty()) {
node e = q.front();
q.pop();
if(e.x == zx && e.y == zy)return e.step;
for(int i = 0; i < 4 ; i++) {
int dx = e.x + dr[i];
int dy = e.y + dc[i];
if(check(dx, dy) && !vis[dx][dy]) {
vis[dx][dy] = 1;
node now;
now.x = dx;now.y = dy;now.step = e.step + 1;
q.push(now);
}
}
}
} int main() {
int t;
cin >> t;
while(t--) {
cin >> w >> h;
for(int i = 0; i < h; i++) {
cin >> Map[i];
for(int j = 0; j < w; j++) {
if(Map[i][j] == 'S') {//得到起点坐标
sx = i;
sy = j;
}
if(Map[i][j] == 'E') {//得到终点坐标
zx = i;
zy = j;
}
}
}
ans = 1;flag = 0;//初始化
dfs_left(sx, sy, 0);//向左搜索
cout << ans << " ";
ans = 1;flag = 0;//初始化
dfs_right(sx, sy, 0);//向右搜索
cout << ans << " ";
memset(vis, 0, sizeof(vis));//最bfs时的vis标记数组初始化
cout << bfs(sx, sy) << endl;//bfs最短路径输出结果
}
return 0;
}

部分测试数据:

//Input:
7
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########
3 3
###
S.#
#E#
40 40
######################################E#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#......................................#
#S######################################
40 40
########################################
#.#.#.#.#.#.#.#.#.#.#.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#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#
#......................................#
#S#E####################################
40 40
#E######################################
S......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
######################################.#
#......................................#
#......................................#
########################################
11 11
#S#########
#.........#
#.#.#.#.#.#
#...#...#.#
#####.###.#
#...#.#...#
#.#...#.#.#
#..##.#...#
#.#.#.###.#
#...#.#...#
#####E##### /*=======================================================*/
Output:
37 5 5
17 17 9
3 3 3
77 77 77
1481 5 5
3 1483 3
47 45 15

POJ 3083 Children of the Candy Corn (DFS + BFS)的更多相关文章

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

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

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

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

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

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

  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(DFS+BFS)

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

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

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

  7. 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 ...

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

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

  9. POJ 3083 Children of the Candy Corn 解题报告

    最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...

随机推荐

  1. Java的几种常见排序算法

    一.所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作.排序算法,就是如何使得记录按照要求排列的方法.排序算法在很多领域得到相当地重视,尤其是在大量数据的处理方面. ...

  2. Jquery.form异步上传文件常见问题解决

    Jquery.form常用方法我就不多说,主要说一下在使用过程中碰到的问题 1.提示 “xxxx” is not define 或者"xxx" is not a function ...

  3. Mysql索引进阶入门

    1. 索引操作 MySQL 索引 菜鸟 2. 索引类型 PRIMARY 唯一且不能为空:一张表只能有一个主键索引 INDEX 普通索引 UNIQUE 唯一性索引 FULLTEXT 全文索引:用于搜索很 ...

  4. hadoop学习(二)----HDFS简介及原理

    前面简单介绍了hadoop生态圈,大致了解hadoop是什么.能做什么.带着这些目的我们深入的去学习他.今天一起看一下hadoop的基石--文件存储.因为hadoop是运行与集群之上,处于分布式环境之 ...

  5. 【0812 | Day 13】闭包函数/装饰器/迭代器

    目录 闭包函数 无参装饰器 有参装饰器 迭代器 闭包函数 一.什么是闭包? 闭包指的是:函数内部函数对外部作用域而非全局作用域的引用. def outter(): x = 1 def inner(): ...

  6. studio无限轮播

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  7. js常见兼容性问题以及解决方法

    1.关于使用 event对象,出现的兼容性问题IE/Chrom: event.clientX;event.clientYFF/IE9以上/Chrom: 传参e   e.clientX;e.client ...

  8. 用原生JS实现AJAX和JSONP

    前端开发在需要与后端进行数据交互时,为了方便快捷,都会选择JQuery中封装的AJAX方法,但是有些时候,我们只需要JQuery的AJAX请求方法,而其他的功能用到的很少,这显然是没必要的.其实,原生 ...

  9. Python爬虫视频教程

    ├─第1章_[第0周]网络爬虫之前奏 │ ├─第1节_"网络爬虫"课程内容导学 │ │ 第1部分_全课程内容导学.mp4 │ │ 第2部分_全课程内容导学(WS00单元)学习资料. ...

  10. python实例:自动保存百度盘资源到百度盘中

    本实例的实现逻辑是,应用selenium UI自动化登录百度盘,读取存储百度分享地址和提取码的txt文档,打开百度盘分享地址,填入提取码,然后保存到指定的目录中 全部代码如下: # -*-coding ...