P1086 [NOIP2004 普及组] 花生采摘 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
import java.awt.Checkbox;
import java.awt.PageAttributes.OriginType;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.math.MathContext;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner scanner=new Scanner(System.in);
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw1=new PrintWriter(System.out);
String[] aStrings=br1.readLine().split(" ");
int a=Integer.parseInt(aStrings[0]);
int b=Integer.parseInt(aStrings[1]);
int c=Integer.parseInt(aStrings[2]);
int d,e,f,jishu;
jishu=0;
for(d=1;d<=a;d++) {
String[] bStrings=br1.readLine().split(" ");
for(e=1;e<=b;e++) {
f=Integer.parseInt(bStrings[e-1]);
if(f!=0) {
aa1[jishu]=new huasheng(f, d, e);
jishu++;
}
}
}
Arrays.sort(aa1,0,jishu);
int g;
int answer=0;
int x1=0;
int y1=aa1[0].y;
int haoshi=0;
for(g=0;g<jishu;g++) {
haoshi=haoshi+Math.abs(aa1[g].x-x1)+Math.abs(aa1[g].y-y1)+1+aa1[g].x;
if(haoshi<=c) {
haoshi=haoshi-aa1[g].x;
x1=aa1[g].x;
y1=aa1[g].y;
answer=answer+aa1[g].number;
}
else {
break;
}
}
System.out.println(answer);
}
public static huasheng[] aa1=new huasheng[500];
}
class huasheng implements Comparable<huasheng>{
int number;
int x;
int y;
public huasheng(int number, int x, int y) {
super();
this.number = number;
this.x = x;
this.y = y;
}
@Override
public int compareTo(huasheng o) {
// TODO Auto-generated method stub
return o.number-this.number;
}
@Override
public String toString() {
return "huasheng [number=" + number + ", x=" + x + ", y=" + y + "]";
}
}