hdu 2364 Escape【模拟优先队列】【bfs】
题目链接:https://vjudge.net/contest/184966#problem/A
题目大意:
走迷宫。从某个方向进入某点,优先走左或是右。如果左右都走不通,再考虑向前。绝对不能往后走,即使没走过。
解题思路:
重点是定义vis数组,每个点的四个方向都走过,这个点才算vis完了。……不过我还是没想明白,为什么能够想到要这样定义vis数组,应该题目做多了就知道了吧。
其它要注意的点就是如何实现优先左右转弯的功能。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define N 85
int map[N][N], mmin, flag, n, m, vis[N][N][];
char str[N][N];
typedef struct node {
int x, y;
int father;//记录方向
int step;//记录步数
}node; int bfs(int x, int y)
{
int k[][] = { ,,-,,,-,, };
int nx, ny, xi, yi, x1, y1, x2, y2;
node nextq, nowq;
memset(vis, , sizeof(vis));//初始化标记数组
queue<node>Q;
flag = ;//标记是否能够成功到达
nowq.father = -;//起点的方向是-1,区别其它点
nowq.x = x;
nowq.y = y;
nowq.step = ;//初始化起点
vis[x][y][] = , vis[x][y][] = ;//起点标记为已经走过
vis[x][y][] = , vis[x][y][] = ;
Q.push(nowq);//将起点加入队列
while (!Q.empty())
{
nowq = Q.front();
Q.pop();
if (nowq.x == || nowq.y == || nowq.x == n - || nowq.y == m - )//到达出口,因为map的边界上只有一个为'.',且该点为出口
{
if (str[nowq.x][nowq.y] != '#')
flag = ;//能够成功到达
return nowq.step;
}
for (int i = ; i < ; i++)
{
nx = nowq.x + k[i][];
ny = nowq.y + k[i][];
if (nx < || ny < || nx > n - || ny > m - || str[nx][ny] == '#' || vis[nx][ny][i])
continue; //不能越界,不能是墙,不能已经是访问过的点
nextq.x = nx;
nextq.y = ny;
nextq.father = i;//记录父点到当前点的方向
nextq.step = nowq.step + ;//步数加1
if (nowq.father % == i % )
{
if (nowq.father == i)
{
x1 = nowq.x + k[(i + ) % ][];
y1 = nowq.y + k[(i + ) % ][];
x2 = nowq.x + k[(i - + ) % ][];
y2 = nowq.y + k[(i - + ) % ][];
if (str[x1][y1] != '.'&&str[x2][y2] != '.')//如果当前位置左右两边都不能走即只能直走
{
vis[nx][ny][i] = ;//标记该方向为已经访问过
Q.push(nextq);
}
}
}
else
{
vis[nx][ny][i] = ;
Q.push(nextq);
}
}
}
return -;
}
int main()
{
int t, i, j, x, y;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
memset(str, , sizeof(str));
for (i = ; i < n; i++)
{
scanf("%s", str[i]);
for (j = ; j < m; j++)
{
if (str[i][j] == '@')//找到起点,存入x,y.
{
x = i;
y = j;
}
}
}
mmin = bfs(x, y);//返回步数
if (flag)//如果能到达出口。输出步数
printf("%d\n", mmin);
else
printf("-1\n"); //否则输出-1 }
return ;
}
2018-03-25
hdu 2364 Escape【模拟优先队列】【bfs】的更多相关文章
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- Hdu 2364 Escape
Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=2364 这道题的特殊之处在于能转弯时不能直走,必须转弯,所以在行走时,要判断能否转弯,不能转弯 ...
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 3533 Escape(大逃亡)
HDU 3533 Escape(大逃亡) /K (Java/Others) Problem Description - 题目描述 The students of the HEU are maneu ...
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 【POJ3635】Full Tank 优先队列BFS
普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...
- Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]
题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...
随机推荐
- js数组的实例方法sort() 排序方法的运用,不再只是.sort()
1, sort() 不传回调函数的话,默认按照字母顺序(字符编码)的顺序进行排序. 2, sort() 通过传回调函数来控制从小到大的排序还是从大到小的排序: var arr = [1,23,5,6, ...
- nginx(一)之默认配置文件
首先是nginx.conf vim /etc/nginx/nginx.conf user nginx; // 设置nginx服务的系统使用用户 worker_processes 1; // 工作进程数 ...
- Socket网络编程(三)
TCP协议网络通讯案例(http协议) 1.创建TcpServer(tcp服务端) package com.cppdy.tcp; import java.io.IOException; import ...
- 微信小程序简单介绍 一
一 组件及api网址: 组件 :https://developers.weixin.qq.com/miniprogram/dev/component/view.html api:https://dev ...
- 【gearman】gearmand -d 无反应解决
背景:安装了gearman后,用指令gearmand -d启动后.输入ps -ef|grep gearmand 查找不到.说明服务并没有启动. 查看报错: gearmand -d -l gear.lo ...
- 基于concurrent.futures的进程池 和线程池
concurrent.futures:是关于进程池 和 线程池 的 官方文档 https://docs.python.org/dev/library/concurrent.futures.html 现 ...
- 论文阅读笔记三十二:YOLOv3: An Incremental Improvement
论文源址:https://pjreddie.com/media/files/papers/YOLOv3.pdf 代码:https://github.com/qqwweee/keras-yolo3 摘要 ...
- .net C# 抽奖,中奖
demo设置了8个奖项,每个奖项可以自定义中奖率,精度为1/10000 public string PrizeDraw() { //奖品以及中奖率 const string prizeString = ...
- 正则re模块
正则表达式的特殊字符: 语法: re.match(正则语法,字符串) # re.match() 为关键字 group(1) # 取出第一个匹配 括号中的值,1位第一个括号内的值 1. 特殊字符 1 . ...
- 关于The specified Android SDK Build Tools version (26.0.2) is ignored, as it is below the minimum...
今天将项目迁移到另一台笔记本,进行build出现以下问题,导致build失败 The specified Android SDK Build Tools version (26.0.2) is ign ...