BFS HDOJ 1728 逃离迷宫
/*
BFS:三维BFS,加上方向。用dp[x][y][d]记录当前需要的最少转向数
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std; const int MAXN = 1e2 + ;
const int INF = 0x3f3f3f3f;
struct P
{
int x, y, z;
}now, to;
char maze[MAXN][MAXN];
int dp[MAXN][MAXN][];
bool inq[MAXN][MAXN][];
int dx[] = {-, , , };
int dy[] = {, , -, };
int k, x1, y1, x2, y2;
int n, m; bool check(int x, int y)
{
if (x >= && x <= n && y >= && y <= m) return true;
else return false;
} bool BFS(void)
{
memset (dp, , sizeof (dp));
memset (inq, false, sizeof (inq));
queue<P> Q;
for (int i=; i<=n; ++i)
{
for (int j=; j<=m; ++j)
{
for (int l=; l<; ++l)
{
dp[i][j][l] = INF; inq[i][j][l] = false;
}
}
}
for (int i=; i<; ++i)
{
dp[x1][y1][i] = ; inq[x1][y1][i] = true;
Q.push ((P) {x1, y1, i});
} while (!Q.empty ())
{
now = Q.front (); Q.pop ();
for (int i=; i<; ++i)
{
to.x = now.x + dx[i];
to.y = now.y + dy[i]; to.z = i;
if (check (to.x, to.y) && maze[to.x][to.y] == '.')
{
int tmp = dp[now.x][now.y][now.z];
if (to.z == now.z)
{
if (tmp < dp[to.x][to.y][to.z])
{
dp[to.x][to.y][to.z] = tmp;
if (!inq[to.x][to.y][to.z])
{
inq[to.x][to.y][to.z] = true; Q.push (to);
}
}
}
else
{
if (++tmp < dp[to.x][to.y][to.z])
{
dp[to.x][to.y][to.z] = tmp;
if (!inq[to.x][to.y][to.z])
{
inq[to.x][to.y][to.z] = true; Q.push (to);
}
}
}
}
}
inq[now.x][now.y][now.z] = false;
} for (int i=; i<; ++i)
{
if (dp[x2][y2][i] <= k) return true;
} return false;
} int main(void) //HDOJ 1728 逃离迷宫
{
// freopen ("HDOJ_1728.in", "r", stdin); int t; scanf ("%d", &t);
while (t--)
{
scanf ("%d%d", &n, &m);
for (int i=; i<=n; ++i) scanf ("%s", maze[i] + );
scanf ("%d%d%d%d%d", &k, &y1, &x1, &y2, &x2);
if (BFS ()) puts ("yes");
else puts ("no");
} return ;
}
BFS HDOJ 1728 逃离迷宫的更多相关文章
- hdoj 1728 逃离迷宫
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 1728 逃离迷宫 bfs记转向
题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Mem ...
- hdu 1728 逃离迷宫 bfs记步数
题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Mem ...
- hdu 1728 逃离迷宫 (BFS)
逃离迷宫 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- HDU 1728 逃离迷宫(DFS||BFS)
逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...
- HDU 1728 逃离迷宫(DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) ...
- hdu 1728:逃离迷宫(DFS,剪枝)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 1728 逃离迷宫 [ dfs ]
传送门 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- cdq分治入门--BZOJ3262: 陌上花开
n<=100000个人,每个人三个属性Ai,Bi,Ci,一个人i的等级为Ai>=Aj,Bi>=Bj,Ci>=Cj的人数,求每个等级有多少人. 裸的三维偏序.按照常规思路,一维排 ...
- hdu - 1627 Krypton Factor (dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1627 给定 n 和 L 找出第n个范围在0-L之内的字符串,字符串要求没有相邻的子串是相同的. 按照格式输出. ...
- cogs——619. [金陵中学2007] 传话
619. [金陵中学2007] 传话 ★★ 输入文件:messagez.in 输出文件:messagez.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 兴趣小 ...
- JSP国际化设置
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/internationalization.html: 国际化(i18n):这意味着可以使网站根据访问者的语 ...
- 使用Hexo搭建博客
好长时间没在博客园写东西了,自己搭了一套博客,把自己的一些积累分享给大家,欢迎指正! 博客地址:http://www.baiguangnan.com 使用Hexo搭建自己的博客,可以参考这里:http ...
- Bundle格式文件的安装
安装VMware Workstation for Linux,文件是Bundle格式,安裝如下: 1 su要先取得root權限2hmod +x VMware-Workstation-Full-7.1. ...
- matlab Newton method
% Matlab script to illustrate Newton's method % to solve a nonlinear equation % this particular scri ...
- linux下常用快捷方式
一.终端最常用的快捷键: 1.新建终端窗口:crtl+shift+n 2.终端的切换:shift+左右箭头 3.挂起:crtl+s 4.解除挂起:crtl+q 5.清屏:crtl+l 二.命令行光标移 ...
- vs2013生成lib
引擎cocos2d-x-3.1.1 一. cocos创建一个项目.随便是lua还是cpp.这里用cpp演示 二.创建完毕之后执行下项目 之后创建两个类.例如以下 TestLib.cpp文件 #incl ...
- leetcode笔记:Search in Rotated Sorted Array
一.题目描写叙述 二.解题技巧 因为这道题出现了旋转的情况,即比第一个元素小的元素可能出如今数值的后半段或者不出现. 因此.能够考虑採用变种的二分查找,即在比較中间元素与目标之前,先比較第一个元素与目 ...