Problem Description
  给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?
 
Input
  第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,
  第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m),其中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,其中x1,x2对应列,y1, y2对应行。
 
Output
  每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。
 
题目大意:最小转弯问题。(注意哪些是行哪些是列)
思路:一种O(12nm)的算法,用dis[i][x][y]代表上一步走的是i方向,现处于(x, y)所需要的最小转弯数。用两个队列BFS,按转弯数小到大BFS。pre队列记录当前用来拓展的结点,step代表目前的转弯数,同方向的拓展后放到pre队列后面,转弯的拓展后放到cur队列。当pre队列为空之后,交换cur和pre队列,step自增1。若step>k结束循环。
PS:连连看作业的算法有着落啦~~
 
代码(15MS):
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ; struct Node {
int f, x, y;
Node() {}
Node(int f, int x, int y):
f(f), x(x), y(y) {}
}; int fx[] = {-, , , };
int fy[] = {, , , -}; int dis[][MAXN][MAXN];
char mat[MAXN][MAXN];
int T, n, m;
int k, x1, y1, x2, y2; bool solve() {
memset(dis, 0x3f, sizeof(dis));
queue<Node>* pre = new queue<Node>();
queue<Node>* cur = new queue<Node>();
int step = ;
for(int i = ; i < ; ++i) pre->push(Node(i, x1, y1));
for(int i = ; i < ; ++i) dis[i][x1][y1] = ;
while(step <= k) {
Node t = pre->front(); pre->pop();
if(dis[t.f][t.x][t.y] != step) continue;
if(t.x == x2 && t.y == y2) return true;
for(int i = ; i < ; ++i) {
if((t.f + ) % == i) continue;
int x = t.x + fx[i], y = t.y + fy[i], d = dis[t.f][t.x][t.y] + (t.f != i);
if(mat[x][y] == '.' && d < dis[i][x][y]) {
dis[i][x][y] = d;
if(t.f == i) pre->push(Node(i, x, y));
else cur->push(Node(i, x, y));
}
}
if(pre->empty()) {
step++;
if(cur->empty()) break;
swap(pre, cur);
}
}
return false;
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
memset(mat, , sizeof(mat));
for(int i = ; i <= n; ++i) scanf("%s", &mat[i][]);
scanf("%d%d%d%d%d", &k, &y1, &x1, &y2, &x2);
puts(solve() ? "yes" : "no");
}
}

HDU 1728 逃离迷宫(BFS)的更多相关文章

  1. hdu 1728 逃离迷宫 bfs记转向

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  2. hdu 1728 逃离迷宫 bfs记步数

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  3. hdu 1728 逃离迷宫 (BFS)

    逃离迷宫 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  4. hdu 1728 逃离迷宫 BFS加优先队列 DFS()

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...

  5. HDU 1728 逃离迷宫 BFS题

    题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...

  6. HDU 1728 逃离迷宫(DFS||BFS)

    逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...

  7. HDU 1728 逃离迷宫(DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)  ...

  8. HDU 1728 逃离迷宫

    [题目描述 - Problem Description] 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,glo ...

  9. HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  10. hdu 1728:逃离迷宫(DFS,剪枝)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. terminal(终端),shell,tty,console(控制台)区别

    原文地址  stackexchange:What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'con ...

  2. 蓝牙Bluetooth技术手册规范下载

    [背景] 之前就已经整理和转帖了和蓝牙技术相关的一些内容: [资源下载]bluetooth 协议 spec specification 蓝牙1.1.蓝牙1.2.蓝牙2.0(蓝牙2.0+EDR)区别 但 ...

  3. HBase协处理器统计表数据量

    1.Java代码实现 import org.apache.hadoop.hbase.client.coprocessor.AggregationClient; import org.apache.ha ...

  4. Github 与Git pages

    基础git命令 设置username,email $ git config --global user.name "your name" $ git config --global ...

  5. [LeetCode]题解(python):082 - Remove Duplicates from Sorted List II

    题目来源 https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ Given a sorted linked list ...

  6. LightOj1007 - Mathematically Hard(欧拉函数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1007 题意:给你两个数a和b,求他们之间所有数的欧拉值得平方和; a and b (2 ...

  7. MoSCoW Method

    When managing a project, it is important to develop a clear understanding of the customers' requirem ...

  8. 关于讯飞 使用android SDK出现21001错误码的分析

    21001,没有安装语音组件1.有没有使用SpeechUtility.createUtility()设置appid2.有没有将libmsc.so放到工程中,jar包有Msc.jar.Sunflower ...

  9. 【转】Android各种Adapter的用法

    转自:http://my.oschina.net/u/658933/blog/372151 Android各种Adapter的用法 发表于5个月前(2015-01-27 10:56)   阅读(143 ...

  10. imx6dl i2c4 support

    imx6dl i2c4 support 最近的项目用到了imx6dl的i2c4,其实完全可以用gpio-i2c的方法来实现.既然imx6的datasheet中提到有4个i2c,那么一定可以生成i2c的 ...