题目链接: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】的更多相关文章

  1. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  2. Hdu 2364 Escape

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=2364 这道题的特殊之处在于能转弯时不能直走,必须转弯,所以在行走时,要判断能否转弯,不能转弯 ...

  3. 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 ...

  4. HDU 3533 Escape(大逃亡)

    HDU 3533 Escape(大逃亡) /K (Java/Others)   Problem Description - 题目描述 The students of the HEU are maneu ...

  5. HDU 3605 Escape (网络流,最大流,位运算压缩)

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

  6. 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,最先 ...

  7. ZOJ 649 Rescue(优先队列+bfs)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  8. 【POJ3635】Full Tank 优先队列BFS

    普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...

  9. Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]

    题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...

随机推荐

  1. 点击<a>标签后禁止页面跳至顶部

    一.点击<a>标签后禁止页面跳至顶部 1. 使用 href="javascript:void(0);",例如: <a href="javascript: ...

  2. canvas 保存bitmap到本地

    File f = new File("/sdcard/DCIM/Camera/0.png"); FileOutputStream fos = null; try { fos = n ...

  3. LeetCode(69):x 的平方根

    Easy! 题目描述: 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: ...

  4. laravel 视图

    在实际开发中,除了 API 路由返回指定格式数据对象外,大部分 Web 路由返回的都是视图,以便实现更加复杂的页面交互,我们在前面已经看到过了视图的定义方式: return view('以.分隔的视图 ...

  5. 网络扫描信息收集基于(Windows)

    1.首先说明一下一款网络扫描工具,在之前的博客中我曾简要的写过关于Advance IP Scanner使用方法,最近要写网络扫描的工具,所以对这款工具做一个详细的功能细节上的介绍. 如下图  在输入框 ...

  6. light1341 唯一分解定理

    一定要先打表素数,然后进行分解,直接分解是会t的 #include <cstdio> #include <cstring> #include <algorithm> ...

  7. zoj3659

    #include<iostream> #include<algorithm> #include<cstring> #define ll long long #inc ...

  8. bzoj 1856

    做这题之前先看道高考真题(好像是真题,我记不清了) 例:已知一个由n个0和n个1排列而成的数列,要求对于任意k∈N*且k∈[1,2n],在前k个数中1的个数不少于0的个数,求当n=4时这样的数列的数量 ...

  9. C++ StrCat()

    关于StrCat function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/bb759925(v=vs.85).aspx ...

  10. 即时通讯协议之XMPP

    目前IM即时通信有四种协议 1.即时信息和空间协议(IMPP) 2.空间和即时信息协议(PRIM) 3.针对即时通讯和空间平衡扩充的进程开始协议SIP 4.XMPP协议: 该协议的前身是Jabber, ...