您的位置:首页 > 其它

Noip2004P2 花生采摘

2011-09-07 23:01 169 查看
题目:

花生采摘

来源:

Noip2004P2

题目大意:

M*N的矩阵中每个点有值,可进行k次操作,求依次去最大值最多能去多少。

每次操作为以下之一

1) 从路边跳到最靠近路边(即第一行)的某棵花生植株;

2) 从一棵植株跳到前后左右与之相邻的另一棵植株;

3) 采摘一棵植株下的花生;

4) 从最靠近路边(即第一行)的某棵花生植株跳回路边。

数据范围:

1 <= M, N <= 20、0 <= K <= 1000

样例:

6 7 21
0 0 0 0 0 0 0
0 0 0 0 13 0 0
0 0 0 0 0 0 7
0 15 0 0 0 0 0
0 0 0 9 0 0 0
0 0 0 0 0 0 0
37

做题思路:

依照题意每次取最大知道剩下的操作数只够回到路边,记得啊这不是动规,还有采摘 也算操作。

知识点:

模拟、贪心

type
act=record
x,y,d:longint;
end;
var
a:array[0..500]of act;
n,m,i,j,ans,k,tot,x,y:longint;
//==================================================
procedure qsort(l,r:longint);
var
i,j,k:longint;
t:act;
begin
i:=l;j:=r;
k:=a[(l+r)shr 1].d;
repeat
whilea[i].d>k do inc(i);
whilea[j].d<k do dec(j);
ifi<=j then
begin
t:=a[i];a[i]:=a[j];a[j]:=t;
inc(i);dec(j);
end;
untili>j;
ifi<r then qsort(i,r);
ifj>l then qsort(l,j);
end;
//================================================
begin
readln(m,n,k);
fori:=1 to m do
begin
forj:=1 to n do
begin
inc(tot);
read(a[tot].d);
a[tot].x:=i;a[tot].y:=j;
end;
readln;
end;
qsort(1,tot);
i:=1;
y:=a[1].y;x:=0;
while(k>=0)and(i<=tot) do
begin
if(k-(abs(a[i].x-x)+abs(a[i].y-y))-a[i].x-1)>=0 then{<前往目标值距离和回到路边的距离和采摘操作总和是否在操作数内>}
begin
k:=k-(abs(a[i].x-x)+abs(a[i].y-y))-1;{<走过去并采摘>}
x:=a[i].x;y:=a[i].y;
inc(ans,a[i].d);
end
elsek:=0;{<不满足条件直接回路边>}
inc(i);
end;
writeln(ans);
end.
题目来源:http://yuanti.tyvj.cn:8080/Problem_Show.asp?id=1043
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: