【NOIP2004】【Luogu1086】花生采摘(枚举,给定顺序的模拟)

problem

emm,大致这样,不过还是有点,,解释不清楚。

  • 给定一个n行m列的网格,每格有一个价值。
  • 从第一行的任意一个格子开始,每次跳到(多次转移)剩余格子中价值最大的那个并获得价值。相邻格子转移代价为1,每次转移只能到相邻四个格子。
  • 求总代价为k的情况下能获得多少价值(最后要回到第一行)

关于第一行的补充:在原题的马路上新建第0行,表示这里的第一行?(雾,,

solution

题解:
直接将每个有花生的点按照花生数量进行排序。然后 按照花生数量从大到小进行枚举,对于 i 号点的花生,若多多采了这个花生后还能回到路边,那么采之;否则就结束枚举。
注意:
1、每次只能拿剩下花生株中最大的。所以不是组合问题!,顺序已经定了,是模拟!。
2、没有两株花生株的花生数目相同(不存在排序后相同数量的决策问题)。

codes

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 20;
struct node{int x, y, z; }a[maxn*maxn+50];int len;
bool cmp(node a, node b){return a.z>b.z;}
int main(){
    int n, m, k;
    cin>>n>>m>>k;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            int t;  cin>>t;
            if(t!=0)a[++len] = node{i,j,t};
        }
    }
    sort(a+1,a+len+1,cmp);
    //第一株要特判因为不是原点开始走,在马路上是不要时间的。
    int res = k-(a[1].x+1), ans = a[1].z;
    if(a[1].x*2+1 > k){ cout<<0<<'\n'; return 0;}//可能一株也摘不了
    for(int i = 2; i <= len; i++){
        //剩余时间能够跑到下一株+摘下来+跑到马路那就摘
        if(res >= abs(a[i].x-a[i-1].x)+abs(a[i].y-a[i-1].y)+abs(a[i].x)+1){
            ans += a[i].z;  res -= abs(a[i].x-a[i-1].x)+abs(a[i].y-a[i-1].y)+1;
        }else break;
    }
    cout<<ans<<'\n';
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33957603/article/details/81178096