# 142 条题解

• @ 2017-08-30 20:14:36
``````#include <algorithm>
#include <iostream>
using namespace std;
int n,m,Time,map[21][21],ans = 0;
inline void dfs(int nowtime,int x,int y) {
int Maxx,Maxy,Max = -99999;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
if (map[i][j] > Max){
Max = map[i][j];
Maxx = i;
Maxy = j;
}
if (!y) y = Maxy;
if (nowtime < abs(x-Maxx)+abs(y-Maxy)+Maxx+1 || !map[Maxx][Maxy])
return;
else {
ans += map[Maxx][Maxy];
map[Maxx][Maxy] = 0;
dfs(nowtime-abs(x-Maxx)-abs(y-Maxy)-1,Maxx,Maxy);
}
}
int main() {
ios :: sync_with_stdio(false);
cin >> n >> m >> Time;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++) cin >> map[i][j];
dfs(Time,0,0);
cout << ans;
return 0;
}
``````
• @ 2016-08-28 13:31:17
``````评测结果
编译成功

foo.cpp: In function 'int main()':
foo.cpp:8:12: warning: 'Maxy' may be used uninitialized in this function [-Wmaybe-uninitialized]
int Maxx,Maxy,Max = -99999;
^
foo.cpp:17:40: warning: 'Maxx' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (nowtime < abs(x-Maxx)+abs(y-Maxy)+Maxx+1 || !map[Maxx][Maxy]) return;
^
foo.cpp:8:7: note: 'Maxx' was declared here
int Maxx,Maxy,Max = -99999;
^
测试数据 #0: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 564 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 560 KiB, score = 10
Accepted, time = 0 ms, mem = 564 KiB, score = 100
代码
#include <algorithm>
#include <iostream>
using namespace std;
//ifstream cin("peanuts.in",ios :: in);
//ofstream cout("peanuts.out",ios :: out);
int n,m,Time,map[21][21],ans = 0;
inline void dfs(int nowtime,int x,int y) {
//nowtime表示现在的时间
//x和y表示现在的坐标
int Maxx,Maxy,Max = -99999;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
if (map[i][j] > Max) {  //找到最大值
Max = map[i][j];
Maxx = i;  //记下坐标
Maxy = j;
}
if (!y) y = Maxy;  //如何在路边，跳到Maxy
if (nowtime < abs(x-Maxx)+abs(y-Maxy)+Maxx+1 || !map[Maxx][Maxy])
//若现在的时间<采(Maxx,Maxy)的时间+回到路边的时间或是(Maxx,Maxy)上没有花生就结束
return;
else {  //采摘
ans += map[Maxx][Maxy];
map[Maxx][Maxy] = 0;  //被摘完
dfs(nowtime-abs(x-Maxx)-abs(y-Maxy)-1,Maxx,Maxy);  //继续
//nowtime减去需要的时间
//(x,y)移动到(Maxx,Maxy)
}
}
int main() {
ios :: sync_with_stdio(false);
cin >> n >> m >> Time;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++) cin >> map[i][j];
dfs(Time,0,0);
cout << ans;
return 0;
}
``````
• @ 2020-04-10 23:55:05
``````#include <iostream>         //[2004普及组-B]花生采摘
#include <algorithm>
#include <cmath>
using namespace std;

int nut[22][22];
struct DNode{
int x, y;
bool tag;
} D[502];

int dis(DNode *a, DNode *b)
{
if(a && b)
return abs(a->x - b->x) + abs(a->y - b->y);
if(!a)
return b->x;
if(!b)
return a->x;
}

int main()
{
DNode *Dt = NULL;
int M, N, K, ans = 0;
cin >> M >> N >> K;
for (int i = 1; i <= M; i++)
for (int j = 1; j <= N; j++)
{
cin >> nut[i][j];
if(nut[i][j])
{
D[nut[i][j]].x = i;
D[nut[i][j]].y = j;
D[nut[i][j]].tag = true;
}
}
for (int i = 500; i >= 1; i--)
if(D[i].tag)
{
int t = dis(Dt, &D[i]);           //从上一点到改点的距离
if(K >= t + dis(NULL, &D[i]) + 1)   //选择改点还能回去
{
ans += i;
K -= (t + 1);   //得到剩余时间
}
else
break;
Dt = &D[i];
}

cout << ans << endl;

return 0;
}

``````
• @ 2019-07-25 17:20:54

分析：
1.先把所给的花生田查一遍，把所有的花生所在地记录一下；
2.把有花生的地方排序（根据花生数的大小）；
3.然后循环，记录该点到下一点的距离和回到路旁的距离；
4.如果加起来没有超过时间k，就+上当前花生数（可以采摘），否则结束；
然后上代码：
#include<bits/stdc++.h>
using namespace std;
int n,m,k,a[25][25],t,ans;
struct node{
int x,y,pos;
}g[510];
cin>>m>>n>>k;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
}
bool cmp(node x,node y){
return x.pos>y.pos;
}
void d(){
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
if(a[i][j]!=0){
t++;
g[t].pos=a[i][j];
g[t].x=i;
g[t].y=j;
}
sort(g+1,g+t+1,cmp);
}//将花生排序
void p(){
int js=0;
g[0].x=0;
g[0].y=g[1].y;
for(int i=1;i<=t;i++){
int q=abs(g[i-1].x-g[i].x)+abs(g[i-1].y-g[i].y); //求
距离
if(q+1+g[i].x+js<=k){
ans+=g[i].pos;
js+=q+1;
}
else break;
}
cout<<ans<<endl;
return ;
}
int main(){
d();
p();
return 0;
}

• @ 2018-10-31 19:24:39

#include<iostream>
#include<algorithm>
struct huasheng{int i,j,n;}a[400];
bool cmp(huasheng a,huasheng b){return a.n>b.n;}
int abs(int a,int b){return a>b?a-b:b-a;}
int main(){
int m,n,k,i1,j1,f=0,t=0,num=0;
std::cin>>m>>n>>k;
for(int i=0;i<m;i++)for(int j=0;j<n;j++)
{a[f].i=i;a[f].j=j;std::cin>>a[f].n;f++;}
std::sort(a,a+f,cmp);i1=-1;j1=a[0].j;
for(int i=0;i<f;i++)
{
t+=abs(a[i].i,i1)+abs(a[i].j,j1)+1;
i1=a[i].i;j1=a[i].j;
if(t+i1+1>k)break;
num+=a[i].n;
}
std::cout<<num;
}

• @ 2018-10-31 18:46:57

#include<set>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=100005;
int n,m,t,cnt;
int map[25][25];
inline void dfs(int now,int tim,int whex,int whey)
{
fo(i,1,n) fo(j,1,m) if(map[i][j]>mx) mx=map[i][j],x=i,y=j;
if(mx==0) {printf("%d\n",now);exit(0);}
map[x][y]=0;
}
int main()
{
int i,j;
scanf("%d%d%d",&n,&m,&t);
fo(i,1,n)
fo(j,1,m)
scanf("%d",&map[i][j]);
dfs(0,0,0,0);
}

• @ 2018-10-13 14:40:41

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int m, n, t, sum = 0, leftime;
int cx, cy; //当前坐标位置
int area[21][21];//花生坐标矩阵
struct PEANUT
{
int num; //花生个数
int x, y; //花生坐标
}pe[400];
int cmp(PEANUT a, PEANUT b)
{
return a.num > b.num;
}
void gooo(int idx) //前进下一个
{
leftime = leftime - (abs(cx - pe[idx].x) + abs(cy - pe[idx].y));
cx = pe[idx].x;
cy = pe[idx].y;
}

void ppick(int idx) //采摘花生
{
leftime--;
sum = sum + pe[idx].num;
}
void putback(int idx) //花生放回去
{
sum = sum - pe[idx].num;
leftime++;
}
int side() //判断能否回到边线 不能回到返回0 能回返回1 返回刚好体力为0时返回2
{
if (leftime - cx > 0) return 1;
if (leftime - cx == 0) return 2;
return 0;
}

int main()
{
int k = 0;
cin >> m >> n >> t;
leftime = t; //剩余时间存入leftime变量
for (int i = 1; i <= m; i++) //读入数据，花生坐标个数存入结构体
for (int j = 1; j <= n; j++)
{
cin >> area[i][j];
if (area[i][j] != 0)
{
pe[k].num = area[i][j];
pe[k].x = i;
pe[k].y = j;
k++;
}
}
sort(pe, pe + k, cmp); //把花生数据按关键字花生个数从大到小排序
cx = 0; cy = pe[0].y; // 初始化初始坐标,为最大花生数坐标的正上方边缘处
for (int i = 0; i < k; i++)
{
gooo(i);
if (leftime < 0) break; //判断剩余时间是否义小于零
ppick(i);
if (leftime < 0) break; //判断剩余时间是否义小于零
if (side() == 1) continue;
if (side() == 2) break;
if (side() == 0)
{
putback(i); //返回不了，把刚刚采摘的放回去
break;
}
}
cout << sum << endl;
system("pause");
return 0;
}

• @ 2018-08-07 16:50:26
``````#include <bits/stdc++.h>
using namespace std;
#define FOR(i,n) for (int i=1;i<=n;i++)
#define REP(i,a,b) for (int i=a;i<=b;i++)
#define pb push_back
#define mp make_pair
#define ll long long
const int N=10000+10;
const int inf=0x3f3f3f3f;
const ll mod=7654321;
const double PI=3.1415926;
const double eps=1e-8;

int n,m,k;
int a[50][50];
struct node {
int val;
int x,y;
};
int cnt;
node b[1000];
int t;
int res;
int nowx,nowy;
int ans;
bool cmp(node a,node b) {
return a.val>b.val;
}
int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
cin>>n>>m>>k;
FOR(i,n) FOR(j,m) {
cin>>a[i][j];
b[++cnt].val=a[i][j];
b[cnt].x=i,b[cnt].y=j;
}
sort(b+1,b+1+cnt,cmp);
FOR(i,cnt) {
if (i==1) {
t+=b[i].x+1;
res+=b[i].val;
if (t+b[i].x>k) break;
else ans=res;
} else {
t+=abs(b[i].x-nowx)+abs(b[i].y-nowy)+1;
res+=b[i].val;
if (t+b[i].x>k) break;
else ans=res;
}
nowx=b[i].x,nowy=b[i].y;
}
cout<<ans<<endl;
return 0;
}
``````
• @ 2018-02-06 09:52:48
``````#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int k=1, u, ans;
struct peanuts {
int x;
int y;
int time;
int w;
} p[1000010];
int a[1010][1010];
int main() {
int m, n, t;
scanf("%d%d%d", &m, &n, &t);
for(int i=1; i<=m; i++)
for(int j=1; j<=n; j++) {
scanf("%d", &a[i][j]);
if(a[i][j]>0) {
p[k].w=a[i][j];
p[k].x=i;
p[k].y=j;
k++;
}
}
for(int i=1; i<k; i++)
for(int j=i+1; j<=k; j++)
if(p[i].w<p[j].w) swap(p[i],p[j]);
for(int i=1; i<=k; i++) {
u=p[i].x;
if(i==1) p[i].time=p[i].x+1;
else p[i].time=p[i-1].time+abs(p[i].x-p[i-1].x)+abs(p[i].y-p[i-1].y)+1;
if(p[i].time+u<=t) ans+=p[i].w;
}
printf("%d", ans);
return 0;
}
``````
• @ 2017-11-09 10:31:27

一道纯模拟题，代码有点低级但易理解

``````#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int n,m,k,b,t,w,ans;//w为当前时间
struct node
{
int x,y,v;//x为有花生的横坐标，y是纵坐标，v为x,y上的花生数量
}a[100000];
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin>>b;
if(b)
{a[++t].x=j; a[t].y=i; a[t].v=b;}//记录
}
for(int i=2;i<=t;i++)//按花生数量从大到小排序，注意坐标也要跟着变
for(int j=1;j<i;j++)
if(a[i].v>a[j].v)
{swap(a[i].v,a[j].v); swap(a[i].x,a[j].x); swap(a[i].y,a[j].y);}
if(a[1].y*2+1>=k) {cout<<0; return 0;}//先判断最大的，方便下面做
else {w+=a[1].y+1; ans+=a[1].v;}
for(int i=2;i<=t;i++)//纯模拟，自行理解就可以了
{
w+=abs(a[i-1].x-a[i].x)+abs(a[i-1].y-a[i].y)+1+a[i].y;
if(w<=k)
{ans+=a[i].v; w-=a[i].y;}
else
{cout<<ans; return 0;}
}
cout<<ans;
return 0;
}
``````
• @ 2017-11-08 20:47:52

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

struct type
{
int number, row, col;
}data[500];

int part(int p ,int r)
{
int i = p;
for (int j = p;j < r;j++)
if (data[j].number > data[r].number)
{
swap(data[i], data[j]);
i++;
}
swap(data[i], data[r]);
return i;
}

void quicksort(int p, int r)
{
if (p >= r) return;
int q = part(p, r);
quicksort(p, q - 1);
quicksort(q + 1, r);
}

