期末考试-垃圾炸弹(算法基础 第10周)

问题描述:
期末考试-垃圾炸弹(算法基础 第10周)_第1张图片
期末考试-垃圾炸弹(算法基础 第10周)_第2张图片
分析
最简单粗暴的方法,直接枚举每一个坐标点。
注意POJ上http://cxsjsx.openjudge.cn/2015finalpractice/33/ 这道题是只要求输入一个样例,Coursera要求输入多个样例。
注:http://www.cnblogs.com/Inkblots/p/4854052.html 这种方法能更简单一点,他旨只在垃圾的邻域枚举可能清扫的垃圾数。

源码

#include <iostream>
#include <vector>
using namespace std;

int d, n;
struct Rubbish{
    int x;
    int y;
    int counts;
};
int bestplot,maxrubbishs;
vector<Rubbish> v(21);

void enumerate() {
    bestplot=0; 
    maxrubbishs=0;
    for(int i=0; i<1025; i++) {
        for (int j=0; j<1025; j++) {
            int tem=0;
            for(int k=0; k<n; k++) {
                if (v[k].x<=i+d && v[k].x>=i-d && v[k].y<=j+d && v[k].y>=j-d) {
                    tem += v[k].counts;
                }
            }
            if (maxrubbishs<tem){
                maxrubbishs=tem;
                bestplot=1;
            }
            else if (maxrubbishs==tem){
                bestplot++;
            }
        }
    }
}

int main() {
    int T;
    cin >> T;
    while(T--) {
        cin >> d;
        cin >> n;
        for(int i=0; i<n; i++) {
            cin >> v[i].x >> v[i].y >>v[i].counts;
        }       
        enumerate();
        cout << bestplot << ' ' << maxrubbishs << endl;
    }
    return 0;
}

你可能感兴趣的:(期末考试-垃圾炸弹(算法基础 第10周))