题目链接

题意:

先沿着左边的墙从 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. Creating a simple static file server with Rewrite--reference

    Today, I’d like to take a quick moment to demonstrate how to make a simple file server using Rewrite ...

  2. java多态---ABC案列

    class A{ public void show(){ show2(); } public void show2(){ System.out.println("我"); } } ...

  3. sed 批量替换多个文件里的某个字符/串

    提示: 国际惯例使用前先备份 sed -i "s/a/b/g" `grep 'a' -rl ./`

  4. 异步tcp通信——APM.ConsoleDemo

    APM测试 俗话说麻雀虽小,五脏俱全.apm虽然简单,但是可以实现单机高性能消息推送(可以采用redis.kafka等改造成大型分布式消息推送服务器). 测试demo: using System; u ...

  5. COGS 渡轮问题 (LIS规定字典序输出方案数)

    /* 下标字典序最小 */ #include<iostream> #include<cstdio> #include<cstring> #define maxn 1 ...

  6. JY03-HTML/CSS-京东02

    ---恢复内容开始--- 1. position:absolute 1.1 绝对定位设置定位值为百分比时: 如设置right:50%,即元素右侧外边框距离父盒子右侧始终始终为父盒子宽度的一半. 可以使 ...

  7. AmazeUI 模态框封装

    /** * 模态窗口 */ window.Modal = { tpls:{ alert:'<div class="am-modal am-modal-alert" tabin ...

  8. 免写前缀JS包--prefixfree.min.js--插件

    /** * StyleFix 1.0.3 & PrefixFree 1.0.7 * @author Lea Verou * MIT license */ (function(){functio ...

  9. Wpf 数据绑定之BindingBase.StringFormat

    BindingBase.StringFormat 属性获取或设置一个字符串,该字符串指定如果绑定值显示为字符串,应如何设置该绑定的格式. StringFormat 可以是预定义的.撰写的或自定义的字符 ...

  10. 浅谈C++ 异常处理的语义和性能

    异常处理是个十分深奥的主题,这里只是浅论其对C++性能的影响. 在VC++中,有多个异常处理模式,三个最重要: No exception handling (无异常处理) C++ only (C++语 ...