洛谷p1902 二分答案 +bfs

来源:互联网 发布:161631人工智能怎么 编辑:程序博客网 时间:2024/06/05 04:54

题目描述

伊朗伊斯兰革命卫队(某恐怖组织)正在策划一起刺杀行动,他们的目标是沙特驻美大 使朱拜尔。他们来到了沙特驻美使馆,准备完成此次刺杀,要进入使馆首先必须通过使馆前 的防御迷阵。

迷阵由 n*m 个相同的小房间组成,每个房间与相邻四个房间之间有门可通行。在第 n 行的 m 个房间里有 m 个机关,这些机关必须全部打开才可以进入大使馆。而第 1 行的 m 个 房间有 m 扇向外打开的门,是迷阵的入口。除了第 1 行和第 n 行的房间外,每个房间都被 使馆的安保人员安装了激光杀伤装置,将会对进入房间的人造成一定的伤害。第 i 行第 j 列 造成的伤害值为 p[i][j](第 1 行和第 n 行的 p 值全部为 0)。

现在伊斯兰革命卫队打算以最小伤害代价进入迷阵,打开全部机关,显然,他们可以选 择任意多的人从任意的门进入,但必须到达第 n 行的每个房间。一个士兵受到的伤害值为他 到达某个机关的路径上所有房间的伤害值中的最大值,整个部队受到的伤害值为所有士兵的 伤害值中的最大值。现在,这个恐怖组织掌握了迷阵的情况,他们需要提前知道怎么安排士 兵的行进路线可以使得整个部队的伤害值最小。

输入输出格式

输入格式:

第一行有两个整数 n,m,表示迷阵的大小。

接下来 n 行,每行 m 个数,第 i 行第 j 列的数表示 p[i][j]。

输出格式:

输出一个数,表示最小伤害代价。

输入输出样例

输入样例#1:
4 20 0 3 5 2 4 0 0 
输出样例#1:
3

说明

50%的数据,n,m<=100;

100%的数据,n,m<=1000,p[i][j]<=1000。




水过。。。代码难看dalao勿吐槽。

var
  l,r,i,j,k,t,n,m,bj:longint;
  a:array[0..1001,0..1001]of longint;
  b:array[0..1001,0..1001]of boolean;
  c,d:array[0..1000001]of longint;
function bfs:boolean;
  var
    l,r,i,j,k:longint;
  begin
    c[1]:=1;
    d[1]:=1;
    l:=1;
    r:=1;
    while l<=r do
    begin
      if c[l]=n then exit(true);
      if b[c[l]+1,d[l]] then begin b[c[l]+1,d[l]]:=false; if c[l]+1=n then exit(true);inc(r);c[r]:=c[l]+1;d[r]:=d[l];end;
      if b[c[l]-1,d[l]] then begin b[c[l]-1,d[l]]:=false; inc(r);c[r]:=c[l]-1;d[r]:=d[l];end;
      if b[c[l],d[l]+1] then begin b[c[l],d[l]+1]:=false; inc(r);c[r]:=c[l];d[r]:=d[l]+1;end;
      if b[c[l],d[l]-1] then begin b[c[l],d[l]-1]:=false; inc(r);c[r]:=c[l];d[r]:=d[l]-1;end;
      inc(l);
    end;
    exit(false);
  end;
begin
  readln(n,m);
  t:=0;
  for i:=1 to m do
    for j:=1 to n do
    begin
      read(a[i,j]);
      if a[i,j]>t then t:=a[i,j];
    end;
  l:=1;
  r:=t;
  while l<=r do
  begin
    fillchar(b,sizeof(b),false);
    t:=(l+r+1)div 2;
    for i:=1 to n do
      for j:=1 to m do
        if a[i,j]<=t then b[i,j]:=true;
    if bfs then begin bj:=t;r:=t-1;end
                else l:=t+1;
  end;
  writeln(bj);
end.

0 0
原创粉丝点击