您的位置:首页 > 其它

【p1086-花生采摘】解题记录

2017-02-24 17:52 232 查看
原题点 这里

这是一道简单模拟题,没涉及什么算法。

只是需要自己建立一个地图坐标系的概念。

通常程序中习惯把原点定位左上角,右边为 x 轴正方向,下边为 y 轴正方向。只要把地图想明白,程序就很简单了。

代码:

#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;

int width, height, time, ans=0;

struct Unit{
int x, y, n;
bool operator<(const Unit u)const{
return this->n<u.n;
}
};

priority_queue<Unit> pq;

int main(){

scanf("%d%d%d", &height, &width, &time);

//printf("height=%d, width=%d, time=%d\n", height, width, time);

for(int y=0; y<height; ++y){
for(int x=0; x<width; ++x){
static int n;
scanf("%d", &n);
if(n>0){
pq.push((Unit){x, y, n});
}
}
}

time-=2;
Unit maxu= pq.top();
int posx= maxu.x, posy=0;
while(true){
maxu= pq.top();
pq.pop();
int to_time= abs(posx-maxu.x)+abs(posy-maxu.y);
int stay_time= 1;
int back_time= maxu.y;
int total_time= to_time + stay_time + back_time;

if(total_time>time){
break;
}
else{
time-= to_time+stay_time;
ans+= maxu.n;
posx= maxu.x;
posy= maxu.y;
}
if(pq.empty()) break;
}

printf("%d\n", ans);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: