vijos p1011(清帝之惑之顺治)
一到DP题目,跟晴天小猪历险记之hill类似,光扫一次不够,设个布尔变量,确定不能再优时,退出循环,一开始本来以为要记录比自己低的和比自己高的,结果总是超时,后来发现其实只要记录二者之一就OK。
代码:
1 program P1011; uses math; 2 var 3 i,j,k,l,m,n:longint; 4 bool:boolean; 5 s:array[0..501,0..501]of longint; 6 up,down:array[0..501,0..501]of longint; 7 begin 8 read(m,n); 9 for i:=1 to m do 10 for j:=1 to n do 11 read(s[i,j]); 12 repeat 13 bool:=true; 14 for i:=1 to m do 15 for j:=1 to n do 16 begin 17 if (j<n)and(s[i,j]>s[i,j+1]) then begin 18 if (down[i,j]<down[i,j+1]+1) then 19 begin 20 bool:=false; 21 down[i,j]:=down[i,j+1]+1; 22 end; 23 end; 24 if (j>1)and(s[i,j]>s[i,j-1]) then begin 25 if down[i,j]<down[i,j-1]+1 then 26 begin 27 bool:=false; 28 down[i,j]:=down[i,j-1]+1; 29 end; 30 end; 31 if (i<m)and(s[i,j]>s[i+1,j])then begin 32 if down[i,j]<down[i+1,j]+1 then 33 begin 34 bool:=false; 35 down[i,j]:=down[i+1,j]+1; 36 end; 37 end; 38 if (i>1)and(s[i,j]>s[i-1,j]) then begin 39 if down[i,j]<down[i-1,j]+1 then 40 begin 41 bool:=false; 42 down[i,j]:=down[i-1,j]+1; 43 end; 44 end 45 end; 46 until bool=true; 47 k:=0; 48 for i:=1 to m do 49 for j:=1 to n do 50 if k<down[i,j] then k:=down[i,j]; 51 write(k+1); 52 end.