fork(5) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. short** newGrid(const short width, const short height)
  6. {
  7. short** g = NULL;
  8. g = new short*[width];
  9.  
  10. short temp = 0;
  11.  
  12. for(short i = 0; i < width; i++)
  13. {
  14. g[i] = new short[height];
  15. }
  16.  
  17. for(short i = 0; i < width; i++)
  18. {
  19. for(short k = 0; k < height; k++)
  20. {
  21. cin >> temp;
  22. g[i][k] = temp;
  23. }
  24. }
  25.  
  26. return g;
  27.  
  28. }
  29.  
  30. void solveGrid(const short gridNum,
  31. short* g[],
  32. const short width,
  33. const short height)
  34. {
  35. const short right = 1;
  36. const short farRight = 2;
  37. const short under = 1;
  38. const short farUnder = 2;
  39.  
  40. short X_coordinate = 0;
  41. short Y_coordinate = 0;
  42.  
  43. long tempTotal = 0;
  44. long total = 4294967295;
  45.  
  46. for(short i = 0; i < width - 2; i++)
  47. {
  48. for(short k = 0; k < height - 2; k++)
  49. {
  50.  
  51. cout << "(" << i << "," << " " << k << ")";
  52.  
  53. tempTotal =
  54. g[i][k] +
  55.  
  56. g[i][k + right] +
  57.  
  58. g[i][k + farRight] +
  59.  
  60. g[i + under][k] +
  61.  
  62. g[i + farUnder][k] +
  63.  
  64. g[i + under][k + right] +
  65.  
  66. g[i + under][k + farRight] +
  67.  
  68. g[i + farUnder][k + right] +
  69.  
  70. g[i + farUnder][k + farRight];
  71.  
  72. cout << " " << tempTotal << endl;
  73.  
  74. if(tempTotal < total)
  75. {
  76. total = tempTotal;
  77. X_coordinate = i;
  78. Y_coordinate = k;
  79. }
  80. }
  81. }
  82.  
  83. cout << "#" << gridNum << ":" << " " << "(" << X_coordinate <<
  84. "," << " " << Y_coordinate << ")" << " " << total
  85. << endl;
  86.  
  87. cout << endl;
  88.  
  89. return;
  90.  
  91. }
  92.  
  93. void deleteGrid(short* g[], const short width)
  94. {
  95. for(short i = 0; i < width; i++)
  96. {
  97. delete [] g[i];
  98. }
  99.  
  100. delete [] g;
  101.  
  102. return;
  103. }
  104.  
  105. int main()
  106. {
  107. short** g = NULL;
  108. short numGrids = 0, width = 0, height = 0;
  109. cin >> numGrids;
  110.  
  111. for(short gridCount = 0; gridCount < numGrids; gridCount++) //main loop for number of grids
  112. {
  113. cin >> width;
  114. cin >> height;
  115.  
  116. g = newGrid(width, height);
  117.  
  118. solveGrid(gridCount, g, width, height);
  119.  
  120. deleteGrid(g, width);
  121. }
  122.  
  123. return 0;
  124. }
Success #stdin #stdout 0s 3236KB
stdin
3
4 4
1 0 0 1
0 1 0 1
0 2 1 3
0 3 1 1
6 3
4 1 2 0 0 6
6 7 5 9 8 3
3 4 6 2 8 1
5 5 
1 0 0 3 1
0 1 0 1 1
0 2 1 4 3
0 0 0 1 4
0 0 0 1 3
stdout
(0, 0) 5
(0, 1) 9
(1, 0) 8
(1, 1) 13
#0: (0, 0) -1

(0, 0) 31
(1, 0) 44
(2, 0) 51
(3, 0) 44
#1: (0, 0) -1

(0, 0) 5
(0, 1) 12
(0, 2) 14
(1, 0) 4
(1, 1) 10
(1, 2) 15
(2, 0) 3
(2, 1) 9
(2, 2) 17
#2: (0, 0) -1