hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html
使用广度优先搜索,dp[key][x][y]表示在拥有钥匙key并在坐标(x,y)时需要的最少的步数,key的二进制的第i位等于1则代表拥有第i把钥匙。
需要注意以下几点:
1.可能存在同一坐标有多把钥匙。
2.墙和门是在两个坐标间进行移动时的障碍,并不在坐标点上,因此两个方向的移动都要加入wall数组。
2.可以使用方向数组来进行上下左右的搜索。
3.搜索到坐标(n,m)时记录最小步数并退出搜索。
代码如下:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <queue>
#include <limits.h>
#include <queue>
#define MAXP 11
#define MAXN 51
using namespace std;
int bin[] = {, , , , , , , , , , , , };
int dir[][]={{-, }, {, }, {, }, {, -}};//左,上,右,下
int dp[][MAXN][MAXN];
int key[MAXN][MAXN];
int wall[MAXN][MAXN][MAXN][MAXN];
int n, m, p;
class state
{
public:
int x, y, key, step;
};
void solve()
{
memset(dp, -, sizeof(dp));
state b;
b.x = ;
b.y = ;
b.key = key[][];
b.step = ;
queue<state> qu;
qu.push(b);
int res = INT_MAX;
while( qu.size() > )
{
state cur = qu.front();
qu.pop();
for( int i = ; i < ; i++ )
{
if( cur.x==n && cur.y == m )//到达目的地
{
res = min(res, cur.step);
continue;
}
state next;
next.x = cur.x + dir[i][];
next.y = cur.y + dir[i][];
next.key = ;
next.step = ;
//出界
if( next.x < || next.x > n ) continue;
if( next.y < || next.y > m ) continue;
int w = wall[cur.x][cur.y][next.x][next.y];
if( w == ) continue;//是墙
if( w > && (cur.key/bin[w]% == )) continue;//是门没钥匙
next.step = cur.step+;
next.key = cur.key | key[next.x][next.y];
if( dp[next.key][next.x][next.y]< )
{
dp[next.key][next.x][next.y] = next.step;
qu.push(next);
}
else if(dp[next.key][next.x][next.y] > next.step)
{
dp[next.key][next.x][next.y] = next.step;
qu.push(next);
}
}
}
if( res == INT_MAX )
{
printf("-1\n");
return;
}
printf("%d\n", res);
}
int main(int argc, char *argv[])
{
while(scanf("%d%d%d", &n, &m, &p)!=EOF)
{
memset(key, , sizeof(key));
memset(wall, -, sizeof(wall));
int tmp;
scanf("%d", &tmp);
int x1, x2, y1, y2, type;
for( int i = ; i < tmp ; i++ )
{
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &type);
wall[x1][y1][x2][y2] = type;
wall[x2][y2][x1][y1] = type;
}
scanf("%d", &tmp);
for( int i = ; i < tmp ; i++ )
{
scanf("%d%d%d", &x1, &x2, &type);
key[x1][x2] |= bin[type];
}
solve();
}
}
hdu 5094 Maze 状态压缩dp+广搜的更多相关文章
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- HDU 3001(状态压缩dp)
状态压缩dp的第一题! 题意:Mr ACMer想要进行一次旅行,他决定访问n座城市.Mr ACMer 可以从任意城市出发,必须访问所有的城市至少一次,并且任何一个城市访问的次数不能超过2次.n座城市间 ...
- hdu 4856 Tunnels 状态压缩dp
Tunnels Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU 3001【状态压缩DP】
题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数 ...
- hdu 5045 Contest(状态压缩DP)
题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...
- hdu 3091 Necklace 状态压缩dp *******
Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)Total ...
- hdu 4628 Pieces 状态压缩dp
Pieces Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total S ...
- HDU 2167 Pebbles 状态压缩dp
Pebbles Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
随机推荐
- Spring 从零開始-03
这里说说bean装配集合.spring的支持的集合元素,其基本使用方式如同与Java的集合,所以假设对Java的集合不太了解的能够先找个帖子好好学习一下, 时间关系这里就不说了. ~~ list的样例 ...
- Hive权限介绍
一.开启权限 眼下hive支持简单的权限管理,默认情况下是不开启.这样全部的用户都具有同样的权限.同一时候也是超级管理员.也就对hive中的全部表都有查看和修改的权利,这样是不符合一般数据仓库的安全原 ...
- JS 实现2+2=5的代码 实现原理解析
当我在http://segmentfault.com/q/1010000000531302 看到的时候 先是一惊. 这是代码 g = function () { H = 3 return H ...
- nginx+tomcat动静分离结构
本文采用另一种策略对动静分离进行演示,它的大致结构如图 2 所示. 图 2. 本文设计的动静分离结构 在本文中,我们将静态资源放在 A 主机的一个目录上,将动态程序放在 B 主机上,同时在 A 上安装 ...
- Vim文本编辑器 指令簿(二)
常常处理文本以及常常须要写代码的人,都会有自己比較常常使用的编辑器,本人喜欢用Vim.理由就是Vim编辑器灵活,而且能够达到纯键盘操作,使用纯熟情况下,根本不须要鼠标操作.听起来是不是非常酷的?只是别 ...
- 文件和目录之umask函数
本篇博文内容摘自<UNIX环境高级编程>(第二版),仅作个人学习记录所用.关于本书可参考:http://www.apuebook.com/. umask函数为进程设置文件模式创建屏蔽字,并 ...
- Requirements
Requirements The framework requirements are limited. PHP 5.5 or greater. Apache Web Server or equiva ...
- 通过代码设置button中文字的对齐方式
// button.titleLabel.textAlignment = NSTextAlignmentLeft; 这句无效 button.contentHorizontalAlignment = U ...
- tab选项卡
1 <!doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...
- Linux 的使用基础---Linux的常用命令
自己电脑的安装的软件太多了,如果重装linux系统的话,是在是太麻烦了,本身电脑系统是32位的,硬件上的配置也行,所以就安装了虚拟机,在虚拟机上又安装了一个Linuxd 系统,虽然速度是有些慢,总比浪 ...