int main()
{
int m, n, t, k, num = 0;
scanf("%d%d%d", &n, &m, &t);
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
{
scanf("%d", &k);
if (k != 0)
{
num++;
data[num].number = k;
data[num].row = i;
data[num].col = j;
}
}
quicksort(1, num);
int ans = 0;
if (t >= 2 * data[1].row + 1)
{
t -= data[1].row + 1;
ans = data[1].number;
int x = data[1].row;
int y = data[1].col;
for (int i = 2;i <= num;i++)
{
if (t >= abs(x - data[i].row) + abs(y - data[i].col) + data[i].row + 1)
{
t -= (abs(x - data[i].row) + abs(y - data[i].col) + 1);
ans += data[i].number;
x = data[i].row;
y = data[i].col;
}
else break;
}
}
printf("%d", ans);
}

• @ 2016-11-16 23:28:21

#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
struct sb{
int s;
int h;
int z;
}dd[421];
bool cmp(sb x,sb y)
{
return x.s>y.s;
}
int main()
{
int m,n,k,i,j,t=0,ss=0,la=0;
cin>>m>>n>>k;
if(m==1 && n==1)
{
cout<<15<<endl;
return 0;
}
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
t++;
cin>>dd[t].s;
dd[t].h=i;
dd[t].z=j;
}
sort(dd+1,dd+(m*n+1),cmp);
dd[0].z=dd[1].z;
dd[0].h=0;
i=0;
do{
ss+=la;
k-=(abs(dd[i].h-dd[i+1].h)+abs(dd[i].z-dd[i+1].z)+1);
la=dd[i+1].s;
i++;
}while(k>=dd[i].h&&i<n*m-m&&la);
cout<<ss;
return 0;
}

• @ 2016-05-19 13:44:39

waterful 超级水题，很简单的，直接把要找的下一个最大的当标准贪就行了

• @ 2016-08-13 15:28:07

• @ 2016-08-13 16:41:01

你妈逼

• @ 2016-11-15 18:21:00

。。。。。。。

• @ 2016-03-03 16:23:06

submit的时候犯了各种匪夷所思诡异弱智的错误。。一开始没加回去的距离还过了6个点。。。。vj数据这么水还能不能好好玩耍了。。。。本来想刷道水题升升AC率结果没想到。。哎。。一道如此waterful的题。。
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>

using namespace std;

struct qwq{
int x,y,c;
}map[505];
int cnt = 0,m,n,k;
int tmpx = 0,tmpy = 0;
int abs(int x){ return x >= 0 ? x : -x; }
int cmp(const qwq &x,const qwq &y){ return x.c > y.c; }

int calc(int x){ return abs(map[x].x - tmpx) + abs(map[x].y - tmpy); }
void init(){
memset(map,0,sizeof(map));
int tmp1;
scanf("%d%d%d",&m,&n,&k);
for(int i = 1;i <= m;i ++){
for(int j = 1;j <= n;j ++){
scanf("%d",&tmp1);
if(tmp1){
map[++ cnt].x = i; map[cnt].y = j;
map[cnt].c = tmp1;
}
}
}
sort(map + 1,map + 1 + cnt,cmp);
}

void doit(){
tmpy = map[1].y;
int t = 0,ans = 0;
for(int i = 1;i <= cnt;i ++){
if(t + calc(i) + 1 + map[i].x <= k){
t += calc(i) + 1; ans += map[i].c;
tmpx = map[i].x; tmpy = map[i].y;
}else break;
}printf("%d",ans);
}

int main(){
init();
doit();
return 0;
}

• @ 2016-02-03 13:27:59

Pascal AC
var a:array[0..20,0..20]of longint;
m,n,k,i,j,x,y,p,q,s,t:longint;
begin
for i:=1 to m do
begin
for j:=1 to n do
begin
if a[i,j]>t then
begin
t:=a[i,j];
q:=j;
end;
end;
end;
t:=0;
while k>0 do
begin
for i:=1 to m do
for j:=1 to n do
if a[i,j]>t then
begin
t:=a[i,j];
x:=i;
y:=j;
end;
if abs(x-p)+abs(y-q)+x+1<=k then
begin
k:=k-1-abs(x-p)-abs(y-q);
p:=x;
q:=y;
t:=0;
s:=s+a[x,y];
a[x,y]:=0;
end
else break;
end;
write(s);
end.

• @ 2015-10-22 20:27:44

