poj 1928 peanuts 排序
来源:互联网 发布:vb中如何建立数据库 编辑:程序博客网 时间:2024/04/30 11:29
The Peanuts
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 6863 Accepted: 2834
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.
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
算法思路:
直接对所有的点含有 花生的数量进行排序
依次取出最大的花生的数量。要注意的是这个花生的数量是个不相同的。
然后边取出花生边判断时间。
#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<iostream>#include<stdlib.h>using namespace std;struct node{ int x,y,ans; }a[3000];int cmp(node a,node b){ return a.ans>b.ans;}int main(){// freopen("ysh.text","r",stdin);// freopen("ans.text","w",stdout); int i,j,n,m,k,x,y,z,time,ge,t,spend; scanf("%d",&z); while(z--) { ge=0; memset(a,0,sizeof(a)); scanf("%d %d %d",&n,&m,&time); for(i=1;i<=n;i++) for(j=1;j<=m;j++) { scanf("%d",&a[(i-1)*m+j].ans); if( a[(i-1)*m+j].ans >0 )ge++; //记录有多少个有花生的点 a[ (i-1)*m+j ].x=i; //记录每个点的坐标 a[ (i-1)*m+j ].y=j; } sort(a+1,a+1+i*j,cmp); //对有花生的点进行排序 y=a[1].y; x=0; t=0;j=0;i=1; int sum=0; while(t<=time) { // cout<<"t="<<t<<endl; if(j==ge)break; spend= abs(a[i].x-x)+1+abs(a[ i ].y-y)+a[i].x; //判断取完花生后能否返回 if(t+spend>time)break; spend-=a[i].x; //能返回的话就减去剩余时间 t+=spend; sum+=a[i].ans; //累加采摘的花生的数量 x=a[i].x; y=a[i].y; i++; j++; } printf("%d\n",sum); }}
- poj 1928 peanuts 排序
- poj 1928 The Peanuts
- POJ-1928-The Peanuts
- POJ 1928 The Peanuts
- POJ 1928 The Peanuts
- POJ 1928 The Peanuts
- POJ 1928 The Peanuts 笔记
- ACM篇:POJ 1928--The Peanuts
- POJ:1928 花生问题 The Peanuts
- pku 1928 The Peanuts
- 【解题报告】 openjudge The peanuts 数据结构与算法mooc 内排序
- 花生问题——百练OJ:2950:摘花生与1928:The Peanuts
- fjnu 1013 The Peanuts
- 经典散文 《落花生》Peanuts
- hdu 1355 The Peanuts
- hdu1355 The Peanuts
- POJ1928 The Peanuts
- POJ1928 The Peanuts
- 百度2013校园招聘研发类笔试题
- shell shift
- PostGis入门知识
- 华数传媒2013校园招聘测试开发类笔试题
- java异常学习二(总结)
- poj 1928 peanuts 排序
- VC2010 Intermediate Directory 造成的链接问题
- 跟燕十八学习PHP-第二十三天-mysql日期时间列详解date-time-year-timesta
- JSp window对象
- 服务Service和通信HttpGet及HttpPost
- 基于Python下的OAuth 2.0 点点网API认证
- 时间片轮转调度算法
- JSP内置对象
- 我编写的C语言程序(记录)