题目链接:https://vjudge.net/contest/84620#problem/K

题目大意:一个人从起点走到终点,问他是否能够在规定的时间走到,在走向终点的路线上,可能会有一些障碍门,他需要在路上捡到开门的对应钥匙,才能通过这扇门。如果他到达终点的时间超过了规定时间,或者他根本就走不到终点,输出-1,否则的话,输出他走的步数。

解题思路:此题与普通的bfs类似,只不过到下一个点的时候,要加上一些判断,如果是钥匙,则将它储存起来,如果是门,则判断是否已经捡到了对应的钥匙,如果有对应钥匙,则这个门与其它 '.' 无异,如果没有对应的钥匙,则不能通过。这个功能是由状态压缩来实现的,将钥匙的对应序号转化成二进制,方便钥匙的存储和判断。另外,还有一个需要注意的就是vis数组的设置,此题只要仔细思考就会发现,设置成二维的显然不行,因为走过的路是可以重复走的,只不过你钥匙的数量要增加,比如说,你走到了一扇门那里,但是你没有钥匙,你只好向别的方向继续搜索,当你捡到对应的钥匙后,可以沿着原路返回到那扇门那里。因此,为了满足题目意思,又不需要走太多重复的路,这样的设置还是挺合理的。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int vis[][][]; <<=
char map[][];
int dir[][] = {,,,,-,,,-};
int n, m,t; struct node
{
int x;
int y;
int key;
int step;
}; queue<node>q; int bfs()
{
node s,now, next;
int i, j,nx,ny;
while (!q.empty())
{
now = q.front();
q.pop();
if (map[now.x][now.y] == '^')
{
/*now.step < t ?printf("%d\n", now.step) : printf("-1\n"); */ //不能这样写,因为它可能走不到终点
if (now.step < t)return now.step;
}
for (i = ; i < ; i++)
{
nx = now.x + dir[i][];
ny = now.y + dir[i][];
if (nx < || nx >= n || ny < || ny >= m || map[nx][ny] == '*')continue;
else if ((map[nx][ny] == '.'||map[nx][ny]=='^') && !vis[nx][ny][now.key])
{
vis[nx][ny][now.key] = ;
next.x = nx, next.y = ny;
next.step = now.step + , next.key = now.key;
q.push(next);
}
else if (map[nx][ny] >= 'A'&&map[nx][ny] <= 'J'&&vis[nx][ny][now.key] == )
{
int key = << (map[nx][ny] - 'A');
if (now.key&key)
{
vis[nx][ny][now.key] = ;
next.x = nx, next.y = ny;
next.step = now.step + ;
next.key = now.key;
q.push(next);
}
}
else if (map[nx][ny] >= 'a'&&map[nx][ny] <= 'j' && !vis[nx][ny][now.key])
{
int key = << (map[nx][ny] -'a');
vis[nx][ny][now.key] = ;
next.x = nx, next.y = ny;
next.step = now.step + ;
next.key = now.key | key;
q.push(next);
}
}
}
return -;
} int main()
{
int i, j;
node s;
while (scanf("%d%d%d", &n, &m, &t) != EOF)
{
while (!q.empty())q.pop();
memset(vis, , sizeof(vis));
for (i = ; i < n; i++)
{
scanf("%s", &map[i]);
for (j = ; j < m; j++)
{
if (map[i][j] == '@')
{
s.x = i;
s.y = j;
map[i][j] = '.';
}
}
}
s.key = ; s.step = ;
vis[s.x][s.y][s.key] = ;
q.push(s);
printf("%d\n", bfs());
}
return ;
}
2018-03-26

hdu1429 胜利大逃亡(续) 【BFS】+【状态压缩】的更多相关文章

  1. HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 思路分析:题目要求找出最短的逃亡路径,但是与一般的问题不同,该问题增加了门与钥匙约束条件: 考虑 ...

  2. hdu - 1429 胜利大逃亡(续) (bfs状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态 ...

  3. hdu 1429 胜利大逃亡(续) (bfs+状态压缩)

    又开始刷题了 题意:略过. 分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙.状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,k ...

  4. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  5. hdu 1429 胜利大逃亡(续)(bfs+位压缩)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  6. hdu.1429.胜利大逃亡(续)(bfs + 0101011110)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. Hdu1429 胜利大逃亡(续) 2017-01-20 18:33 53人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  8. hdu_1429_胜利大逃亡(续)(BFS状压)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 题意:迷宫的加强版,迷宫里有钥匙和门,问在指定的时间下能否逃出 题解:用二进制位来记录是否有该门 ...

  9. 胜利大逃亡(续)(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1429 #include <stdio.h> #include <queue> #incl ...

随机推荐

  1. MySQL数据库驱动jar包

    地址:https://www.mysql.com/

  2. 20165314 学习基础和C语言基础调查

    技能学习心得 你有什么技能比大多人(超过90%以上)更好?针对这个技能的获取你有什么成功的经验?与老师博客中的学习经验有什么共通之处? 从小我的父母就逼着我学习很多技能,比如钢琴,围棋,书法等,不过很 ...

  3. Linux基础三:linux目录结构和目录文件的浏览、管理及维护

    目录文件的浏览.管理及维护(一) 1.Linux文件系统的层次结构 1)Linux文件系统的树状结构:在Linux或UNIX操作系统中,所有的文件和目录都被组织成一个以根节点开始的倒置的树状结构. 2 ...

  4. Java 输入一组数字,用穷举的方法列出

    import java.util.Scanner; public class TestScanner { public static void main(String[] args) { Scanne ...

  5. centos--git搭建之Gogs安装

    1.下载git yum intall -y git 2. 创建git用户(必须新创建git用户, 用root用户会导致无法下载) #创建git用户 sudo adduser git #给git用户设置 ...

  6. 插件使用一颜色选择器---cxColor

    cxColor 是一款颜色选择器.这样的插件使用场景不多.可喜的这是国人写的. 官方网站: https://github.com/ciaoca/cxColor 使用方法: 1.引入jquery库 1 ...

  7. VUE,页面跳转传多个参数

    <template> <a @click="goNext">Next</a> <input type="text" v ...

  8. GoogleTest入门

    Googletest入门 来源:https://github.com/google/googletest/blob/master/googletest/docs/primer.md P.S. gmoc ...

  9. 有道词典Linux版下载安装

    http://cidian.youdao.com/index-linux.html Ubuntu http://codown.youdao.com/cidian/linux/youdao-dict_1 ...

  10. Asp.net MVC - 使用PRG模式(附源码)

    阅读目录: 一. 传统的Asp.net页面问题 二.Asp.net MVC中也存在同样的问题 三.使用PRG模式 四.PRG模式在MVC上的实现 一. 传统的Asp.net页面问题 一个传统的Asp. ...