洛谷P1086 花生采摘【模拟】

题目描述

鲁宾逊先生有一只宠物猴,名叫多多。这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!――熊字”。

鲁宾逊先生和多多都很开心,因为花生正是他们的最爱。在告示牌背后,路边真的有一块花生田,花生植株整齐地排列成矩形网格(如图111)。有经验的多多一眼就能看出,每棵花生植株下的花生有多少。为了训练多多的算术,鲁宾逊先生说:“你先找出花生最多的植株,去采摘它的花生;然后再找出剩下的植株里花生最多的,去采摘它的花生;依此类推,不过你一定要在我限定的时间内回到路边。”

我们假定多多在每个单位时间内,可以做下列四件事情中的一件:

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

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

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

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

现在给定一块花生田的大小和花生的分布,请问在限定时间内,多多最多可以采到多少个花生?注意可能只有部分植株下面长有花生,假设这些植株下的花生个数各不相同。

例如在图2所示的花生田里,只有位于(2,5),(3,7),(4,2),(5,4)(2, 5), (3, 7), (4, 2), (5, 4)(2,5),(3,7),(4,2),(5,4)的植株下长有花生,个数分别为13,7,15,913, 7, 15, 913,7,15,9。沿着图示的路线,多多在212121个单位时间内,最多可以采到373737个花生。

输入输出格式

输入格式:

第一行包括三个整数,M,NM, NM,N和KKK,用空格隔开;表示花生田的大小为M×N(1≤M,N≤20)M \times N(1 \le M, N \le 20)M×N(1M,N20),多多采花生的限定时间为K(0≤K≤1000)K(0 \le K \le 1000)K(0K1000)个单位时间。接下来的MMM行,每行包括NNN个非负整数,也用空格隔开;第i+1i + 1i+1行的第jjj个整数Pij(0≤Pij≤500)P_{ij}(0 \le P_{ij} \le 500)Pij(0Pij500)表示花生田里植株(i,j)(i, j)(i,j)下花生的数目,000表示该植株下没有花生。

输出格式:

一个整数,即在限定时间内,多多最多可以采到花生的个数。

输入输出样例

输入样例#1: 复制
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
输出样例#1: 复制
37
输入样例#2: 复制
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
输出样例#2: 复制
28

说明

noip2004普及组第2题

题意:

给定n*m的田,每个位置有一定的花生。每次取花生数最多的,每一个时间只能走一格或者采一个地方的花生。

问在给定时间内从路边出发回到路边最多能采多少花生。

思路:

模拟。

 1 //#include<bits/stdc++>
 2 #include<stdio.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<stdlib.h>
 7 #include<queue> 
 8 
 9 #define LL long long
10 #define ull unsigned long long
11 #define inf 0x3f3f3f3f 
12 
13 using namespace std;
14 
15 int n, m, t;
16 const int maxn = 25;
17 int peanut[maxn][maxn];
18 struct node{
19     int x, y;
20     int num;
21     
22     bool operator < (const node b) const
23     {
24         return num < b.num;
25     }
26     node(){}
27     node(int a, int b, int c)
28     {
29         x = a;
30         y = b;
31         num = c; 
32     } 
33 };
34 priority_queue<node> que;
35 
36 int main()
37 {
38     scanf("%d%d%d", &m, &n, &t);
39     for (int i = 1; i <= m; i++){
40         for(int j = 1; j <= n; j++){
41             scanf("%d", &peanut[i][j]);
42             if(peanut[i][j]){
43                 que.push(node(i, j, peanut[i][j]));
44             }
45         }
46     }
47     
48     int now_time = 0;
49     int now_x = 0, now_y = 0;
50     int ans = 0;
51     while(!que.empty()){
52         node go = que.top();que.pop();
53         int tmp_time;
54         if(now_time == 0)tmp_time = go.x;
55         else tmp_time = abs(go.x - now_x) + abs(go.y - now_y);
56         //now_time += tmp_time + 1 + go.x;
57         if(now_time + tmp_time + 1 + go.x <= t){
58             now_x = go.x;
59             now_y = go.y;
60             ans += go.num;
61             now_time += tmp_time + 1;
62         } 
63         else{
64             break;
65         }
66     }
67     printf("%d\n", ans);
68 }

猜你喜欢

转载自www.cnblogs.com/wyboooo/p/10260166.html