题目链接

题意:

先沿着左边的墙从 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. 设计模式(二)The Observer Pattern 观察者模式

    问题引入 生成一个公告板显示当时的天气状况,当天气状况发生改变的时候公告板能够实时的更新. 模式定义 定义对象之间的一对多的依赖.当一个对象改变状态时,它的全部依赖者都会自己主动收到通知并自己主动更新 ...

  2. Linux下生产者与消费者的线程实现

    代码见<现代操作系统> 第3版. 为了显示效果,添加了printf()函数来显示运行效果 #include<stdio.h> #include<pthread.h> ...

  3. 第四篇:R语言数据可视化之折线图、堆积图、堆积面积图

    折线图简介 折线图通常用来对两个连续变量的依存关系进行可视化,其中横轴很多时候是时间轴. 但横轴也不一定是连续型变量,可以是有序的离散型变量. 绘制基本折线图 本例选用如下测试数据集: 绘制方法是首先 ...

  4. [转] boost库的Singleton的实现以及static成员的初始化问题

    http://www.cnblogs.com/kex1n/archive/2011/04/05/2006194.html effectie c++的条款4中提到: (global对象,定义在names ...

  5. 给iOS开发者的GCD用户手册

    Grand Central Dispatch,或者GCD,是一个极其强大的工具.它给你一些底层的组件,像队列和信号量,让你可以通过一些有趣的方式来获得有用的多线程效果.可惜的是,这个基于C的API是一 ...

  6. Volley的基本使用(转)

    Volley是Google在2003年的I/O大会上推出的通信框架,结合了AsyncHttpClient和Universal-Image-Loader的优点——简化了http的使用 + 异步加载图片的 ...

  7. Java基础知识强化之集合框架笔记13:Collection集合存储学生对象并遍历

    1. Collection集合存储学生对象并遍历: 需求:存储自定义对象并遍历Student(name,age) 分析: (1)创建学生类 (2)创建集合对象 (3)创建学生对象 (4)把学生对象添加 ...

  8. Android(java)学习笔记237:多媒体之图形的变化处理

    1.图形的缩放 (1)布局文件activity_main.xml如下: <LinearLayout xmlns:android="http://schemas.android.com/ ...

  9. HDU 4455(dp)

    题意:给定一个序列ai,个数为n.再给出一系列w:对于每个w,求序列中,所有长度为w的连续子串中的权值和,子串权值为子串中不同数的个数. 思路:动态规划,用dp[w]表示当前长度为w的时候的权值和.显 ...

  10. LINQ的基本用法

    1.var q =from c in db.Customers select c.ContactName; 这个语句只是一个声明或者一个描述,并没有真正把数据取出来,只有当你需要该数据的时候,它才会执 ...