题目链接

题意:

先沿着左边的墙从 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. 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.

    在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...

  2. (转)void指针(void *的用法)

    指针有两个属性:指向变量/对象的地址和长度 但是指针只存储地址,长度则取决于指针的类型 编译器根据指针的类型从指针指向的地址向后寻址 指针类型不同则寻址范围也不同,比如: int*从指定地址向后寻找4 ...

  3. ubuntu下安装tomcat和配置mysql

    1.到官网 http://jakarta.apache.org/tomcat-7.0.2.tar.gz 安装文件,JDK假设已经安装完毕,接下来直接安装tomcat-7.0.2.tar.gz # ls ...

  4. [转] linux之sed用法

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed ...

  5. android核心服务初探

    终于进入android学习的进阶阶段,第一个课题是android的核心服务.首先,让我们来认识一下核心服务. android核心服务与app服务有所区别.app服务继承自Service基类,在app运 ...

  6. Verilog之event的用法

    编写verilog的testbench时,可使用event变量触发事件. event变量声明为: event var; event触发为: ->var; 捕获触发为: @(var); 在mode ...

  7. 简单html以及css的用法

    我将利用三天的时间来完成制作京东首页的静态页面效果,其中包含的内容有html以及css. 1.在开发进行之前,首先要配置开发环境:我们需要安装sublime  webstorm  vscode  Hb ...

  8. 修改DeDe标签Pagelist分页样式

    我们在用dede仿站的时候,调用文章列表页的分页时,我们会用到: {dede:pagelist listitem=”info,index,end,pre,next,pageno” listsize=” ...

  9. css.day04

    1. box   盒子模型 <p>   <span>   <hr/>   <div> css+   div  p  span css+  xhtml b ...

  10. 把某个asp.net 控件 替换成 自定义的控件

    功能:可以把某个asp.net 控件 替换成 自定义的控件 pages 的 tagMapping 元素(ASP.NET 设置架构) 定义一个标记类型的集合,这些标记类型在编译时重新映射为其他标记类型. ...