算法1
代码
package 模拟;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
class tuplex{
int num;
int x;
int y;
public tuplex(int num, int x, int y) {
super();
this.num = num;
this.x = x;
this.y = y;
}
}
public class 花生采摘 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String []p=bufferedReader.readLine().split(" ");
int m=Integer.parseInt(p[0]);
int n=Integer.parseInt(p[1]);
int k=Integer.parseInt(p[2]);
int a[][]=new int [m+10][n+10];
Queue<tuplex> queue = new PriorityQueue<tuplex>(500,new Comparator<tuplex>() {
@Override
public int compare(tuplex x, tuplex xx) {
// TODO Auto-generated method stub
return x.num>xx.num?-1:1;
}
});
for(int i=1;i<=m;i++){
p=bufferedReader.readLine().split(" ");
for(int j=1;j<=n;j++){
a[i][j]=Integer.parseInt(p[j-1]);
if(a[i][j]!=0){
queue.offer(new tuplex(a[i][j], i, j));
}
}
}
int t=0;
int res=0;
int c=0;
int lastx=0,lasty=0,x=0,y=0;
while(!queue.isEmpty() )
{
tuplex tuplex=queue.poll();
x=tuplex.x;
y=tuplex.y;
c=tuplex.num;
if(t==0){
t=2*x+1;
if(t>k) break;
else {
t=t-x;
res+=c;
lastx=x;
lasty=y;
}
}else {
t+=Math.abs(x-lastx)+Math.abs(y-lasty)+1;
if(t+x>k) break;
else {
res+=c;
lastx=x;
lasty=y;
}
}
}
System.out.println(res);
}
}