hdu 4771 Stealing Harry Potter's Precious【状压+Bfs】

Stealing Harry Potter's Precious

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3245    Accepted Submission(s): 1474

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

 

题目大意:

给你一个N*M的地图,起点在@处 ,然后给你K个哈利波特的东西,你的任务是从起点开始走,直到拿到所有他的东西为止,问最少需要走多少步,如果拿不全,输出-1、

0<k<=4;

思路:


1、因为考虑到一个点可以重复走这一点,我们将vis【】【】数组将要开大一维,考虑到一共K个物品,且K最大不超过4,那么我们将状态压缩一共16种,都用十进制数代表,例如5:0101,表示第一个物品和第三个物品已经在手里了,第二个物品和第四个没有在手里。

那么我们将数组开成:vis【tmp】【x】【y】即可,表示在点(x,y)处,手里有tmp这种情况的物品,是否有走过,如果有做过,其值为1,否则为0.


2、那么我们确定一下算法过程:

①将K个物品按照输入顺序编号,分别表示第i个物品。

②Bfs起点定位@处,开始向四周Bfs,同时定义vis【tmp】【x】【y】数组,表示在点(x,y)处,手里有tmp这种情况的物品,是否有走过,如果有做过,其值为1,否则为0.

③在不能走#的条件下,下一步如果遇到的是点,那么直接走上去,如果下一步遇到的是有物品存在的地方,那么我们首先要判断手上是否已经有了该物品,对应二进制条件下,tmp=0101(5)如果想要测试是否有第一个物品,根据与运算的特性:相同位同为1值为1,否则为0.那么我们判断tmp&(1<<当前遇到的物品的编号)的值如果是大于0的,那么说明tmp这时手上是有这种物品的,那么当做这个格子为点去走就可以了,相反,如果与运算的值为0,说明手上没有这种物品,那么拿起来,tmp+=(1<<当前物品的编号)然后继续走即可。


3、当我们走到某一时刻,tmp==(1<<k)-1,那么说明已经收集到了所有物品,输出此时一共走了多少步即可,那么如果一直没有遇到这样的情况,那么输出-1。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
    int x,y,output,tmp;
}now,nex;
char a[300][300];
int vis[32][300][300];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int n,m,q;
void Bfs(int sx,int sy)
{
    memset(vis,0,sizeof(vis));
    vis[0][sx][sy]=1;
    queue<node >s;
    now.x=sx;
    now.y=sy;
    now.output=0;
    now.tmp=0;
    if(a[now.x][now.y]>='0'&&a[now.x][now.y]<='5')
    {
        int num=a[now.x][now.y]-'0';
        now.tmp+=(1<<num);
    }
    s.push(now);
    while(!s.empty())
    {
        now=s.front();
        if(now.tmp==(1<<q)-1)
        {
            printf("%d\n",now.output);
            return ;
        }
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            nex.output=now.output+1;
            nex.tmp=now.tmp;
            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&a[nex.x][nex.y]!='#')//hefa
            {
                if(a[nex.x][nex.y]=='.')
                {
                    if(vis[nex.tmp][nex.x][nex.y]==0)
                    {
                        vis[nex.tmp][nex.x][nex.y]=1;
                        s.push(nex);
                    }
                }
                else if(a[nex.x][nex.y]>='0'&&a[nex.x][nex.y]<='5')
                {
                    int num=a[nex.x][nex.y]-'0';
                    if((nex.tmp&(1<<num))==0)
                    {
                        nex.tmp+=(1<<num);
                        if(vis[nex.tmp][nex.x][nex.y]==0)
                        {
                            vis[nex.tmp][nex.x][nex.y]=1;
                            s.push(nex);
                        }
                    }
                    else
                    {
                        if(vis[nex.tmp][nex.x][nex.y]==0)
                        {
                            vis[nex.tmp][nex.x][nex.y]=1;
                            s.push(nex);
                        }
                    }
                }
            }
        }
    }
    printf("-1\n");
    return ;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;
        int sx=0,sy=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            for(int j=0;j<m;j++)
            {
                if(a[i][j]=='@')
                {
                    sx=i;sy=j;
                }
            }
        }
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            int xx,yy;
            scanf("%d%d",&xx,&yy);
            xx--;yy--;
            a[xx][yy]=i+'0';
        }
        Bfs(sx,sy);
    }
}





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值