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 ...
随机推荐
- Oracle case when then else end的两种用法
查询表结构 SELECT T.COLUMN_ID, T.COLUMN_NAME, (CASE WHEN (T.DATA_TYPE = 'VARCHAR2' OR T.DATA_TYPE = 'RAW' ...
- JavaScript匿名函数与托付
<1> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- C#匿名函数--& ...
- SQL 怎样 远程备份数据库到本地
SQL 怎样 远程备份数据库到本地 --1.启用xp_cmdshell USE master EXEC sp_configure 'show advanced options', 1 RECONFIG ...
- 捕获网络数据包并进行分析的开源库-WinPcap
什么是WinPcap WinPcap是一个基于Win32平台的,用于捕获网络数据包并进行分析的开源库. 大多数网络应用程序通过被广泛使用的操作系统元件来访问网络,比如sockets. 这是一种简单的 ...
- Atitit.跨平台预定义函数 魔术方法 魔术函数 钩子函数 api兼容性草案 v2 q216 java c# php js.docx
Atitit.跨平台预定义函数 魔术方法 魔术函数 钩子函数 api兼容性草案 v2 q216 java c# php js.docx 1.1. 预定义函数 魔术方法 魔术函数是什么1 1.2. & ...
- centos7 中文输入法设置
安装centos7 后,他有自带的中文输入法安装包找到 applications->systemTools->settings->region&language 2:在 in ...
- Http调试工具-Fiddler使用指引
转自:http://my.oschina.net/u/1388024/blog/186886#OSC_h1_9 目录[-] Fiddler是什么? Fiddler能做什么? 从哪里下载? 安装: 初次 ...
- 对LCD接口的认识
LCD接口类型: 1.首先我们以传递的信号类型来区分主要有两大类:- 模拟信号: - VGA: Video Graphics Array- 数字信号 - TTL: Transistor Transis ...
- 【ubantu】Ubuntu的一些常用命令
创建文件: touch a.txt 创建文件夹: mkdir NewFolder 删除文件: rm a.txt 删除文件夹: rmdir NewFolder 删除带有文件的文件夹: rm -rf Ne ...
- 【ubantu】在Ubuntu上安装tar.gz,tar.bz以及deb文件(例:libreoffice安装)
参考文章: https://blog.csdn.net/zhuquan945/article/details/52986712 ==================================== ...