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. Netty源码分析-- FastThreadLocal分析(十)

    上节讲过了ThreadLocal的源码,这一节我们来看下FastThreadLocal.这个我觉得要比ThreadLocal要简单,因为缺少了对于Entry的清理和整理工作,所以ThreadLocal ...

  2. python基础之循环与迭代器

    循环 python 循环语句有for循环和while循环. while循环while循环语法 while 判断条件: 语句 #while循环示例 i = 0 while i < 10: i += ...

  3. c#实现深拷贝的几种方法

    为什么要用到深拷贝呢?比如我们建了某个类Person,并且实例化出一个对象,然后,突然需要把这个对象复制一遍,并且复制出来的对象要跟之前的一模一样,来看下我们一般会怎么做,看代码 public cla ...

  4. fiddler设置断点

    1.有两种方法设置断点 before response:也就是发送请求之后,但是Fiddler代理中转之前,这时可以修改请求的数据 after response:也就是服务器响应之后,但是在Fiddl ...

  5. 逆向破解之160个CrackMe —— 001

    CrackMe —— 001 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...

  6. Go中的异常处理

    1. errors包 Go 有一个预先定义的 error 接口类型 : type error interface { Error() string } 错误值用来表示异常状态.Go也提供了一个包:er ...

  7. 关于Linux的简单介绍

    Linux: 诞生日期:1991年 开发者:林纳斯·托瓦茨 特点:免费,开源    发行版本:centos|red Hat|Ubuntu|红旗等    思想:一切都是文件 重要文件目录 bin:二进制 ...

  8. mybatis基础简介

    1.mybatis的加载过程? 程序首先加载mybatis-config.xml文件,根据配置文件创建SQLSessionFactory对象:    然后通过SQLSessionFactory对象创建 ...

  9. 建立apk定时自动打包系统第二篇——自动上传文件

    在<建立apk定时自动打包系统第一篇——Ant多渠道打包并指定打包目录和打包日期>这篇文章中介绍多渠道打包的流程.很多时候我们需要将打包好的apk上传到ftp中,这时候我可以修改custo ...

  10. 【已解决】Https请求—未能创建 SSL/TLS 安全通道

    在做项目的微信推送消息功能时,由于微信并发量大,导致其它第三方接口调用时直接挂掉报错. 问题: 测试工程师做压测,100个线程同时调用微信和XX站的接口,日志报XX站的“请求被中止: 未能创建 SSL ...