算法:搜索
其实还算是一道比较简单的BFS,由于长时间不写了,手有点生……
一看有明显的0和1的区别,果断采用位运算。
1.先假设都患病,并设状态都为1,那么初始状态也就是n个1。目标状态因此很明确就是全为0。
其实还算是一道比较简单的BFS,由于长时间不写了,手有点生……
一看有明显的0和1的区别,果断采用位运算。
1.先假设都患病,并设状态都为1,那么初始状态也就是n个1。目标状态因此很明确就是全为0。
2.因为可以重复选,所以每次从1~m里选一个然后再每一项都改变,这里就采用了位运算的技巧。
program VJ1026;
const
maxn=10;
maxm=100;
maxt=10000;
type
atp=record
zt,dep:longint;
end;
var
head,tail,n,m:longint;
a:array [0..maxm,0..maxn] of longint;
b:array [0..maxt] of boolean;
que:array [0..maxt] of atp;
procedure init;
var
i,j:longint;
begin
readln(n);
readln(m);
for i:=1 to m do
begin
for j:=0 to n-1 do read(a[i,j]);
readln;
end;
end;
procedure main;
var
i,j,t:longint;
begin
t:=0;
fillchar(b,sizeof(b),false);
for i:=1 to n do t:=(t shl 1)+1;
head:=0;
tail:=1;
que[1].zt:=t;
que[1].dep:=0;
b[t]:=true;
while head<tail do
begin
inc(head);
for i:=1 to m do
begin
t:=que[head].zt;
for j:=0 to n-1 do
begin
if a[i,j]=0 then continue;
if a[i,j]=1 then t:=t and (not (1 shl j));
if a[i,j]=-1 then t:=t or (1 shl j);
end;
if not b[t] then
begin
inc(tail);
que[tail].zt:=t;
que[tail].dep:=que[head].dep+1;
b[t]:=true;
if t=0 then
begin
writeln(que[head].dep+1);
close(input);
close(output);
halt;
end;
end;
end;
end;
end;
begin
assign(input,'VJ1026.in'); reset(input);
assign(output,'VJ1026.out'); rewrite(output);
init;
main;
writeln('The patient will be dead.');
close(input); close(output);
end.