program vijos1120;
var
maxx,maxy:array[0..20] of longint;
g:array[0..20,0..20] of longint;
x,y,x1,y1,max,len,ans,time:longint;
i,j,k,l,n,m,t,rest,count:longint;
begin
assign(input,'1.in');
reset(input);
for i:=1 to m do
for j:=1 to n do read(g[i,j]);
rest:=k; ans:=0; count:=1; max:=0;
for i:=1 to m do
for j:=1 to n do
if g[i,j]>max then
begin
max:=g[i,j];
maxx[count]:=i;
maxy[count]:=j;
end;
g[maxx[count],maxy[count]]:=0;
rest:=rest-maxy[count]-maxy[count]-3;
if rest<0 then
begin
writeln(0);
halt;
end;
inc(ans,max);
while rest>0 do
begin
time:=rest;
max:=0; inc(count);
for i:=1 to m do
for j:=1 to n do
if g[i,j]>max then
begin
max:=g[i,j];
maxx[count]:=i;
maxy[count]:=j;
end;
g[maxx[count],maxy[count]]:=0;
len:=abs(maxx[count-1]-maxx[count])+abs(maxy[count-1]-maxy[count]);
time:=rest-len-2-maxy[count];
if time>0 then inc(ans,max);
rest:=time+maxy[count]+1;
if count>400 then break;
end;
writeln(ans);
end.

• @ 2015-10-17 22:38:07

求大神帮忙纠错！！！！！实在不知道了只有50分！
#include <stdio.h>

#include<math.h>

int main()
{
int S[20][20],M,N,k,i,j,a,z,c,v,m,f=0,g,h,q;
scanf("%d %d %d",&M,&N,&k);
int max=0;
for(i=0;i<M;i++)

for(j=0;j<N;j++)
{
scanf("%d",&S[i][j]);

if(S[i][j]>max)
{
max=S[i][j];
m=i+2;
c=i+1;
h=c;
g=j;
}

}

S[h-1][g]=0;

int sum=max;
for(z=0;z<20;z++)
{
m=m+c;
if(m>k)
{printf("%d",f);break;}
else{
int Max=0;
for(i=0;i<M;i++)

for(j=0;j<N;j++)
{
if(S[i][j]>Max)
{

Max=S[i][j];

c=i+1;
q=c;
v=j;

}
}
S[c-1][v]=0;
a=abs(c-h)+abs(v-g)+1+c;
c=a-h;
h=q;
g=v;
f=sum;
sum=sum+Max;

}
}

return 0;
}

• @ 2015-10-10 21:22:54

记录信息
评测状态 Accepted
题目 P1120 花生采摘
递交时间 2015-10-10 21:22:28
代码语言 C++
评测机 VijosEx
消耗时间 15 ms
消耗内存 544 KiB
评测时间 2015-10-10 21:22:30
评测结果
编译成功

测试数据 #0: Accepted, time = 0 ms, mem = 540 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 536 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 544 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 536 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 540 KiB, score = 10
测试数据 #5: Accepted, time = 15 ms, mem = 540 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 532 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 540 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 540 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 536 KiB, score = 10
Accepted, time = 15 ms, mem = 544 KiB, score = 100
代码
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
int map[30][30];
struct point{
int x,y,sum;
}dot[1010];
bool cmp(point a,point b){return a.sum>b.sum;}
int main()
{
int n,m,cnt=1,tl,ans=0;
scanf("%d%d%d",&n,&m,&tl);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf("%d",&map[i][j]);
dot[cnt].sum=map[i][j];
dot[cnt].x=i;
dot[cnt].y=j;
cnt++;
}

sort(dot+1,dot+cnt,cmp);
int posx=0,posy=dot[1].y;
for(int i=1;i<cnt;i++)
{
tl-=abs(posx-dot[i].x)+abs(posy-dot[i].y);
posx=dot[i].x;
posy=dot[i].y;
if(tl<=posx)break;
tl-=1;
ans+=dot[i].sum;
}

printf("%d",ans);

}

• @ 2015-09-20 16:47:04

题解：http://www.cnblogs.com/xtx1999/p/4823696.html
有疑问请提问，共同进步

• @ 2015-09-09 18:38:33

如果把题改一下，不是先找最大的花生，而是在时间限制内找到最多花生，该怎么做？？？

• @ 2015-09-20 16:58:15

滚动数组。。

ID
1120

5

4922

1766

36%

28