首页 > 其他 > 详细

经典模拟问题--摘花生 POJ-1928

时间:2014-04-19 01:31:21      阅读:986      评论:0      收藏:0      [点我收藏+]

The Peanuts

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7327   Accepted: 3035

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.
bubuko.com,布布扣

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

2
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
6 7 20
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

Sample Output

37
28

解题思路:模拟人摘花生的过程,要注意两点

【1】进出大路的时候要算一步

【2】每次摘花生的时候花费一个时间

考虑极端情况:第一次就不成功。

源代码:

// 花生问题.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct pos{
	int x,y,valu;
}pos;
struct pos a[4100]={0};

bool cmp(pos a,pos b)
{
	return a.valu>b.valu;
}
int main()
{
	int num=0,n=0,m=0,k=0;
	while(scanf("%d",&num)!=EOF)
	{
		while(num--)
		{
			scanf("%d%d%d",&m,&n,&k);
			int temp=0,cnt=0;
			memset(a,0,sizeof(a));

			for(int i=0;i<m;i++)//横向
				for(int j=0;j<n;j++)//竖向
				{
					scanf("%d",&temp);
					if(temp)
					{
						a[cnt].x=j;a[cnt].y=i;a[cnt].valu=temp;
						cnt++;
					}
				}
				sort(a,a+cnt,cmp);
				int sum=0,time=0;
				for(int i=0;i<cnt;i++)
				{
					if(i==0)
					{
						time=2*a[0].y+3;//摘1 进出2
						if(time>k) break;//够不够往返
						sum+=a[0].valu;
						time=a[0].y+2;//进1 摘1
						continue;
  					}
					time+=abs(a[i].x-a[i-1].x)+abs(a[i].y-a[i-1].y)+1;//摘1
				    if(time+a[i].y+1>k) break;//要考虑能够回到大路上 出1
					sum+=a[i].valu;
				}

				printf("%d\n",sum);
		}
	}
	return 0;
}


 

经典模拟问题--摘花生 POJ-1928,布布扣,bubuko.com

经典模拟问题--摘花生 POJ-1928

原文:http://blog.csdn.net/linsheng9731/article/details/24042853

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!