Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package javaapplication1;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.util.Scanner;
  11.  
  12. /**
  13. *
  14. * @author Maciek
  15. */
  16. public class JavaApplication1 {
  17.  
  18. public static void Search(int data[][], int total, int minValue, int n, int m, int locationX, int locationY, int prevDepth)
  19. {
  20.  
  21. if (data[locationY][ locationX] >= minValue && (
  22. (prevDepth < data[locationY][ locationX] && data[locationY][ locationX] < (prevDepth + 5) && data[locationY][ locationX] - prevDepth < 5) ||
  23. (prevDepth > data[locationY][ locationX] && data[locationY][ locationX] < (prevDepth + 5) && (prevDepth - data[locationY][ locationX]) < 5) ||
  24. data[locationY][ locationX] == prevDepth))
  25. {
  26. if (data[locationY][ locationX] - 5 >= minValue)
  27. {
  28. total += 5;
  29. }
  30. else
  31. {
  32. total += (data[locationY][ locationX] - minValue) + 1;
  33. }
  34. System.out.println(total);
  35.  
  36. int prev_depth = data[locationY][ locationX];
  37. data[locationY][ locationX] = 10000;
  38.  
  39.  
  40. if ((locationY - 1) >= 0)
  41. {
  42.  
  43. if (locationX + 1 < m && data[locationY - 1][ locationX + 1] != 10000)
  44. {
  45. Search( data, total, minValue, n, m, locationX + 1, locationY - 1, prev_depth);
  46. }
  47.  
  48. if (locationX - 1 >= 0 && data[locationY - 1][ locationX - 1] != 10000)
  49. {
  50. Search( data, total, minValue, n, m, locationX - 1, locationY - 1, prev_depth);
  51. }
  52. if (data[locationY - 1][ locationX] != 10000)
  53. {
  54. Search( data, total, minValue, n, m, locationX, locationY - 1, prev_depth);
  55. }
  56.  
  57. }
  58. if (locationY + 1 < n)
  59. {
  60.  
  61.  
  62. if (locationX + 1 < m && data[locationY + 1][ locationX + 1] != 10000)
  63. {
  64. Search( data, total, minValue, n, m, locationX + 1, locationY + 1, prev_depth);
  65.  
  66. }
  67. if (locationX - 1 >= 0 && data[locationY + 1][ locationX - 1] != 10000)
  68. {
  69. Search( data, total, minValue, n, m, locationX - 1, locationY + 1, prev_depth);
  70.  
  71. }
  72. if (data[locationY + 1][ locationX] != 10000)
  73. {
  74. Search( data, total, minValue, n, m, locationX, locationY + 1, prev_depth);
  75. }
  76. }
  77. if (locationX + 1 < m && data[locationY][ locationX + 1] != 10000)
  78. {
  79. Search( data, total, minValue, n, m, locationX + 1, locationY, prev_depth);
  80.  
  81. }
  82. if (locationX - 1 >= 0 && data[locationY][ locationX - 1] != 10000)
  83. {
  84. Search( data, total, minValue, n, m, locationX - 1, locationY, prev_depth);
  85. }
  86. }
  87. }
  88.  
  89. public static void main(String[] args) throws FileNotFoundException {
  90. Scanner sr = new Scanner(new File("in4.txt"));
  91. int total = 0;
  92.  
  93.  
  94. int n;
  95. int m;
  96. n = sr.nextInt();
  97.  
  98. m = sr.nextInt();
  99. int[][] data = new int[n][m];
  100.  
  101. for (int i = 0; i < n; i++)
  102. {
  103. for (int j = 0; j < m; j++)
  104. {
  105. data[i][j] = sr.nextInt();
  106. }
  107. }
  108.  
  109. int locationX ;
  110. int locationY ;
  111. locationX=sr.nextInt();
  112. locationY=sr.nextInt();
  113. int minValue =data[locationY - 1][ locationX - 1];
  114.  
  115.  
  116.  
  117. Search( data, total, minValue, n, m, locationX - 1, locationY-1, data[locationY - 1][ locationX - 1]);
  118. System.out.println(total);
  119. }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement