拯救大兵瑞恩

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 511    Accepted Submission(s): 184

Problem Description

   1944年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩。瑞恩被关押在一个迷宫里,迷宫地形复杂,但是幸好麦克得到了迷宫的地形图。
   迷宫的外形是一个长方形,其在南北方向被划分为N行,在东西方向被划分为M列,于是整个迷宫被划分为N*M个单元。我们用一个有序数对(单元的行号,单元的列号)来表示单元位置。南北或东西方向相邻的两个单元之间可以互通,或者存在一扇锁着的门,又或者存在一堵不可逾越的墙。迷宫中有一些单元存放着钥匙,并且所有的门被分为P类,打开同一类的门的钥匙相同,打开不同类的门的钥匙不同。
   大兵瑞恩被关押在迷宫的东南角,即(N,M)单元里,并已经昏迷。迷宫只有一个入口,在西北角,也就是说,麦克可以直接进入(1,1)单元。另外,麦克从一个单元移动到另一个相邻单元的时间为1,拿取所在单元的钥匙的时间以及用钥匙开门的时间忽略不计。
   你的任务是帮助麦克以最快的方式抵达瑞恩所在单元,营救大兵瑞恩。
 

Input

有多组数据对于每一组数据来说:
第一行是三个整数,依次表示N,M,P的值;
第二行是一个整数K,表示迷宫中门和墙的总个数;
第I+2行(1<=I<=K),有5个整数,依次为Xi1,Yi1,Xi2,Yi2,Gi:
当Gi>=1时,表示(Xi1,Yi1)单元与(Xi2,Yi2)单元之间有一扇第Gi类的门,当Gi=0时,表示(Xi1,Yi1)单元与(Xi2,Yi2)单元之间有一堵不可逾越的墙;
(其中,|Xi1-Xi2|+|Yi1-Yi2|=1,0<=Gi<=P)
第K+3行是一个整数S,表示迷宫中存放的钥匙总数;
第K+3+J行(1<=J<=S),有3个整数,依次为Xi1,Yi1,Qi:表示第J把钥匙存放在(Xi1,Yi1)单元里,并且第J把钥匙是用来开启第Qi类门的。(其中1<=Qi<=P)
注意:输入数据中同一行各相邻整数之间用一个空格分隔。

参数设定:
3<=N,M<=15;
1<=P<=10;

 

Output

对于每一组数据,输出一行,只包含一个整数T,表示麦克营救到大兵瑞恩的最短时间的值,若不存在可行的营救方案则输出-1。
 

Sample Input

4 4 9
9
1 2 1 3 2
1 2 2 2 0
2 1 2 2 0
2 1 3 1 0
2 3 3 3 0
2 4 3 4 1
3 2 3 3 0
3 3 4 3 0
4 3 4 4 0
2
2 1 2
4 2 1
 

Sample Output

14
 

Source

 
一个格子可以有多把钥匙。
 //2017-08-18
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue> using namespace std; const int N = ;
const int P = ;
const int inf = 0x3f3f3f3f;
struct Node{
int x, y, step, state;
void setNode(int a, int b, int c, int d){
x = a; y = b; step = c; state = d;
}
};
//vis[x][y][state]记录该状态是否走过,edge[x1*100+y1][x2*100+y2]记录格子(x1,y1)和(x2,y2)之间关系(门、墙、路),grid[x][y]记录(x,y)格子的钥匙,因为钥匙可以有多把,所以压缩成二进制。
int vis[N][N][ << P], n, m, p, k, s, edge[][], grid[N][N];
int dx[] = {, , , -};
int dy[] = {, , -, }; void bfs(){
queue<Node> q;
Node tmp;
int state = ;
if(grid[][])state = state|grid[][];
tmp.setNode(, , , state);
q.push(tmp);
vis[][][] = ;
while(!q.empty()){
int x = q.front().x;
int y = q.front().y;
int step = q.front().step;
int state = q.front().state;
q.pop();
for (int i = ; i < ; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < || nx > n || ny < || ny > m || vis[nx][ny][state])
continue;
if (edge[nx * + ny][x * + y] == -)//遇到墙不可走
continue;
if (edge[nx * + ny][x * + y] == || (( << (edge[nx * + ny][x * + y] - )) & state) > )//路或有钥匙可走
{
if(nx == n && ny == m){
printf("%d\n", step+);
return;
}
if (grid[nx][ny])//拿钥匙
{
tmp.setNode(nx, ny, step+, state|grid[nx][ny]);
vis[nx][ny][state|grid[nx][ny]] = ;
}
else
{
tmp.setNode(nx, ny, step+, state);
vis[nx][ny][state] = ;
}
q.push(tmp);
}
}
}
printf("-1\n");
} int main()
{
//freopen("inputB.txt", "r", stdin);
while (scanf("%d%d%d%d", &n, &m, &p, &k) != EOF)
{
memset(grid, , sizeof(grid));
memset(edge, , sizeof(edge));
memset(vis, , sizeof(vis));
int x1, x2, y1, y2, g;
for (int i = ; i < k; i++)
{
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &g);
if (g == )g = -;
edge[x1 * + y1][x2 * + y2] = edge[x2 * + y2][x1 * + y1] = g;
}
scanf("%d", &s);
for (int i = ; i < s; i++)
{
scanf("%d%d%d", &x1, &y1, &g);
grid[x1][y1] |= (<<(g-));
}
bfs();
}
return ;
}

