算法:DP
分析:先排序一下,这样的话就从最大的那个点开始,因为这样能滑的更远。
分析:先排序一下,这样的话就从最大的那个点开始,因为这样能滑的更远。
如果状态成立的话,每个点可以由这个点的四个方向得来,记录一个最大值,然后继续找第二大的点。以此类推。
program P1011;
const
maxn=500;
maxt=250000;
dx:array [1..4] of -1..1=(0,-1,0,1);
dy:array [1..4] of -1..1=(-1,0,1,0);
type
atp=record
x,y,val:longint;
end;
var
n,m,tot,ans:longint;
a:array [0..maxt] of atp;
v,f:array [0..maxn,0..maxn] of longint;
procedure init;
var
i,j,x:longint;
begin
ans:=-maxlongint;
tot:=0;
readln(n,m);
for i:=1 to n do
begin
for j:=1 to m do
begin
read(x);
v[i,j]:=x;
inc(tot);
a[tot].x:=i;
a[tot].y:=j;
a[tot].val:=x;
end;
readln;
end;
end;
procedure qsort(l,r:longint);
var
i,j,m:longint;
t:atp;
begin
i:=l;
j:=r;
m:=a[(l+r) shr 1].val;
repeat
while a[i].val>m do inc(i);
while a[j].val<m do dec(j);
if i<=j then
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
inc(i);
dec(j);
end;
until i>j;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end;
procedure main;
var
i,j,x,y,now:longint;
begin
for i:=1 to tot do
begin
x:=a[i].x;
y:=a[i].y;
now:=0;
for j:=1 to 4 do
begin
if (v[x+dx[j],y+dy[j]]>v[x,y]) and (f[x+dx[j],y+dy[j]]>now) then
now:=f[x+dx[j],y+dy[j]];
end;
f[x,y]:=now+1;
if f[x,y]>ans then ans:=f[x,y];
end;
end;
begin
assign(input,'P1011.in'); reset(input);
assign(output,'P1011.out'); rewrite(output);
init;
qsort(1,tot);
main;
writeln(ans);
close(input); close(output);
end.
本文介绍了一种基于动态规划(DP)的算法实现方案,通过排序和状态转移的方式解决特定问题。文章详细展示了如何初始化变量、进行快速排序,并逐步更新状态以找到最优解。
113

被折叠的 条评论
为什么被折叠?



