直接模擬。。
下面是AC代碼:
[cpp]
#include <stdio.h>
int main(){
int t;
int row, col;
int time;
int l;
scanf("%d",&t);
for(l=1;l<=t;l++){
scanf("%d %d %d", &row, &col, &time);
int i,j;
int curPi = 0, curPj;
int peanuts[51][51];
int timeUsed = 0;
int peaGot = 0;
for(i = 1; i <= row; i++){
for(j = 1; j <= col; j++){
scanf("%d", &peanuts[i][j]);
}
}
int maxPi = 0, maxPj = 0;
while(timeUsed <= time)
{
int max = 0;
for(i = 1; i <= row; i++)
{
for(j = 1; j <= col; j++)
{
if(peanuts[i][j] > max)
{
max = peanuts[i][j];
maxPi = i;
maxPj = j;
}
}
}
if(max == 0)
break;
if(curPi == 0)
curPj = maxPj;
if(timeUsed + (abs(maxPj - curPj) + abs(maxPi - curPi) + 1 + maxPi) <= time){
timeUsed = timeUsed + abs(maxPj - curPj) + abs(maxPi - curPi) + 1;
curPi = maxPi;
curPj = maxPj;
peaGot += peanuts[curPi][curPj];
peanuts[curPi][curPj]=0;
}
else
break;
}
printf("%d\n", peaGot);
}
return 0;
}