HDU 1885 Key Task + HDU 4771 Stealing Harry Potter's Precious(状压)


这两题都是多个物品取和不取的问题,一般用二进制存储。假设有K件物品,就开一个vis[N][N][1 << K]的数组来表示当前这个点已经得到了几件物品。其实可以开这个一个数组vis[N][N][2][2][2][2],也可以达到同样的效果,但是当k很大的时候会很麻烦。
当K为4时,编号为0,1,2,3。当前的状态用sta来表示:
当只取了0时:sta = 1<<0,此时的sta为1,二进制为 0001
当只取了1时:sta = 1 <<1 ,此时的sta 为2,二进制为 0010
取完0再取1时: sta |= 1<<1, 此时的sta为3,二进制为 0011

判断当前 1物品取了没 : (sta >> 1)&1,若为真则取了。

同时给自己强化下位运算,老错:
0&0=0,0&1=0 ,1&0=0 , 1&1=1;
0 | 0=0 , 0 | 1=1 , 1 | 0= 1, 1 | 1=1;


http://acm.hdu.edu.cn/showproblem.php?pid=4771
题意: 给个n*m的网格,@是起点,#是障碍物。再给个k,下面k行给出哪个格子里面有宝石。问从起点开始得到所有的宝石最少几步。
(直接bfs+vis[N][N][1 << k]..
其中结构体中加个sta,表示当前取物品的状态,用vis[i][j][now.sta]判重

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100+10;
char Map[N][N];
bool vis[N][N][1<<5];
int n,m,k;
struct Node
{
    int x,y;
    int cnt,sta;
};
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int bfs(int x,int y)
{
    memset(vis,true,sizeof(vis));
    Node now,next;
    queue<Node> que;
    now.x=x; now.y=y;
    now.cnt=now.sta=0;
    vis[x][y][0]=false;
    que.push(now);
    while(!que.empty())
    {
        now=que.front();
        que.pop();
        bool flag=true;
        for(int i=1;i<=k;i++)
        {
            if(!((now.sta>>i)&1))
            {
                flag=false; break;
            }
        }
        if(flag) return now.cnt;

        for(int i=0;i<4;i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.sta = now.sta;

            if(next.x<1 ||next.x>n) continue;
            if(next.y<1 ||next.y>m) continue;
            if(!vis[next.x][next.y][next.sta] || Map[next.x][next.y]=='#') continue;//直接判掉同样的状态已经走过的点

            if(Map[next.x][next.y]>'0'&&Map[next.x][next.y]<'9') next.sta |= 1<< (Map[next.x][next.y]-'0');
            vis[next.x][next.y][next.sta]=false;
            next.cnt=now.cnt+1;
            que.push(next);
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        int x,y;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                scanf(" %c",&Map[i][j]);
                if(Map[i][j]=='@') x=i,y=j;
            }
        scanf("%d",&k);
        for(int i=1;i<=k;i++)
        {
            int xx,yy;
            scanf("%d %d",&xx,&yy);
            if(Map[xx][yy]!='#') Map[xx][yy]=i+'0';
        }
        printf("%d\n",bfs(x,y));
    }
    return 0;
}

http://acm.hdu.edu.cn/showproblem.php?pid=1885
**题意:**n*m的网格,#是障碍物,g,b,r,y是钥匙,与之对应的G,B,R,Y是门,其中一把钥匙可以开多个颜色相同的门,问能否走出这个迷宫,要是能走出最少几步。
(与上题并无不同。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 110;
char Map[N][N];
bool vis[N][N][1<<5];
int dir[4][2]={0,1,1,0,0,-1,-1,0};
struct Node
{
    int x,y;
    int cnt,sta;
};
int n,m;
int judge(char x)
{
    int tmp;
    if(x=='B'||x=='b') tmp=1;
    if(x=='R'||x=='r') tmp=2;
    if(x=='G'||x=='g') tmp=3;
    if(x=='Y'||x=='y') tmp=4;
    return tmp;
}
int bfs(int x,int y)
{
    memset(vis,true,sizeof(vis));
    Node now,next;
    queue<Node> que;
    now.x=x;
    now.y=y;
    now.cnt=now.sta=false;
    vis[x][y][0]=false;
    que.push(now);
    while(!que.empty())
    {
        now=que.front();
        que.pop();

        if(Map[now.x][now.y]=='X') return now.cnt;

        for(int i=0;i<4;i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.sta=now.sta;

            if(next.x<1||next.x>n) continue;
            if(next.y<1||next.y>m) continue;
            if(!vis[next.x][next.y][now.sta]||Map[next.x][next.y]=='#') continue;

            if(Map[next.x][next.y]>'A'&&Map[next.x][next.y]<'Z'&&Map[next.x][next.y]!='X')
            {
                if(!(now.sta>>(judge(Map[next.x][next.y]))&1)) continue;
            }

            if(Map[next.x][next.y]>'a'&&Map[next.x][next.y]<'z')
            {
                next.sta |= 1<<(judge(Map[next.x][next.y]));
            }
            next.cnt=now.cnt+1;
            vis[next.x][next.y][next.sta]=false;
            que.push(next);
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        int x,y;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                scanf(" %c",&Map[i][j]);
                if(Map[i][j]=='*') x=i,y=j;
            }
        int ans=bfs(x,y);
        if(ans==-1) puts("The poor student is trapped!");
        else printf("Escape possible in %d steps.\n",ans);
    }
    return 0;
}

长期更新 :)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值