vijos P1026毒药?解药?

#include <stdio.h>
#define STATUS 0
#define STEP 1


int table[120][10];
short searched[1<<15];
int queue[1<<15][2];
int head, tail;


void addToQueue(int status, int step){
    if(!searched[status]){
        searched[status] = 1;
        queue[tail][STATUS] = status;
        queue[tail][STEP] = step;
        tail++;
    }
}
int bfs(int numIllness, int numDrug){
    int status, step, i, j, tmp, target;
    head = tail = 0;
    target = (1<<numIllness) - 1;  //all illnesses have been cured
    addToQueue(0, 0);  //all illnesses haven't been cured
    while(head < tail){
        status = queue[head][STATUS];
        step = queue[head][STEP];
        if(status == target)
            return step;
        for(i=0; i<numDrug; i++){
            tmp = status;
            for(j=0; j<numIllness; j++){
                switch(table[i][j]){
                    case 1:
                        tmp |= (1<<j);
                        break;
                    case -1:
                        tmp &= (target^(1<<j));
                }
            }
            addToQueue(tmp, step+1);
        }
        head++;
    }
    return -1;
}
int main(){
    int numIllness, numDrug;
    int i, j;
    scanf("%d %d", &numIllness, &numDrug);
    for(i=0; i<numDrug; i++){
        for(j=0; j<numIllness; j++)
            scanf("%d", &table[i][j]);
    }
    for(i=0; i<(1<<numIllness); i++)
        searched[i] = 0;


    i = bfs(numIllness, numDrug);
    if(i < 0)
        printf("The patient will be dead.\n");
    else
        printf("%d\n", i);


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值