hdu--5093--状压bfs

主要就是开个 三维vis数组 第三维 表示在<x,y>坐标下的 钥匙有哪些

还有要注意的话 就是wall数组  是有3种不同的情况 -1 直接行走  0  墙   >=1 大门 需要钥匙

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5
 6 int n , m;
 7 const int size = 55;
 8 int wall[size][size][size][size];
 9 int dir[4][2] = {1,0,-1,0,0,1,0,-1};
10 int key[size][size];
11 bool vis[size][size][1<<12];
12 struct node
13 {
14     int x , y , key , T;
15     node(){};
16     node( int u , int v , int w , int p ):x(u),y(v),key(w),T(p){};
17 };
18 queue<node> q;
19
20 void init( )
21 {
22     memset( vis , false , sizeof(vis) );
23     memset( wall , -1 , sizeof(wall) );
24     memset( key , 0 , sizeof(key) );
25     while( !q.empty() )
26         q.pop();
27 }
28
29 int bfs( int x1 , int y1 , int x2 , int y2 )
30 {
31     node now;
32     q.push( node(x1,y1,key[x1][y1],0) );
33     vis[x1][y1][ key[x1][y1] ] = true;
34     while( !q.empty() )
35     {
36         now = q.front();
37         q.pop();
38         if( now.x==x2 && now.y==y2 )
39             return now.T;
40         for( int i = 0 ; i<4 ; i++ )
41         {
42             int xx = now.x + dir[i][0];
43             int yy = now.y + dir[i][1];
44             if( xx>=1 && xx<=n && yy>=1 && yy<=m )
45             {
46                 int door = wall[xx][yy][now.x][now.y];
47                 int kk = now.key | key[xx][yy];
48                 if( !vis[xx][yy][kk] && ( door==-1 || ( door>=1 && ( (1<<door)&now.key )!=0 ) ) )
49                 {
50                     vis[xx][yy][kk] = true;
51                     q.push( node(xx,yy,kk,now.T+1) );
52                 }
53             }
54         }
55     }
56     return -1;
57 }
58
59 int main()
60 {
61     int p , k , s , g , q , x1 , x2 , y1 , y2 , ans;
62     while( cin >> n >> m >> p )
63     {
64         cin >> k;
65         init();
66         while( k-- )
67         {
68             cin >> x1 >> y1 >> x2 >> y2 >> g;
69             wall[x1][y1][x2][y2] = wall[x2][y2][x1][y1] = g;
70         }
71         cin >> s;
72         while( s-- )
73         {
74             cin >> x1 >> y1 >> q;
75             key[x1][y1] |= (1<<q);
76         }
77         ans = bfs( 1 , 1 , n , m );
78         cout << ans << endl;
79     }
80     return 0;
81 }

再去做下 上次的 二分图 =_=

时间: 2024-08-27 20:59:23

hdu--5093--状压bfs的相关文章

hdu 1429 状压bfs

#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f3f3f3f #define ll __in

HDU 5094 状压BFS

给出n*m矩阵 给出k个障碍,两坐标之间存在墙或门,门最多10种, 给出s个钥匙位置及编号,相应的钥匙开相应的门 状压BFS即可,注意有可能同一个位置有多个门或者多个钥匙 #include "stdio.h" #include "string.h" #include "queue" using namespace std; int b[]={1,2,4,8,16,32,64,128,256,512,1024,2048}; int dir[4][2

HDU 4771 状压bfs

Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1295    Accepted Submission(s): 618 Problem Description Harry Potter has some precious. For example, his invisibl

hdu 4845 状压bfs(分层思想)

拯救大兵瑞恩 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 255    Accepted Submission(s): 99 Problem Description 1944年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但是幸好麦克得到了迷宫的地形图.

HDU 4012 Paint on a Wall(状压+bfs)

Paint on a Wall Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 830    Accepted Submission(s): 325 Problem Description Annie wants to paint her wall to an expected pattern. The wall can be repr

HDU 4284 状压dp+spfa堆优化

题意: 给定n个点 m条无向边 d元. 下面m行表示每条边 u<=>v 以及花费 w 下面top 下面top行 num c d 表示点标为num的城市 工资为c 健康证价格为d 目标是经过给定的top个城市,当到达该城市时,必须马上购买该城市的健康证并打工赚钱(每个城市只打工1次) 问从1城市出发,最后回到1城市,能否收集到所有的健康证 思路: 由于top很小,所以状压dp dp[i][tmp]表示当前处于i点 经过城市的状态为tmp时 身上最多的钱. 首先对dis数组floyd 跑出最短路,

hdu 4906 状压dp

/* ID: neverchanje PROG: LANG: C++11 */ #include<vector> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<cstdio> #include<set> #include<queue> #includ

HDU 4892 状压dp

[BestCoder Round #5]冠军的奖励是小米3手机一部 恭喜福州大学杨楠获得[BestCoder Round #4]冠军(iPad Mini一部) <BestCoder用户手册>下载 Defence of the Trees Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 224    Accepted Submiss

[GDOI2015]推箱子(状压bfs)

[GDOI2015]推箱子(状压bfs) 题面 题面过长,略 分析 观察到$m \times m =64 \(,那么可以把箱子的01状态压到一个```unsigned long long```里面 然后对于地图上的每一个点\)(x,y)\(,预处理出左上角在\)(x,y)\(,边长为\)m\(的正方形的01状态.如果这个状态和箱子的状态按位与的结果为0,那么就说明箱子可以通过. 然后发现这类似一个分层图上的最短路问题,直接BFS即可.状态\)dist[x][y][k]\(表示箱子左上角在\)(x

HDU 3619 优先队列+状压+bfs

Heroes of Might and Magic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 170    Accepted Submission(s): 74 Problem Description After a very long journey and uncountable number of uphill battles