TYVJ 1138 Cannot神牛的RP采摘
第一篇文章就弄个水题吧。。
范围太小,DP或者搜索都可以过,就是无解的判断容易弄错。
Code:
var f:array [0..201,0..201,0..201] of longint; a:array [0..201,0..201] of longint; n,m,k,i,j,v,bx,by,ex,ey:longint; function max(a,b:longint):longint; begin if a>b then exit(a) else exit(b); end; begin readln(n,m,k); readln(bx,by,ex,ey); if abs(ex-bx)+abs(ey-by)>k then begin writeln('Cannot can''t!'); exit; end; for i:=1 to n do for j:=1 to m do read(a[i,j]); for v:=0 to k do for i:=0 to n+1 do for j:=0 to m+1 do f[i,j,v]:=-maxlongint div 2; f[bx,by,0]:=0; for v:=1 to k do for i:=1 to n do for j:=1 to m do if a[i,j]<>0 then f[i,j,v]:=max( max( max( f[i,j-1,v-1], f[i,j+1,v-1]), max( f[i-1,j,v-1], f[i+1,j,v-1]))+a[i,j], f[i,j,v-1]) else f[i,j,v]:=f[i,j,v-1]; if f[ex,ey,k]<>-maxlongint div 2 then writeln(f[ex,ey,k]) else writeln('Cannot can''t!'); end.