HDU4845(SummerTrainingDay02-C 状态压缩bfs)的更多相关文章

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

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

  2. hdu4845 状态压缩BFS

    题意:      给一个n*m的矩阵,从11,走到nm,格子和格子之间可能有墙,也可能有门,有的格子上面有钥匙,相应的钥匙开相应的们,捡钥匙和开门都不需要时间,问你最少多少部能走到nm. 思路:   ...

  3. hdu 3681 Prison Break(状态压缩+bfs)

    Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...

  4. 【HDU - 1429】胜利大逃亡(续) (高级搜索)【状态压缩+BFS】

    Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开 ...

  5. POJ 1753 Flip Game (状态压缩 bfs+位运算)

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

  6. HDU 5025 Saving Tang Monk 【状态压缩BFS】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Time Limit: 2000/1000 MS (Java/O ...

  7. POJ - 1324 Holedox Moving (状态压缩+BFS/A*)

    题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...

  8. POJ 3411 Paid Roads (状态压缩+BFS)

    题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...

  9. 「hdu 4845 」拯救大兵瑞恩 [CTSC 1999](状态压缩bfs & 分层图思想)

    首先关于分层图思想详见2004的这个论文 https://wenku.baidu.com/view/dc57f205cc175527072208ad.html 这道题可以用状态压缩,我们对于每一把钥匙 ...

  10. [HNOI2006]最短母串问题(AC自动机+状态压缩+bfs)

    快要THUSC了,来水几道模板题吧. 这题其实是AC自动机模板.看到长度最短,首先就想到AC自动机.那么就直接暴力法来吧,把每个串建立在AC自动机上,建立fail指针,然后由于n<=12,可以把 ...

随机推荐

  1. 反射 方法和函数 type

    1. isinstance/issubclass/type  *** issubclass 判断xxx类是否是xxx类的子类 class Animal: pass class Cat(Animal): ...

  2. <转>php中heredoc与nowdoc的使用方法

    http://www.361way.com/php-heredoc-nowdoc/3008.html 一.heredoc结构及用法 Heredoc 结构就象是没有使用双引号的双引号字符串,这就是说在 ...

  3. 尝试利用slmail的漏洞来getshell

    作者:Joe   本文属于Arctic shell原创内容计划文章,转载请注明原文地址! 二进制,计算机才可以理解的低级语言,简单来说它是一种信号,用电信号为例,0就是断电,而1就是有电,这样子010 ...

  4. Swift5 语言指南(二十) 类型转换

    类型转换是一种检查实例类型的方法,或者将该实例视为与其自己的类层次结构中的其他位置不同的超类或子类. Swift中的类型转换是使用is和as运算符实现的.这两个运算符提供了一种简单而富有表现力的方法来 ...

  5. idea 安装mybatis plugin (mybatis插件)

    注意:可以用免费版本的,就是下面没有 被红框圈中的 Free Mybatis Plugin 安装上以后需要破解,先找到下面的文件 打开文件,设置其中的key 和 value : 这里面的key 和 v ...

  6. 传染病传播模型(SIS)Matlab代码

    function spreadingability=sir(A,beta,mu) for i=1:length(A) for N=1:50%随机次数 InitialState=zeros(length ...

  7. 8 Ways to Become a Better Coder

    It’s time to get serious about improving your programming skills. Let’s do it! That’s an easy career ...

  8. C# 获取所有对象的字符串表示一ToString方法

    应用程序开发过程中经常需要获取对象的字符串表示.Object类中定义了一个ToString的虚方法.所以在任何类型的实例上都能调用该方法. C#中几乎所有的类型都派生自Object,所以如果当前类型没 ...

  9. logstash笔记(一)——redis&es

    下载地址: https://www.elastic.co/downloads 版本:logstash-2.2.2 两台linux虚拟机,一台windows宿主机 shipper: 192.168.22 ...

  10. Eclipse安装ModelGoon控件(ModelGoon控件反向生成UML)

    Eclipse安装ModelGoon 1 下载ModelGoon到本地,放在eclipse的安装目录下 2 打开Eclipse,点击Help,选择Install new software 3 点击ad ...