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): 1343    Accepted Submission(s): 642

Problem Description

  Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon‘s home. But he can‘t bring his precious with him. As you know, uncle Vernon never allows such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:  Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers‘ properties, so they live in the indestructible rooms and put customers‘ properties in vulnerable rooms. Harry Potter‘s precious are also put in some vulnerable rooms. Dudely wants to steal Harry‘s things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can‘t access the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry‘s precious are. He wants to collect all Harry‘s precious by as less steps as possible. Moving from one room to another adjacent room is called a ‘step‘. Dudely doesn‘t want to get out of the bank before he collects all Harry‘s things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry‘s precious.

Input

  There are several test cases.   In each test cases:   The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).   Then a N×M matrix follows. Each element is a letter standing for a room. ‘#‘ means a indestructible room, ‘.‘ means a vulnerable room, and the only ‘@‘ means the vulnerable room from which Dudely starts to move.   The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter‘s precious in the bank.   In next K lines, each line describes the position of a Harry Potter‘s precious by two integers X and Y, meaning that there is a precious in room (X,Y).   The input ends with N = 0 and M = 0

Output

  For each test case, print the minimum number of steps Dudely must take. If Dudely can‘t get all Harry‘s things, print -1.

Sample Input

2 3 ##@ #.# 1 2 2 4 4 #@## .... #### .... 2 2 1 2 4 0 0

Sample Output

-1 5

Source

2013 Asia Hangzhou Regional Contest

Recommend

We have carefully selected several similar problems for you:  5057 5055 5054 5053 5052

题意和题解转自:http://blog.csdn.net/qq574857122/article/details/14649319

题意:

给定n*m的地图

#为墙 @为起点

下面K个坐标

问:遍历K个给定坐标,需要的最小步数

思路:

因为K 最大只有4

状压 当前是否走过某点

用二进制 的 i 位 0、1表示 第i个点是否走过

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<string>
 10
 11 #define N 105
 12 #define M 15
 13 #define mod 10000007
 14 //#define p 10000007
 15 #define mod2 100000000
 16 #define ll long long
 17 #define LL long long
 18 #define maxi(a,b) (a)>(b)? (a) : (b)
 19 #define mini(a,b) (a)<(b)? (a) : (b)
 20
 21 using namespace std;
 22
 23 int ans;
 24 int n,m;
 25 int k;
 26 char s[N][N];
 27 int a[N][N];
 28 int mi[20][N][N];
 29 int dirx[]={1,0,-1,0};
 30 int diry[]={0,1,0,-1};
 31
 32 typedef struct
 33 {
 34     int x;
 35     int y;
 36     int step;
 37     int st;
 38 }PP;
 39
 40 PP start;
 41
 42 void ini()
 43 {
 44     ans=-1;
 45     int i,j,p;
 46     int x,y;
 47     for(i=0;i<n;i++){
 48         scanf("%s",s[i]);
 49     }
 50     memset(a,0,sizeof(a));
 51     scanf("%d",&k);
 52     for(i=0;i<n;i++){
 53         for(j=0;j<m;j++){
 54             for(p=0;p<(1<<k);p++){
 55                 mi[p][i][j]=1000000000;
 56             }
 57             if(s[i][j]==‘@‘){
 58                 start.x=i;
 59                 start.y=j;
 60                 start.st=0;
 61                 start.step=0;
 62             }
 63             if(s[i][j]==‘#‘){
 64                 a[i][j]=-1;
 65             }
 66         }
 67     }
 68     for(i=0;i<k;i++){
 69         scanf("%d%d",&x,&y);
 70         a[x-1][y-1]=(1<<i);
 71         if(x-1==start.x && y-1==start.y){
 72             start.st+=(1<<i);
 73             mi[start.st][start.x][start.y]=0;
 74         }
 75     }
 76     if(start.st==0){
 77         mi[0][start.x][start.y]=0;
 78     }
 79
 80    // for(i=0;i<n;i++){
 81     //    for(j=0;j<m;j++){
 82     //        printf(" %d",a[i][j]);
 83     //    }printf("\n");
 84    // }
 85 }
 86
 87 int ok(int i,int j)
 88 {
 89     if(i>=0 && i<n && j>=0 && j<m && a[i][j]!=-1){
 90         return 1;
 91     }
 92     return 0;
 93 }
 94
 95 void solve()
 96 {
 97     int i;
 98     PP now,nx;
 99     queue<PP> q;
100     q.push(start);
101     while(q.size()>=1)
102     {
103
104         now=q.front();
105       //  printf(" i=%d j=%d st=%d step=%d\n",now.x,now.y,now.st,now.step);
106         q.pop();
107         if(now.st== ((1<<k)-1) ){
108             ans=now.step;return;
109         }
110
111         for(i=0;i<4;i++){
112             nx=now;
113             nx.step++;
114             nx.x=now.x+dirx[i];
115             nx.y=now.y+diry[i];
116             if(ok(nx.x,nx.y)==0) continue;
117             if( (now.st & a[nx.x][nx.y])==0){
118                 nx.st=(now.st ^ a[nx.x][nx.y]);
119                // printf("  %d %d %d\n",now.st, a[nx.x][nx.y],nx.st);
120                 //q.push(nx);
121               //  mi[nx.st][nx.x][nx.y]=min(nx.step,mi[nx.st][nx.x][nx.y]);
122             }
123             //else{
124                 if( nx.step< mi[nx.st][nx.x][nx.y]){
125                     q.push(nx);
126                     mi[nx.st][nx.x][nx.y]=nx.step;
127                 }
128            // }
129         }
130     }
131 }
132
133
134 void out()
135 {
136     printf("%d\n",ans);
137 }
138
139 int main()
140 {
141     //freopen("data.in","r",stdin);
142     //freopen("data.out","w",stdout);
143     //scanf("%d",&T);
144    // for(int ccnt=1;ccnt<=T;ccnt++)
145    // while(T--)
146     while(scanf("%d%d",&n,&m)!=EOF)
147     {
148         if(n==0 && m==0 ) break;
149         //printf("Case %d: ",ccnt);
150         ini();
151         solve();
152         out();
153     }
154
155     return 0;
156 }
时间: 2024-08-10 02:00:22

