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. Spring系列(一):Spring核心概念

    一.Spring概念 Spring是一种多层的J2EE应用程序框架,其核心就是管理资源组件以及依赖关系,Spring框架为现代基于java的企业应用程序提供了一个全面的编程和配置模型. 二.Sprin ...

  2. Myeclipse8.5上基于JAX-WS开发WebService

    1.JAX-WS介绍 JAX-WS规范是一组XML web services的JAVA API. 2.开发步骤 基于JAX-WS的WebService开发步骤如下: 2.1 新建一个Web Servi ...

  3. SQL中一些实用的快捷键

    Ctrl+A全选 快速选中一行: 若光标在这条语句末尾用Shift+Home 若光标在这条语句开头用Shift+End Ctrl+K+U快捷注释本行 Ctrl+K+C反注释 Ctrl+R  关闭下面的 ...

  4. Java学习|HTTP请求头

    https://www.cnblogs.com/honghong87/articles/6941436.html 常见http请求报文头属性      Accept:告诉服务端,客户端接受什么类型的响 ...

  5. SAP Special Fields in FAGLL03 transaction

    https://wiki.scn.sap.com/wiki/display/ERPFI/Special+Fields+in+FAGLL03+transaction https://wiki.scn.s ...

  6. React 练习项目,仿简书博客写作平台

    Introduction 技术栈:react + redux + react-router + express + Nginx 练习点: redux 连接 react-router 路由跳转 scss ...

  7. JMeter的JavaRequest探究

    1.背景 最近笔者的一位老朋友咨询了一个问题:在自定义的Java请求中如何编写多个请求?老朋友反应他们发送请求只能基于这种Java请求形式(代码调需用三方封装的jar包).这个问题恰巧不久前在笔者所在 ...

  8. lnmp环境搭建方法

    网上目前的一键搭建方法: 命令行安装: 1.源码编译安装:(个性化配置,安装配置过程繁琐) 2.使用yum或apt直接安装:(使用编译好的二进制文件安装,速度快) 3.军哥的lnmp一键脚本安装: 4 ...

  9. R-package XML 安装失败及解决方式

    安装R-package XML遇到的问题和解决方式 这个问题已经困扰了我很久很久一直找不到解决之法,后来终于找到了! Fedora 27, R 3.5.0, libxml和libxml2以及开发包均已 ...

  10. 微信小程序页面跳转url如何传对象参数

    两步走 首先第一步:wx.navigateTo({ url:"XXX"+"&params="+ JSON.stringify(obj); }) 第二步获 ...