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. 自定义仿 IPhone 开关控件

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  2. java基础精选题

    Integer比较 看下面这段有意思的代码,对数字比较敏感的小伙伴有没有发现异常? public static void main(String[] args) { Integer a = 128,b ...

  3. .NETCoreCSharp 中级篇2-3 Linq简介

    .NETCoreCSharp 中级篇2-3 本节内容为Linq及其拓展方法.Linq中表达式树的使用 简介 语言集成查询(LINQ)是一系列直接将查询功能集成到C#语言的技术统称.数据查询历来都表示为 ...

  4. asp.net core 一个中小型项目实战的起手式——项目搭建与仓储模式下的持久层创建(1)

    常规的中小型项目搭建方式一般是三层架构加上mvc与webapi作为一个主要框架,再加上一些第三方库,例如orm框架(EF.SqlSugar.Dapper等),API文档工具(Swagger)这些的应用 ...

  5. android ——多线程编程

    1.定义一个线程只需要新建一个类继承自Thread.然后重写run()方法,并在里面编写耗时逻辑即可: class MyThread extends Thread{ public void run() ...

  6. DMP大数据营销

    一.下载大数据营销APP 使用手机浏览器扫描二维码 二.使用大数据营销APP 1.打开app,如果手机没有打开蓝牙和GPS定位app会自动提示让您打开,若app没有提示请手动去打开蓝牙和GPS 2.搜 ...

  7. 利用DoHome APP和音箱控制继电器通断电实验参考步骤

    准备材料: Arduino Uno 一块 Arduino 扩展板        购买链接 DT-06模块一个       购买链接 安卓手机一个 小度音箱一个 继电器模块一个 杜邦线若干 1.DT-0 ...

  8. k8s学习笔记

    9.deployment:声明式的升级应用 9.1.使用RC实现滚动升级 #kubectl rolling-update kubia-v1 kubia-v2 --image=luksa/kubia:v ...

  9. MySQL MGR集群单主模式的自动搭建和自动化故障修复

    随着MySQL MGR的版本的升级以及技术成熟,在把MHA拉下神坛之后, MGR越来越成为MySQL高可用的首选方案.MGR的搭建并不算很复杂,但是有一系列手工操作步骤,为了简便MGR的搭建和故障诊断 ...

  10. Spring源码剖析1:初探Spring IOC核心流程

    本文大致地介绍了IOC容器的初始化过程,只列出了比较重要的过程和代码,可以从中看出IOC容器执行的大致流程. 接下来的文章会更加深入剖析Bean容器如何解析xml,注册和初始化bean,以及如何获取b ...