HDU 4771 BFS + 状压的相关文章

HDU 4771 BFS&amp;状压 水

n*m矩阵,起点@,从矩阵中拿k个物品的最小代价 水BFS #include "stdio.h" #include "string.h" #include "queue" using namespace std; int b[]={1,2,4,8,16}; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; struct node { int x,y,step,status; }; int n,m,aim,s_x

hdu 2209 bfs+状压

http://acm.hdu.edu.cn/showproblem.php?pid=2209 不知为啥有种直觉,会出状压+搜索的题,刷几道先 简单的BFS,状压表示牌的状态, //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring> #include <algorithm> #include <string> #

HDU 5025 BFS+状压

2014 ACM/ICPC Asia Regional Guangzhou Online N*N矩阵 M个钥匙 K起点,T终点,S点需多花费1点且只需要一次,1-9表示9把钥匙,只有当前有I号钥匙才能拿I+1号钥匙,可以不拿钥匙只从上面走过 4维数组判重,第三维表示钥匙已经拿到第几把,第四维表示已经走过的S的状况,用状压存储 #include "stdio.h" #include "string.h" #include "queue" using

hdu 1429 bfs+状压

题意:这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开始 Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置.Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一 个.魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去.经过若干次的尝试,Ignatius已画出整个地牢的地图.现在请你帮 他计算能否再次成功逃亡.只要在魔王下次视察之前走到出口就算离开地牢

HDU 5040 BFS+状压

2014 ACM/ICPC Asia Regional Beijing Online 对于N*N的矩阵 M起点,T终点 有起始方向分别向北N,东E,南S,西W的摄像头,可以检测的范围为自己+所指方向1格,每1秒顺时针旋转90° 前面有灯或者自己站的地方有灯,移动需要花3秒,或者原地等一秒. BFS优先队列 开3维 hash数组判重,第三维是在该点等待的时间,开到4即可(摄像头转一圈) 对图中的每个点提前处理处会在什么时候被摄像头看到,用2进制压缩存储在MAP数组中 然后常规BFS解决 比赛时候手

HDU 3681 BFS&amp;状压DP&amp;二分

N*M矩阵,从F点出发,走完所有的Y点,每走一格花费1点电量,走到G点时,电量充满,D不可到达,问起始时的最小满电量可以走完所有Y,Y和G一共最多15个 先BFS出所有的F,Y,G之间的最短距离. 然后二分起始电量,对每个电量,做状压DP判断是否可行 #include "stdio.h" #include "string.h" #include "queue" using namespace std; int inf=0x3f3f3f3f; in

hdu 1044(bfs+状压)

非常经典的一类题型 没有多个出口.这里题目没有说清楚 Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4684    Accepted Submission(s): 983 Problem Description It is written in the Book of The Lady: Afte

hdu 4568 bfs + 状压dp

//这题的数据是不是有问题... 不考虑宝藏一个也拿不到也能AC... 1 #include "bits/stdc++.h" 2 using namespace std; 3 const int INF = 0x3f3f3f3f; 4 int T; 5 int N, M; 6 int mat[210][210]; 7 int K; 8 int tot_tra, tra[210][210]; 9 10 //dp parameters 11 int dis_tra_broder[20],

hdu 4771 Stealing Harry Potter&#39;s Precious (BFS+状压)

题意: n*m的迷宫,有一些格能走(“.”),有一些格不能走(“#”).起始点为“@”. 有K个物体.(K<=4),每个物体都是放在“.”上. 问最少花多少步可以取完所有物体. 思路: BFS+状压,看代码. 代码: struct node{ int x,s; node(int _x,int _s){ x=_x, s=_s; } }; int n,m,k,sx,sy; char graph[105][105]; int px[5],py[5]; int dp[105][105][35]; int