毒药?解药?(vijos1026)

算法:搜索

其实还算是一道比较简单的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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值