题目链接

题意:

先沿着左边的墙从 S 一直走,求到达 E 的步数。

再沿着右边的墙从 S 一直走,求到达 E 的步数。

最后求最短路。

分析:

最短路好办,关键是沿着墙走不太好想。

但只要弄懂如何转,这题就容易了。

单就沿着左走看一下:

当前方向     检索顺序

↑ :      ← ↑ → ↓

→ :        ↑ → ↓ ←

↓ :      → ↓ ← ↑

← :        ↓ ← ↑ →

如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。

在DFS时,加一个参数,用来保存当前的方向。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue> using namespace std; const int maxn = +; int dx[] = {, -, , };
int dy[] = {-, , , }; int dl[][] = {{, -}, {-, }, {, }, {, }};
int dr[][] = {{, }, {-, }, {, -}, {, }}; int sx, sy, ex, ey, n, m;
char G[maxn][maxn]; struct Pos {
int x, y, s;
}; int dfs(int x, int y, int d, int step, int dir[][]) {
for(int i=; i<; i++) {
int j = ((d-+)%+i)%;
int nx = x+dir[j][];
int ny = y+dir[j][]; if(nx == ex && ny == ey) return step+;
if(nx < || ny < || nx > n || ny > m) continue;
if(G[nx][ny] == '#') continue; return dfs(nx, ny, j, step+, dir);
}
} int BFS(int sx, int sy) {
bool vis[maxn][maxn];
memset(vis, false, sizeof(vis)); queue<Pos> Q;
Q.push((Pos){sx, sy, });
vis[sx][sy] = true; while(!Q.empty()) {
Pos p = Q.front(); Q.pop(); if(p.x == ex && p.y == ey) return p.s; Pos np;
for(int d=; d<; d++) {
np.x = p.x + dx[d];
np.y = p.y + dy[d];
np.s = p.s + ; if(np.x < || np.x > n || np.y < || np.y > m) continue;
if(vis[np.x][np.y]) continue; if(G[np.x][np.y] != '#') {
vis[np.x][np.y] = true;
Q.push(np);
}
}
} return -;
} int main() {
int T, d1, d2;
//freopen("my.txt", "r", stdin);
scanf("%d", &T); while(T--) {
scanf("%d%d", &m, &n); for(int i=; i<n; i++) {
scanf("%s", G[i]);
for(int j=; j<m; j++) {
if(G[i][j] == 'S') { sx = i; sy = j; }
else if(G[i][j] == 'E') { ex = i; ey = j; }
}
} if(sx == ) { d1 = ; d2 = ; }
else if(sx == n-) { d1 = ; d2 = ; }
else if(sy == ) { d1 = ; d2 = ; }
else if(sy == m-) { d1 = ; d2 = ; } printf("%d ", dfs(sx, sy, d1, , dl));
printf("%d ", dfs(sx, sy, d2, , dr));
printf("%d\n", BFS(sx, sy)); } return ;
}

POJ3083 Children of the Candy Corn(搜索)的更多相关文章

  1. POJ3083——Children of the Candy Corn(DFS+BFS)

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

  2. poj3083 Children of the Candy Corn BFS&&DFS

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

  3. POJ3083 Children of the Candy Corn(Bfs + Dfs)

    题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S.E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以 ...

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

  5. poj3083 Children of the Candy Corn 深搜+广搜

    这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...

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

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

  7. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  8. Children of the Candy Corn 分类: POJ 2015-07-14 08:19 7人阅读 评论(0) 收藏

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

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

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

随机推荐

  1. [PWA] 16. Clean IDB

    When we save items to IndexDB, we need to think about clean the items or keep those in a short list. ...

  2. boost库在工作(15)绑定器与函数对象之三

    前面已经可以优美地解决两个参数的函数给算法for_each调用了,但是又会遇到这样的一种情况,当需要三个参数或者三个以上的参数给算法for_each调用呢?从STL里的绑定器bind1st,显然是不行 ...

  3. ubuntu vim YouComlpeteMe配置

    使用vundle安装时,在.vimrc中添加 Plugin 'Valloric/YouCompleteMe' 使用Bundle会安装失败原因未知 YCM编译时附加选项--system-libclang ...

  4. Swift 算法实战之路:基本语法与技巧

    Swift是苹果新推出的编程语言,也是苹果首个开源语言.相比于原来的Objective-C,Swift要更轻便和灵活.笔者最近使用Swift实践了大量的算法(绝大部分是硅谷各大公司的面试题),将心得体 ...

  5. Android(java)学习笔记221:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例)

    1.首先我们看看下面这个需求: 这里我们在A界面上,点击这个按钮"选择要发送的短信",开启B界面上获取网络上各种短信祝福语,然后B界面会把这些网络祝福语短信发送给A界面到" ...

  6. redis 中文手册

    https://redis.readthedocs.org/en/latest/ http://www.cnblogs.com/ikodota/archive/2012/03/05/php_redis ...

  7. python增删改查

    ###增删改查 names = ["zhangding","wangxu","wudong","cheng"] #增na ...

  8. Java 数据类型转换(转换成字节型)

    package com.mystudypro.byteutil; import java.io.UnsupportedEncodingException; public class ConToByte ...

  9. 【开源java游戏框架libgdx专题】-06-使用libgdx自带的日志方法

    Application 接口提供了简单的日志记录,并且提供了颗粒度的控制. Gdx.app.log("MyTag", "my informative message&qu ...

  10. Spring和CXF整合时报Unsupported major.minor version 51.0异常

    好吧,官网上有写:The current plan is that CXF 3.1 will no longer support Java 6 and will require Java 7 or n ...