博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1928 The Peanuts
阅读量:5121 次
发布时间:2019-06-13

本文共 3617 字,大约阅读时间需要 12 分钟。

Description

Mr. Robinson and his pet monkey Dodo love peanuts very much. One day while they were having a walk on a country road, Dodo found a sign by the road, pasted with a small piece of paper, saying "Free Peanuts Here! " You can imagine how happy Mr. Robinson and Dodo were. 
There was a peanut field on one side of the road. The peanuts were planted on the intersecting points of a grid as shown in Figure-1. At each point, there are either zero or more peanuts. For example, in Figure-2, only four points have more than zero peanuts, and the numbers are 15, 13, 9 and 7 respectively. One could only walk from an intersection point to one of the four adjacent points, taking one unit of time. It also takes one unit of time to do one of the following: to walk from the road to the field, to walk from the field to the road, or pick peanuts on a point. 
According to Mr. Robinson's requirement, Dodo should go to the plant with the most peanuts first. After picking them, he should then go to the next plant with the most peanuts, and so on. Mr. Robinson was not so patient as to wait for Dodo to pick all the peanuts and he asked Dodo to return to the road in a certain period of time. For example, Dodo could pick 37 peanuts within 21 units of time in the situation given in Figure-2. 
Your task is, given the distribution of the peanuts and a certain period of time, tell how many peanuts Dodo could pick. You can assume that each point contains a different amount of peanuts, except 0, which may appear more than once. 

Input

The first line of input contains the test case number T (1 <= T <= 20). For each test case, the first line contains three integers, M, N and K (1 <= M, N <= 50, 0 <= K <= 20000). Each of the following M lines contain N integers. None of the integers will exceed 3000. (M * N) describes the peanut field. The j-th integer X in the i-th line means there are X peanuts on the point (i, j). K means Dodo must return to the road in K units of time.

Output

For each test case, print one line containing the amount of peanuts Dodo can pick.

Sample Input

26 7 210 0 0 0 0 0 00 0 0 0 13 0 00 0 0 0 0 0 70 15 0 0 0 0 00 0 0 9 0 0 00 0 0 0 0 0 06 7 200 0 0 0 0 0 00 0 0 0 13 0 00 0 0 0 0 0 70 15 0 0 0 0 00 0 0 9 0 0 00 0 0 0 0 0 0

Sample Output

3728 思路:先把花生个数从大到小排序,然后判断是否超时。
1 #include
2 #include
3 #include
4 5 struct point 6 { 7 int x; 8 int y; 9 int num;10 }node[2510],temp;11 12 int init(int m,int n) 13 {14 int t,i,j,k=0;15 for(i=1;i<=m;++i)16 for(j=1;j<=n;++j)17 {18 scanf("%d",&t);19 if(t)20 {21 node[k].x=j;22 node[k].y=i;23 node[k].num=t;24 ++k;25 }26 }27 return k;28 }29 30 int comp(const void *p1,const void *p2)31 {32 return (*(struct point *)p2).num-(*(struct point *)p1).num;33 }34 35 int main()36 {37 int T,m,n,i,time,k;38 long sum;39 scanf("%d",&T);40 while(T--)41 {42 scanf("%d%d%d",&m,&n,&time);43 sum=0;44 k=init(m,n);45 qsort(node,k,sizeof(struct point),comp);46 time-=node[0].y;47 for(i=0;i
=node[i].y+1)50 {51 sum+=node[i].num;52 time-=abs(node[i].x-node[i+1].x)+abs(node[i].y-node[i+1].y)+1;53 }54 else55 break;56 }57 printf("%d\n",sum);58 }59 return 0;60 }
View Code

 

转载于:https://www.cnblogs.com/wangrunwen/p/4081509.html

你可能感兴趣的文章
Day19内容回顾
查看>>
第七次作业
查看>>
SpringBoot项目打包
查看>>
Linux操作系统 和 Windows操作系统 的区别
查看>>
《QQ欢乐斗地主》山寨版
查看>>
文件流的使用以及序列化和反序列化的方法使用
查看>>
Android-多线程AsyncTask
查看>>
第一个Spring冲刺周期团队进展报告
查看>>
红黑树 c++ 实现
查看>>
Android 获取网络链接类型
查看>>
linux中启动与终止lnmp的脚本
查看>>
gdb中信号的处理[转]
查看>>
LeetCode【709. 转换成小写字母】
查看>>
如何在Access2007中使用日期类型查询数据
查看>>
Jzoj4757 树上摩托
查看>>
CF992E Nastya and King-Shamans(线段树二分+思维)
查看>>
第一个Java Web程序
查看>>
树状数组_一维
查看>>
如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:
查看>>
linux install ftp server
查看>>