Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package trasa;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.PriorityQueue;
- import java.util.Scanner;
- public class Trasa {
- public static void main(String[] args) throws FileNotFoundException, IOException {
- int n;
- int m;
- int startX;
- int startY;
- int finishX;
- int finishY;
- int bufX;
- int bufY;
- int[] movingTab = new int[]{-1, 0, 1};
- ArrayList<Integer> wayS = new ArrayList<Integer>();
- ArrayList<Integer> wayL = new ArrayList<Integer>();
- Scanner input = new Scanner(new File("src/in.txt"));
- File output = new File("src/out.txt");
- FileWriter fWriter = new FileWriter(output);
- PrintWriter pWriter = new PrintWriter(fWriter);
- n = input.nextInt();
- m = input.nextInt();
- int[][] data = new int[n][m];
- boolean[][] visits = new boolean[n][m];
- int[][] cost = new int[n][m];
- Comparator<DataNode> comparator = new CostComparator();
- PriorityQueue<DataNode> costMin = new PriorityQueue<>(n * m, comparator);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- data[i][j] = input.nextInt();
- }
- }
- startX = input.nextInt();
- startY = input.nextInt();
- finishX = input.nextInt();
- finishY = input.nextInt();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cost[i][j] = Integer.MAX_VALUE;
- }
- }
- cost[startX - 1][startY - 1] = 0;
- visits[startX - 1][startY - 1] = true;
- int[][][] prev = new int[n][m][2];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- for (int k = 0; k < 2; k++) {
- prev[i][j][k] = -1;
- }
- }
- }
- bufX = startX - 1;
- bufY = startY - 1;
- input.close();
- shortWay(data, cost, prev, movingTab, costMin, visits, bufX, bufY);
- System.out.print(cost[finishX - 1][finishY - 1]+" ");
- int posX = finishX - 1;
- int posY = finishY - 1;
- wayToString(prev, posX, posY, startX - 1, startY - 1, wayS);
- Collections.reverse(wayS);
- read(data, startX, startY, finishX, finishY, cost, visits, bufX, bufY);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cost[i][j] = 0;
- }
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- for (int k = 0; k < 2; k++) {
- prev[i][j][k] = -1;
- }
- }
- }
- longWay(data, cost, prev, movingTab, costMin, visits, bufX, bufY);
- System.out.println(cost[finishX - 1][finishY - 1]);
- posX = finishX - 1;
- posY = finishY - 1;
- wayToString(prev, posX, posY, startX - 1, startY - 1, wayL);
- Collections.reverse(wayL);
- System.out.println(wayS);
- System.out.println(wayL);
- }
- private static void read(int[][] data, int startX, int startY, int finishX, int finishY, int[][] cost, boolean[][] visits, int bufX, int bufY) throws FileNotFoundException {
- Scanner input = new Scanner(new File("src/in.txt"));
- int n = input.nextInt();
- int m = input.nextInt();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- data[i][j] = input.nextInt();
- }
- }
- startX = input.nextInt();
- startY = input.nextInt();
- finishX = input.nextInt();
- finishY = input.nextInt();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cost[i][j] = Integer.MAX_VALUE;
- visits[i][j] = false;
- }
- }
- cost[startX - 1][startY - 1] = 0;
- visits[startX - 1][startY - 1] = true;
- int[][][] prev = new int[n][m][2];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- for (int k = 0; k < 2; k++) {
- prev[i][j][k] = -1;
- }
- }
- }
- bufX = startX - 1;
- bufY = startY - 1;
- }
- private static class DataNode<Integer> {
- DataNode(int thecost, int x, int y) {
- cost = thecost;
- corX = x;
- corY = y;
- }
- int cost;
- int corX;
- int corY;
- }
- private static class CostComparator implements Comparator<DataNode> {
- @Override
- public int compare(DataNode t, DataNode t1) {
- if (t.cost < t1.cost) {
- return -1;
- }
- if (t.cost > t1.cost) {
- return 1;
- }
- return 0;
- }
- }
- public static boolean areAllTrue(boolean[][] tab) {
- for (boolean[] b : tab) {
- for (boolean c : b) {
- if (!c) {
- return false;
- }
- }
- }
- return true;
- }
- private static void shortWay(int[][] data, int[][] cost, int[][][] prev, int[] movingTab, PriorityQueue<DataNode> costMin, boolean[][] visits, int bufX, int bufY) {
- int buffMath;
- int movedBufX;
- int movedBufY;
- while (areAllTrue(visits) == false) {
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (movingTab[i] == 0 && movingTab[j] == 0) {
- continue;
- }
- try {
- movedBufX = bufX + movingTab[i];
- movedBufY = bufY + movingTab[j];
- if (visits[movedBufX][movedBufY] == false) {
- buffMath = (int) Math.pow((data[bufX + movingTab[i]][bufY + movingTab[j]] - data[bufX][bufY]), 2);
- if (cost[movedBufX][movedBufY] > cost[bufX][bufY] + buffMath) {
- cost[movedBufX][movedBufY] = (cost[bufX][bufY] + buffMath);
- prev[movedBufX][movedBufY][0] = bufX;
- prev[movedBufX][movedBufY][1] = bufY;
- costMin.add(new DataNode(cost[movedBufX][movedBufY], movedBufX, movedBufY));
- }
- }
- } catch (Exception e) {
- continue;
- }
- }
- }
- bufX = costMin.peek().corX;
- bufY = costMin.peek().corY;
- visits[costMin.peek().corX][costMin.peek().corY] = true;
- costMin.remove();
- }
- }
- private static void longWay(int[][] data, int[][] cost, int[][][] prev, int[] movingTab, PriorityQueue<DataNode> costMin, boolean[][] visits, int bufX, int bufY) {
- int buffMath;
- int movedBufX;
- int movedBufY;
- while (true) {
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (movingTab[i] == 0 && movingTab[j] == 0) {
- continue;
- }
- try {
- movedBufX = bufX + movingTab[i];
- movedBufY = bufY + movingTab[j];
- if (data[movedBufX][movedBufY] - data[bufX][bufY] >= 0) {
- buffMath = (int) Math.pow((data[bufX + movingTab[i]][bufY + movingTab[j]] - data[bufX][bufY]), 2);
- if (cost[movedBufX][movedBufY] < cost[bufX][bufY] + buffMath) {
- cost[movedBufX][movedBufY] = (cost[bufX][bufY] + buffMath);
- prev[movedBufX][movedBufY][0] = bufX;
- prev[movedBufX][movedBufY][1] = bufY;
- costMin.add(new DataNode(cost[movedBufX][movedBufY], movedBufX, movedBufY));
- }
- }
- } catch (Exception e) {
- continue;
- }
- }
- }
- try {
- bufX = costMin.peek().corX;
- bufY = costMin.peek().corY;
- costMin.remove();
- } catch (Exception e) {
- break;
- }
- }
- }
- private static void wayToString(int[][][] prev, int posX, int posY, int startX, int startY, ArrayList<Integer> way) {
- int bufX, bufY;
- int[][] positionData = new int[][]{
- {7, 6, 5},
- {0, -1, 4},
- {1, 2, 3}};
- while (!(posX == startX && posY == startY)) {
- bufX = posY - prev[posX][posY][1] + 1;
- bufY = posX - prev[posX][posY][0] + 1;
- way.add(positionData[bufX][bufY]);
- bufX = posX;
- bufY = posY;
- posX = prev[bufX][bufY][0];
- posY = prev[bufX][bufY][1];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement