活蹦乱跳的香穗子(NDK1350)

香穗子在田野上跳动,寻找最优的蘑菇收集路径。通过动态规划或记忆化深度优先搜索算法,探索最优路径问题的解决之道。本文将带你了解如何利用算法技巧,解决这一趣味性的寻路问题。
活蹦乱跳的香穗子


Time Limit:10000MS  Memory Limit:65536K
Total Submit:26 Accepted:9 


Description 


香穗子在田野上调蘑菇!她跳啊跳,发现自己很无聊,于是她想了一个有趣的事情,每个格子最多只能经过1次,且每个格子都有其价值 
跳的规则是这样的,香穗子可以向上下左右四个方向跳到相邻的格子,并且她只能往价值更高(这里是严格的大于)的格子跳. 
香穗子可以从任意的格子出发,在任意的格子结束, 
那么她最多能跳几次? 




Input 


第一行n,m,表示田野的长和宽 
接下来n行,每行m个数,表示该格的价值 




Output 


一个数,表示最多跳得次数


Sample Input 




2 2
2 5
-1 3




Sample Output 




2


Hint 


数据范围: 
n,m <=100 
答案保正小于Maxlongint 




Source 


NOIdaokan


算法:DP或记忆化DFS


很简单的一道入门级别的DP,快排之后从小的开始向四周扩展然后不断保留最大值,更新给ans,输出ans即可。

program p1;

const
 maxn=100;
 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,data:longint;
 end;

var
 n,m,ans:longint;
 a,s:array [0..maxn,0..maxn] of longint;
 d:array [0..maxn*maxn] of atp;

procedure init;
var
 i,j:longint;
begin
 readln(n,m);
 for i:=1 to n do
  begin
   for j:=1 to m do
    begin
     read(a[i,j]);
     d[(i-1)*m+j].x:=i;
     d[(i-1)*m+j].y:=j;
     d[(i-1)*m+j].data:=a[i,j];
    end;
   readln;
  end;
end;

procedure qsort(l,r:longint);
var
 i,j,mm:longint;
 t:atp;
begin
 i:=l;
 j:=r;
 mm:=d[(l+r) shr 1].data;
 repeat
  while d[i].data<mm do inc(i);
  while d[j].data>mm do dec(j);
  if i<=j then
   begin
    t:=d[i];
    d[i]:=d[j];
    d[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;

function max(x,y:longint):longint;
begin
 if x>y then exit(x) else exit(y);
end;

procedure main;
var
 i,j,tx,ty:longint;
begin
 for i:=1 to n*m do
  begin
   for j:=1 to 4 do
    begin
     tx:=d[i].x+dx[j];
     ty:=d[i].y+dy[j];
     if (tx>0) and (tx<=n) and (ty>0) and (ty<=m) and (a[tx,ty]>a[d[i].x,d[i].y]) then
      begin
       s[tx,ty]:=max(s[tx,ty],s[d[i].x,d[i].y]+1);
       if s[tx,ty]>ans then ans:=s[tx,ty];
      end;
    end;
  end;
end;

begin
 assign(input,'1.in'); reset(input);
 assign(output,'1.out'); rewrite(output);

 init;
 qsort(1,n*m);
 main;
 writeln(ans+1);

 close(input); close(output);
end.                                       


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值