HDU - 1728 逃离迷宫 【BFS】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1728
思路
BFS
一开始 从开始位置 往四周走 如果能走的话 这个时候 转弯次数都是0
我们的标记不是简单的标记
我们给每个已经访问过的位置 第一次访问时 我们将此时的转弯次数 赋值给它
当下一次 有其他状态 又来访问的时候,我们判断一下 之前已经标记的转弯次数 是比当前的大于等于吗 如果是 那么这个点 就可以继续走下去 如果不是 目前的这个状态就可以不要 相当于剪枝了
有几个坑点
输入的时候 坐标是从1-N 1-M 的
然后输入起始坐标的时候,, x 代表列 y 代表行
AC代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<list>
#include<stack>
#include <queue>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
int Move[4][2] = // up 0 down 1 left 2 right 3
{
-1, 0, // up
1, 0, // down
0,-1, // left
0, 1, // right
};
int n, m;
int G[maxn][maxn];
int v[maxn][maxn];
int sx, sy, ex, ey, limit;
int ans;
struct node
{
int x, y;
int step;
int dis;
};
bool ok(int x, int y, int step)
{
if (x < 0 || x >= n || y < 0 || y >= m || G[x][y] == 0 || v[x][y] < step)
return false;
return true;
}
void bfs()
{
queue <node> q;
node tmp;
tmp.step = 0;
v[sx][sy] = 1;
for (int i = 0; i < 4; i++)
{
tmp.x = sx + Move[i][0];
tmp.y = sy + Move[i][1];
tmp.dis = i;
if (ok(tmp.x, tmp.y, tmp.step))
{
q.push(tmp);
v[tmp.x][tmp.y] = tmp.step;
}
}
while (!q.empty())
{
node u = q.front(), V;
q.pop();
if (u.x == ex && u.y == ey)
{
if (u.step <= limit)
{
ans = 1;
return;
}
}
if (u.step > limit)
continue;
for (int i = 0; i < 4; i++)
{
V.x = u.x + Move[i][0];
V.y = u.y + Move[i][1];
if (u.dis != i)
V.step = u.step + 1;
else
V.step = u.step;
V.dis = i;
if (ok(V.x, V.y, V.step))
{
q.push(V);
v[V.x][V.y] = V.step;
}
}
}
}
void init()
{
CLR(G, 0);
CLR(v, 0x3f);
}
int main()
{
int t;
cin >> t;
while (t--)
{
init();
scanf("%d%d", &n, &m);
char c;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf(" %c", &c);
if (c == '.')
G[i][j] = 1;
else
G[i][j] = 0;
}
}
scanf("%d%d%d%d%d", &limit, &sy, &sx, &ey, &ex);
sx--, sy--, ex--, ey--;
ans = 0;
bfs();
if (ans)
printf("yes\n");
else
printf("no\n");
}
}
HDU - 1728 逃离迷宫 【BFS】的更多相关文章
- 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 逃离迷宫 BFS加优先队列 DFS()
http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...
- HDU 1728 逃离迷宫 BFS题
题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...
- 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 逃离迷宫
[题目描述 - Problem Description] 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,glo ...
- 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 ...
随机推荐
- 自定义ListView和GridView
1 http://blog.chengyunfeng.com/?p=465 2
- C# 使用UUID生成各种模式方法
UUID简单说明 常见的方式.可以利用数据库也可以利用程序生成,一般来说全球唯一. 优点: 1)简单,代码方便. 2)生成ID性能非常好,基本不会有性能问题. 3)全球唯一,在遇见数据迁移,系统数据合 ...
- Linux下画原理图和PCB
Linux下画原理图和PCB Windows下大名鼎鼎的Allegro和经典的Protel 99SE都是不支持Linux操作系统的.做Linux驱动开发免不了要看一下原理图和PCB. 一般的做法有三种 ...
- kernel feature collection
Fault injection http://lwn.net/Articles/209257/ The framework can cause memory allocation failures a ...
- Maven环境下搭建SSH框架
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Maven:3.3.9 Struts2:2.5.10 Spring:4.3.8.RELEASE Hibernate:5.1.7.Fina ...
- gitlab配置smtp时,总是提示需要鉴权,记录一下爬坑过程。
配置好smtp,然后发送邮件时总是提示 Net::SMTPFatalError: 550 5.7.1 authentication is required 最后发现是因为在gitlab web界面上配 ...
- jar包解压与打包
首先感谢大神的指导:https://blog.csdn.net/mr_pang/article/details/47028921 1.首先准备一个能运行的jar文件,我们使用第三方解压工具进行解压wi ...
- stage3D基础二-----顶点和片段着色器(转)
来源:http://www.adobe.com/cn/devnet/flashplayer/articles/vertex-fragment-shaders.html 本教程将介绍着色器.着色器是 S ...
- find - exec 命令
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是command ...
- MapReduce源码分析之JobSplitWriter
JobSplitWriter被作业客户端用于写分片相关文件,包括分片数据文件job.split和分片元数据信息文件job.splitmetainfo.它有两个静态成员变量,如下: // 分片版本,当前 ...