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 ...
随机推荐
- 跪啃SAM
struct SAM { ],size,last,pre[maxn],pos[maxn]; SAM() { size=; memset(ch[],,])); pre[]=-; } int idx(ch ...
- Linux下汇编语言学习笔记64 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- Linux下汇编语言学习笔记46 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- Codeforces Round #489 (Div. 2) B、C
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
- Gym 100801 J. Journey to the “The World’s Start” DP+单调队列优化+二分
http://codeforces.com/gym/100801 题目大意:有从左到右有n个车站,有n-1种车票,第i种车票一次最多可以坐 i 站(1<=i<=n) 每种票有固定的价钱 ...
- codevs——1006 等差数列
1006 等差数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 给定n(1<=n< ...
- Linux下查看硬盘UUID和修改硬盘UUID(转)
查看硬盘UUID: 1. ls -l /dev/disk/by-uuid 2. blkid /dev/sda5 修改硬盘UUID: 1.新建和改变分区的UUID sudo uuidgen | xarg ...
- 关于SQL命令中不等号(!=,<>)
比较两个表达式(比较运算符).当比较非空表达式时,如果左边操作数的数值不等于右边的操作数,则结果为 TRUE:否则结果为 FALSE.如果两个操作数中有一个或者两个都为 NULL,并且 SET ANS ...
- neutron dhcp
neutron dhcp 采用dnsmasq服务来实现.和传统的 dhcp 一样, dhcp请求也分为4步 The client sends a discover ("I'm a clien ...
- Open Flash Chart IO ERROR Loading test data Error #2032
http://blog.sina.com.cn/s/blog_6754464e0100qfvd.html Open Flash Chart 2 提示Open Flash Chart IO ERROR ...