hdu--1885--状压bfs

这里 我花了将近10分钟找错 才发现是错在 运算符的优先级上面 =_=

1 if( mp[xx][yy]==‘B‘ && ( (now.key&2)==0) )

位运算的优先级 太低了 ....

每次多开一维来表示该点的 钥匙数  很多类似~~

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5
 6 int n , m;
 7 const int size = 110;
 8 int dir[4][2] = {1,0,-1,0,0,1,0,-1};
 9 char mp[size][size];
10 int kk[size][size];
11 bool vis[size][size][1<<6];
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( kk , 0 , sizeof(kk) );
23     memset( vis , false, sizeof(vis) );
24     while( !q.empty() )
25         q.pop();
26 }
27
28 int bfs( int stx , int sty )
29 {
30     q.push( node( stx , sty , kk[stx][sty] , 0 ) );
31     vis[stx][sty][ kk[stx][sty] ] = true;
32     while( !q.empty() )
33     {
34         node now = q.front();
35         q.pop();
36         //cout << "x: " << now.x << " y: " << now.y << " T: " << now.T << endl;
37         if( mp[ now.x ][ now.y ] == ‘X‘ )
38             return now.T;
39         for( int i = 0 ; i<4 ; i++ )
40         {
41             int xx = now.x + dir[i][0];
42             int yy = now.y + dir[i][1];
43             int KEY = now.key | kk[ xx ][ yy ];
44             if( xx>=0 && xx<n && yy>=0 && yy<m && mp[xx][yy]!=‘#‘ && !vis[xx][yy][KEY] )
45             {
46                 if( mp[xx][yy]==‘B‘ && ( (now.key&2)==0) )
47                     continue;
48                 else if( mp[xx][yy]==‘Y‘ && ( (now.key&4)==0) )
49                     continue;
50                 else if( mp[xx][yy]==‘R‘ && ( (now.key&8)==0) )
51                     continue;
52                 else if( mp[xx][yy]==‘G‘ && ( (now.key&16)==0) )
53                     continue;
54                 q.push( node(xx,yy,KEY,now.T+1) );
55                 vis[xx][yy][KEY] = true;
56             }
57         }
58     }
59     return -1;
60 }
61
62 int main()
63 {
64     cin.sync_with_stdio(false);
65     int ans , stx , sty;
66     while( cin >> n >> m &&(n||m) )
67     {
68         init();
69         for( int i = 0 ; i<n ; i++ )
70         {
71             cin >> mp[i];
72         }
73         for( int i = 0 ; i<n ; i++ )
74         {
75             for( int j = 0 ; j<m ; j++ )
76             {
77                 if( mp[i][j]==‘*‘ )
78                 {
79                     stx = i;
80                     sty = j;
81                 }
82                 else if( mp[i][j]==‘b‘ )
83                     kk[i][j] |= (1<<1);
84                 else if( mp[i][j]==‘y‘ )
85                     kk[i][j] |= (1<<2);
86                 else if( mp[i][j]==‘r‘ )
87                     kk[i][j] |= (1<<3);
88                 else if( mp[i][j]==‘g‘ )
89                     kk[i][j] |= (1<<4);
90             }
91         }
92         ans = bfs( stx , sty );
93         if( ans==-1 )
94             cout << "The poor student is trapped!" << endl;
95         else
96             cout << "Escape possible in " << ans << " steps." << endl;
97     }
98     return 0;
99 }

today:

  你必须先要失去什么你视之为珍贵的

  才能得到一些更为有价值即使现在的你看来一文不值的

时间: 2024-11-05 21:57:43

hdu--1885--状压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