Codevs 2594 解药还是毒药

Codevs 传送门

我们可以用二进制数来表示状态(状态压缩),1代表患病,0代表不患病。
把每一种药对人的影响分别用两个数记录下来:
对于治病,我们用0代表治病,1代表无影响,我们与当前状态做一下 and运算,就把病治好啦。
对于患病,我们用1代表患病,0代表无影响,我们与当前状态做一下或 | 运算,就把病患上啦。

用bfs不断拓展,直到人的状态为一串0就可以啦。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int n,m;
bool f[(1<<10)+10];
int ill[101],cure[101];
struct H{
    int illnes;
    int step;
};
int bfs()
{
    int s=(1<<n)-1;
    queue <H> q;
    H k;k.illnes=s;k.step=0;
    q.push(k);f[s]=1;
    while(!q.empty())
    {
        k=q.front();
        q.pop();

        if(k.illnes==0) return k.step;
        for(int i=1;i<=m;i++)
        {
            int x;
            x=(k.illnes&cure[i])|ill[i];
            if(!f[x]){
                if(x==0) return k.step+1;
                H l;l.illnes=x;
                l.step=k.step+1;
                q.push(l);f[x]=1;
            } 
        } 
    }
    return -1;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int D=0,Z=0;
        for(int x,j=1;j<=n;j++)
        {
            scanf("%d",&x);
            if(x==1) Z=Z+(1<<(j-1));
            if(x==-1) D=D+(1<<(j-1)); 
        }
        ill[i]=D;cure[i]=~Z;
    }
    int ans=bfs();
    if(ans==-1) printf("The patient will be dead.");
    else printf("%d",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/dfsac/p/7587858.html