#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;
}
vijos P1026毒药?解药?
最新推荐文章于 2022-05-21 16:51:51 发布