题目链接

题意:

先沿着左边的墙从 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. jni java和C之间的值传递(int String int[])

    我们通过jni调用C代码不可能每次只是去调一个方法,通常,我们需要传递一些值过去. 例如,播放电影,那就肯定需要你把电影的 url给 C的播放器吧,等等. 接下来就看一看怎么去传递这些值: 首先是最简 ...

  2. PHP安全编程:网站安全设计的一些原则(转)

    深度防范 深度防范原则是安全专业人员人人皆知的原则,它说明了冗余安全措施的价值,这是被历史所证明的. 深度防范原则可以延伸到其它领域,不仅仅是局限于编程领域.使用过备份伞的跳伞队员可以证明有冗余安全措 ...

  3. mac缺少预编译.a问题

    在win7的svn提交了coco2d-x 3.0代码,在mac进行更新,用xcode打开工程,编译不成功,一看好多的.a文件全部都是红色的,无法找到文件,一开始不了解coco2d-x的prebuilt ...

  4. C#“简单加密文本器”的实现

    本示例只能加密英文文本,使用的算法为异或算法.源代码:http://pan.baidu.com/share/link?shareid=3241348313&uk=1761850335(本示例属 ...

  5. Java基础知识强化之集合框架笔记10:Collection集合使用的步骤

    集合使用的步骤: (1)创建集合对象 (2)创建元素对象 (3)把元素添加到集合 (4)遍历集合:       • 通过集合对象获取迭代器对象 • 通过迭代器对象的hasnext()方法判断是否有元素 ...

  6. noip 2012 疫情控制

    /* 考试的时候没想出正解 也没打暴力 时间不够了 随便yy了几种情况按出现的先后顺序处理而没有贪心 的了20分 不粘了 正解是围绕首都的儿子来搞的 显然先二分答案 对于每个限定的最大时间 我们尝试着 ...

  7. ORACLE 数据库总结

    1.表和数据恢复 1.从回收站里查询被删除的表 select object_name,original_name,partition_name,type,ts_name,createtime,drop ...

  8. HDU3480

    题意:给你n个数,然后让你分成m个集合,每个集合有一个值(最大值减最小值,然后平方),求整个集合的可能最小值. 思路:因为每个集合里的值只和最大和最小值有关,所以很容易想到先排序,然后用DP可求得解, ...

  9. configSections(配置文件)

    转载:http://www.cnblogs.com/jhxk/articles/1609182.html 由于最近一个项目的数据库变动比较频繁, 为了减少数据层的负担, 打算采用.net的MVC框架, ...

  10. HTTP协议是什么?(及get和post请求的区别)

    http://blog.csdn.net/xiemk2005/article/details/6108618 http://blog.csdn.net/mengleigaocong/article/d ...