POJ 1928 The Peanuts
来源:互联网 发布:徐州软件测试招聘 编辑:程序博客网 时间:2024/04/30 10:13
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 7743 Accepted: 3197
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<stdlib.h>typedef struct tmp{int x;int y;int peaunts;}Point;Point points[2500];int partition(int left, int right){Point tmppoint = points[right];while(left < right){while(left < right && points[left].peaunts >= tmppoint.peaunts) left ++;if(left < right){points[right] = points[left];right --;}while(left < right && points[right].peaunts <= tmppoint.peaunts) right --;if(left < right){points[left] = points[right];left ++;}}points[left] = tmppoint;return left;}void swap(int a, int b){Point tmppoint = points[a];points[a] = points[b];points[b] = tmppoint;}void qsort(int left, int right){int pivot;if(left >= right) return;pivot = (left + right) / 2;swap(pivot, right);pivot = partition(left, right);qsort(left, pivot - 1);qsort(pivot + 1, right);}int main(){int T, M, N, K;int i, j, count, tmpnum, tsum, psum;scanf("%d", &T);while(T--){tsum = 0;psum = 0;count = 0;scanf("%d %d %d", &M, &N, &K);for(i = 1; i <= M; i ++){for(j = 1; j <= N; j ++){scanf("%d", &tmpnum);if(tmpnum){points[count].x = j;points[count].y = i;points[count ++].peaunts = tmpnum;}}}qsort(0, count - 1);i = 0;while(i < count){if(i == 0){tsum += points[i].y + 1;if((tsum + points[i].y) > K){break;} psum += points[i].peaunts;i ++;}else{tsum += abs(points[i].x - points[i - 1].x) + abs(points[i].y - points[i - 1].y) + 1; if((tsum + points[i].y) > K){break;} psum += points[i].peaunts;i ++;}}printf("%d\n", psum);}return 0;}
0 0
- 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 peanuts 排序
- pku 1928 The Peanuts
- POJ:1928 花生问题 The Peanuts
- fjnu 1013 The Peanuts
- hdu 1355 The Peanuts
- hdu1355 The Peanuts
- POJ1928 The Peanuts
- POJ1928 The Peanuts
- ZOJ2235 POJ1928 HDU1355 The Peanuts
- hdu 1355 The Peanuts(DP)
- HDOJ 1355 The Peanuts(水)
- C++11 新特性
- 如何去除UISearchBar默认的灰色背景颜色
- 编程常用英文单词缩写整理
- Tomcat默认的Session持久化的效果
- 《高质量C++编程指南》摘录8
- POJ 1928 The Peanuts
- 漫谈iOS程序的证书和签名机制
- 用Swift写一个发送邮件的iOS用户反馈
- mysql:day5-详解多线程状态下的事务(连接池、动态代理技术)
- 大三实习的工作总结。
- java抽象类、抽象方法
- QT多语言翻译工作
- python 变量命名规范
- 海量数据处理——倒排索引