Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
记忆化搜索水题~~RT 贴代码 program leap2;//By_Thispoet const maxn=365; ddx:Array[1..8]of Integer=(1,1,2,2,-1,-1,-2,-2); ddy:Array[1..8]of Integer=(2,-2,1,-1,2,-2,1,-1); var i,j,k,m,n,p,q,ans :Longint; f,map,frx,fry :Array[0..maxn,0..maxn]of Longint; posx,posy :Longint; function check(i,j:Longint):Boolean; begin if (i>n)or(j>n)or(i<=0)or(j<=0)then exit(false); exit(true); end; function dp(i,j:Longint):Longint; var k,p,q:Longint; begin if f[i,j]<>-1 then exit(f[i,j]); f[i,j]:=1; for k:=1 to 8 do begin p:=i+ddx[k];q:=j+ddy[k]; if check(p,q) and (map[p,q]>map[i,j]) then begin if dp(p,q)+1>f[i,j] then begin f[i,j]:=f[p,q]+1; frx[i,j]:=p; fry[i,j]:=q; end else if (dp(p,q)+1=f[i,j])and(map[p,q]<map[frx[i,j],fry[i,j]]) then begin frx[i,j]:=p;fry[i,j]:=q; end; end; end; exit(f[i,j]); end; begin fillchar(f,sizeof(f),255); fillchar(frx,sizeof(frx),0); fillchar(fry,sizeof(fry),0); readln(n); ans:=0; for i:=1 to n do for j:=1 to n do read(map[i,j]); for i:=1 to n do for j:=1 to n do if dp(i,j)>ans then begin ans:=f[i,j]; posx:=i; posy:=j; end else if (ans=f[i,j])and(map[i,j]<map[posx,posy]) then begin posx:=i; posy:=j; end; writeln(ans); while not ((posx=0)and(posy=0)) do begin writeln(map[posx,posy]); i:=posx;j:=posy; posx:=frx[i,j]; posy:=fry[i,j]; end; end. Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator