poj 1928

题目:http://poj.org/problem?id=1928

直接模拟。。


下面是AC代码:

#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;   
  
}  


你可能感兴趣的:(poj 1928)