POJ 1928 The Peanuts

 1 #include <iostream>

 2 #include <math.h>

 3 #define N 100

 4 using namespace std;

 5 

 6 int field[N][N];

 7 

 8 int main()

 9 {

10 int m, n, k, t, i, j;

11 scanf("%d", &t);

12 while (t--)

13 {

14 scanf("%d%d%d", &m, &n, &k);

15 for (i=1; i<=m; i++)

16 for (j=1; j<=n; j++)

17 scanf("%d", &field[i][j]);

18 int tTime=0, tPea=0;

19 int curi=0, curj;

20 while (tTime < k)

21 {

22 int maxi=0, maxj=0, max=0;

23 for (i=1; i<=m; i++)

24 for (j=1; j<=n; j++)

25 if (max < field[i][j])

26 {

27 max = field[i][j];

28 maxi = i;

29 maxj = j;

30 }

31 if (max == 0)

32 break;

33 if (curi == 0)

34 curj = maxj;

35 if (tTime + maxi + 1 + abs(curi-maxi) + abs(curj-maxj) <= k)

36 {

37 tTime += 1 + abs(curi-maxi) + abs(curj-maxj);

38 tPea += max;

39 curi = maxi; curj = maxj;

40 field[maxi][maxj] = 0;

41 }

42 else

43 break;

44 }

45 printf("%d\n", tPea);

46 }

47 return 0;

48 }

你可能感兴趣的:(poj)