HDUOJ 4771 Stealing Harry Potter's Precious

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4771

题意:给定一张地图和地图上若干宝藏的坐标,地图上一些地方是不能走的,我们从给定点出发,问拿到所有宝藏的最少步数是多少。

思路:类似的题目专题做过,考虑到宝藏的数量很少,我们直接状压表示拿到宝藏的数量,用vis[x][y][key]记录走过的路程,BFS即可。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
struct Node{
    int x,y,key,step;
 };
 int n,m,s,t,num,has[105][105];
 int dir_x[]={0,1,0,-1};
 int dir_y[]={1,0,-1,0};
 char maze[105][105];
 bool vis[105][105][(1<<4)+5];
int main(){
    while (~scanf("%d%d",&n,&m) && n+m){
        for (int i=1;i<=n;i++){
            scanf("%s",maze[i]+1);
            for (int j=1;j<=m;j++){
                if (maze[i][j]=='@'){
                    s=i;
                    t=j;
                }
            }
        }
        bool flag=false;
        memset(has,0,sizeof(has));
        read(num);
        for (int i=1;i<=num;i++){
            int x,y;read(x),read(y);
            if (maze[x][y]=='#') flag=true;
            else has[x][y]=i;
        }
        if (flag){
            puts("-1");
        }
        else{
             queue<Node>Q;
             memset(vis,false,sizeof(vis));
             int tmp=0;
             if (has[s][t]){
                tmp=1<<(has[s][t]-1);
             }
             Q.push((Node){s,t,tmp,0});
             vis[s][t][tmp]=true;
             int ans=-1;
             while (!Q.empty()){
                Node cur=Q.front();Q.pop();
                int tmp=cur.key,cnt=0;
                for (int i=1;i<=num;i++){
                    if (!(tmp&(1<<(i-1)))){
                        cnt++;
                        break;
                    }
                }
                if (!cnt){
                    ans=cur.step;
                    break;
                }
                for (int i=0;i<4;i++){
                    int tx=dir_x[i]+cur.x,ty=dir_y[i]+cur.y;
                    if (tx<1||tx>n||ty<1||ty>m||maze[tx][ty]=='#') continue;
                    int tmp=cur.key;
                    if (has[tx][ty]) tmp|=(1<<(has[tx][ty]-1));
                    if (vis[tx][ty][tmp]) continue;
                    Q.push((Node){tx,ty,tmp,cur.step+1});
                    vis[tx][ty][tmp]=true;
                }
             }
            printf("%d\n",ans);
        }
    }
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注