POJ2110

题意:给定一个矩阵,从左上角到右下角,使走过的路径中数字的最大值最小值之差最小,问差最小是多少.

分析:二分枚举差值,枚举下界,bfs判定可行性.

code:

type  mapnode=record
      x,y:longint;
end;
var   fx:array[1..4,0..1] of longint=((0,1),(0,-1),(-1,0),(1,0));
      tall:array[0..101,0..101] of longint;
      vis:array[0..101,0..101] of boolean;
      q:array[0..10000] of mapnode;
      n,i,j,l,r,mid,maxh,minh,hx:longint;
      flag:boolean;

      function check(i,j,ll,rr:longint):boolean;
      begin
            if (tall[i,j]>=ll)and(tall[i,j]<=rr) then exit(true);
            exit(false);
      end;

      function bfs(ll,rr:longint):boolean;
      var   h,t,o,x,y:longint;
      begin
            if not check(1,1,ll,rr) then exit(false);
            fillchar(vis,sizeof(vis),0);
            h:=0; t:=1;
            q[1].x:=1;
            q[1].y:=1;
            vis[1,1]:=true;
            while h<t do
            begin
                  inc(h);
                  for o:=1 to 4 do
                  begin
                        x:=q[h].x+fx[o,0];
                        y:=q[h].y+fx[o,1];
                        if not check(x,y,ll,rr) then continue;
                        if (x>0)and(x<=n)and(y>0)and(y<=n)and(not vis[x,y]) then
                        begin
                              inc(t);
                              q[t].x:=x;
                              q[t].y:=y;
                              vis[x,y]:=true;
                              if (x=n)and(y=n) then exit(true);
                        end;
                  end;
            end;
            exit(false);
      end;

begin
      readln(n);
      minh:=0; maxh:=0;
      for i:=1 to n do
      begin
            for j:=1 to n do
            begin
                  read(tall[i,j]);
                  if tall[i,j]>maxh then maxh:=tall[i,j];
            end;
            readln;
      end;

      l:=minh; r:=maxh;

      while l<r do
      begin
            mid:=(l+r)>>1;
            flag:=false;
            for hx:=minh to maxh-mid do
               if Bfs(hx,hx+mid) then
               begin
                     flag:=true;
                     break;
               end;
            if flag then r:=mid
            else l:=mid+1;
      end;
      writeln(l);
end.

转载于:https://www.cnblogs.com/exponent/archive/2011/08/12/2136326.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值