Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.27 KB | None | 0 0
  1. package trasa;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.Comparator;
  11. import java.util.PriorityQueue;
  12. import java.util.Scanner;
  13.  
  14. public class Trasa {
  15.  
  16. public static void main(String[] args) throws FileNotFoundException, IOException {
  17. int n;
  18. int m;
  19. int startX;
  20. int startY;
  21. int finishX;
  22. int finishY;
  23. int bufX;
  24. int bufY;
  25. int[] movingTab = new int[]{-1, 0, 1};
  26. ArrayList<Integer> wayS = new ArrayList<Integer>();
  27. ArrayList<Integer> wayL = new ArrayList<Integer>();
  28.  
  29. Scanner input = new Scanner(new File("src/in.txt"));
  30. File output = new File("src/out.txt");
  31. FileWriter fWriter = new FileWriter(output);
  32. PrintWriter pWriter = new PrintWriter(fWriter);
  33. n = input.nextInt();
  34. m = input.nextInt();
  35. int[][] data = new int[n][m];
  36. boolean[][] visits = new boolean[n][m];
  37. int[][] cost = new int[n][m];
  38. Comparator<DataNode> comparator = new CostComparator();
  39. PriorityQueue<DataNode> costMin = new PriorityQueue<>(n * m, comparator);
  40.  
  41. for (int i = 0; i < n; i++) {
  42. for (int j = 0; j < m; j++) {
  43. data[i][j] = input.nextInt();
  44. }
  45. }
  46. startX = input.nextInt();
  47. startY = input.nextInt();
  48. finishX = input.nextInt();
  49. finishY = input.nextInt();
  50. for (int i = 0; i < n; i++) {
  51. for (int j = 0; j < m; j++) {
  52. cost[i][j] = Integer.MAX_VALUE;
  53. }
  54. }
  55. cost[startX - 1][startY - 1] = 0;
  56. visits[startX - 1][startY - 1] = true;
  57. int[][][] prev = new int[n][m][2];
  58. for (int i = 0; i < n; i++) {
  59. for (int j = 0; j < m; j++) {
  60. for (int k = 0; k < 2; k++) {
  61. prev[i][j][k] = -1;
  62. }
  63. }
  64. }
  65. bufX = startX - 1;
  66. bufY = startY - 1;
  67. input.close();
  68.  
  69. shortWay(data, cost, prev, movingTab, costMin, visits, bufX, bufY);
  70. System.out.print(cost[finishX - 1][finishY - 1]+" ");
  71.  
  72. int posX = finishX - 1;
  73. int posY = finishY - 1;
  74. wayToString(prev, posX, posY, startX - 1, startY - 1, wayS);
  75. Collections.reverse(wayS);
  76.  
  77.  
  78. read(data, startX, startY, finishX, finishY, cost, visits, bufX, bufY);
  79.  
  80. for (int i = 0; i < n; i++) {
  81. for (int j = 0; j < m; j++) {
  82. cost[i][j] = 0;
  83. }
  84. }
  85. for (int i = 0; i < n; i++) {
  86. for (int j = 0; j < m; j++) {
  87. for (int k = 0; k < 2; k++) {
  88. prev[i][j][k] = -1;
  89. }
  90. }
  91. }
  92.  
  93. longWay(data, cost, prev, movingTab, costMin, visits, bufX, bufY);
  94.  
  95. System.out.println(cost[finishX - 1][finishY - 1]);
  96. posX = finishX - 1;
  97. posY = finishY - 1;
  98. wayToString(prev, posX, posY, startX - 1, startY - 1, wayL);
  99. Collections.reverse(wayL);
  100. System.out.println(wayS);
  101. System.out.println(wayL);
  102.  
  103. }
  104.  
  105. private static void read(int[][] data, int startX, int startY, int finishX, int finishY, int[][] cost, boolean[][] visits, int bufX, int bufY) throws FileNotFoundException {
  106. Scanner input = new Scanner(new File("src/in.txt"));
  107. int n = input.nextInt();
  108. int m = input.nextInt();
  109. for (int i = 0; i < n; i++) {
  110. for (int j = 0; j < m; j++) {
  111. data[i][j] = input.nextInt();
  112. }
  113. }
  114. startX = input.nextInt();
  115. startY = input.nextInt();
  116. finishX = input.nextInt();
  117. finishY = input.nextInt();
  118. for (int i = 0; i < n; i++) {
  119. for (int j = 0; j < m; j++) {
  120. cost[i][j] = Integer.MAX_VALUE;
  121. visits[i][j] = false;
  122. }
  123. }
  124. cost[startX - 1][startY - 1] = 0;
  125. visits[startX - 1][startY - 1] = true;
  126. int[][][] prev = new int[n][m][2];
  127. for (int i = 0; i < n; i++) {
  128. for (int j = 0; j < m; j++) {
  129. for (int k = 0; k < 2; k++) {
  130. prev[i][j][k] = -1;
  131. }
  132. }
  133. }
  134. bufX = startX - 1;
  135. bufY = startY - 1;
  136. }
  137.  
  138. private static class DataNode<Integer> {
  139.  
  140. DataNode(int thecost, int x, int y) {
  141. cost = thecost;
  142. corX = x;
  143. corY = y;
  144. }
  145. int cost;
  146. int corX;
  147. int corY;
  148. }
  149.  
  150. private static class CostComparator implements Comparator<DataNode> {
  151.  
  152. @Override
  153. public int compare(DataNode t, DataNode t1) {
  154. if (t.cost < t1.cost) {
  155. return -1;
  156. }
  157. if (t.cost > t1.cost) {
  158. return 1;
  159. }
  160. return 0;
  161.  
  162. }
  163. }
  164.  
  165. public static boolean areAllTrue(boolean[][] tab) {
  166. for (boolean[] b : tab) {
  167. for (boolean c : b) {
  168. if (!c) {
  169. return false;
  170. }
  171. }
  172. }
  173. return true;
  174. }
  175.  
  176. private static void shortWay(int[][] data, int[][] cost, int[][][] prev, int[] movingTab, PriorityQueue<DataNode> costMin, boolean[][] visits, int bufX, int bufY) {
  177. int buffMath;
  178. int movedBufX;
  179. int movedBufY;
  180. while (areAllTrue(visits) == false) {
  181. for (int i = 0; i < 3; i++) {
  182. for (int j = 0; j < 3; j++) {
  183. if (movingTab[i] == 0 && movingTab[j] == 0) {
  184. continue;
  185. }
  186. try {
  187.  
  188. movedBufX = bufX + movingTab[i];
  189. movedBufY = bufY + movingTab[j];
  190.  
  191. if (visits[movedBufX][movedBufY] == false) {
  192.  
  193. buffMath = (int) Math.pow((data[bufX + movingTab[i]][bufY + movingTab[j]] - data[bufX][bufY]), 2);
  194.  
  195. if (cost[movedBufX][movedBufY] > cost[bufX][bufY] + buffMath) {
  196. cost[movedBufX][movedBufY] = (cost[bufX][bufY] + buffMath);
  197.  
  198. prev[movedBufX][movedBufY][0] = bufX;
  199. prev[movedBufX][movedBufY][1] = bufY;
  200. costMin.add(new DataNode(cost[movedBufX][movedBufY], movedBufX, movedBufY));
  201. }
  202. }
  203. } catch (Exception e) {
  204. continue;
  205. }
  206. }
  207. }
  208. bufX = costMin.peek().corX;
  209. bufY = costMin.peek().corY;
  210. visits[costMin.peek().corX][costMin.peek().corY] = true;
  211. costMin.remove();
  212. }
  213. }
  214.  
  215. private static void longWay(int[][] data, int[][] cost, int[][][] prev, int[] movingTab, PriorityQueue<DataNode> costMin, boolean[][] visits, int bufX, int bufY) {
  216. int buffMath;
  217. int movedBufX;
  218. int movedBufY;
  219. while (true) {
  220. for (int i = 0; i < 3; i++) {
  221. for (int j = 0; j < 3; j++) {
  222. if (movingTab[i] == 0 && movingTab[j] == 0) {
  223. continue;
  224. }
  225. try {
  226.  
  227. movedBufX = bufX + movingTab[i];
  228. movedBufY = bufY + movingTab[j];
  229.  
  230. if (data[movedBufX][movedBufY] - data[bufX][bufY] >= 0) {
  231. buffMath = (int) Math.pow((data[bufX + movingTab[i]][bufY + movingTab[j]] - data[bufX][bufY]), 2);
  232.  
  233. if (cost[movedBufX][movedBufY] < cost[bufX][bufY] + buffMath) {
  234. cost[movedBufX][movedBufY] = (cost[bufX][bufY] + buffMath);
  235.  
  236. prev[movedBufX][movedBufY][0] = bufX;
  237. prev[movedBufX][movedBufY][1] = bufY;
  238. costMin.add(new DataNode(cost[movedBufX][movedBufY], movedBufX, movedBufY));
  239. }
  240. }
  241. } catch (Exception e) {
  242. continue;
  243. }
  244. }
  245. }
  246. try {
  247. bufX = costMin.peek().corX;
  248. bufY = costMin.peek().corY;
  249. costMin.remove();
  250.  
  251. } catch (Exception e) {
  252.  
  253. break;
  254. }
  255. }
  256.  
  257. }
  258.  
  259. private static void wayToString(int[][][] prev, int posX, int posY, int startX, int startY, ArrayList<Integer> way) {
  260. int bufX, bufY;
  261. int[][] positionData = new int[][]{
  262. {7, 6, 5},
  263. {0, -1, 4},
  264. {1, 2, 3}};
  265.  
  266. while (!(posX == startX && posY == startY)) {
  267. bufX = posY - prev[posX][posY][1] + 1;
  268. bufY = posX - prev[posX][posY][0] + 1;
  269. way.add(positionData[bufX][bufY]);
  270. bufX = posX;
  271. bufY = posY;
  272. posX = prev[bufX][bufY][0];
  273. posY = prev[bufX][bufY][1];
  274. }
  275.  
  276. }
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement