Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.util.List;
- import java.util.Scanner;
- public class Main {
- public static Scanner scan = new Scanner(System.in);
- static Scanner fileScanner;
- public static byte[][] adjacencyMatrix = new byte[0][];
- public record element(int elem, byte i) {}
- public static void main(String[] args) {
- System.out.println("This program searches for the maximal path from one element to another in the matrix.");
- System.out.println("Enter type of input(matrix, elements)." + '\n' + "1 is console input, 0 is file input.");
- element[][] arr = inputInformation();
- Graph graph = showMatrixWithLetters(arr);
- System.out.println("Input start point:");
- String start = getLetter(arr.length * arr[0].length);
- System.out.println("Input finish point:");
- String finish = getLetter(arr.length * arr[0].length);
- findPaths(graph, start, finish);
- }
- private static Graph showMatrixWithLetters (element[][] arr) {
- adjacencyMatrix = transformMatrixToAdjacencyMatrix(arr, arr[0].length, arr.length);
- Graph graph = new Graph();
- char vertex = 'A';
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[0].length; j++) {
- graph.addVertex(arr[i][j].elem, String.valueOf(vertex));
- vertex = (char) (vertex + 1);
- }
- }
- final String ANSI_YELLOW = "\u001B[33m";
- final String ANSI_RESET = "\u001B[0m";
- vertex = 'A';
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[0].length; j++) {
- System.out.print(ANSI_YELLOW + vertex + ANSI_RESET + "(" + arr[i][j].elem + ") ");
- vertex = (char) (vertex + 1);
- }
- System.out.println();
- }
- return graph;
- }
- private static element[][] inputInformation (){
- int n, m;
- element[][] arr;
- String filePath;
- if (choose()) {
- System.out.println("Input size of matrix.");
- System.out.println("Rows:");
- m = inputArraySize();
- System.out.println("Columns:");
- n = inputArraySize();
- System.out.println("Input matrix elements:");
- arr = inputArray(m, n);
- } else {
- System.out.println("Example of file:");
- System.out.println("Rows: 1");
- System.out.println("Columns: 3");
- System.out.println("Matrix: 12 14 234");
- filePath = inputFilePath();
- byte time = 0;
- m = inputSizeOfArrayFromFile(filePath, time);
- time++;
- n = inputSizeOfArrayFromFile(filePath, time);
- arr = inputArrayFile(filePath, m, n);
- }
- return arr;
- }
- private static void findPaths (Graph graph, String start, String finish) {
- List<List<String>> allPaths = graph.allPaths(start, finish);
- int sum, max = 0;
- System.out.println("All paths:");
- for (List<String> path : allPaths) {
- sum = findSum(path, graph);
- if (sum > max) {
- max = sum;
- }
- System.out.println(path + " - " + sum);
- }
- System.out.println("Max path(s):");
- for (List<String> path : allPaths) {
- if (max == findSum(path, graph)) {
- System.out.println(path + " - " + max);
- }
- }
- }
- private static boolean choose() {
- int inputNumber = 0;
- boolean isIncorrect;
- final int MIN_NUM = 0;
- final int MAX_NUM = 1;
- do {
- isIncorrect = false;
- try {
- inputNumber = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a number.");
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- System.out.println("You are out of input range!:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return inputNumber == 1;
- }
- private static int inputData() {
- int n = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a integer number:");
- }
- } while (isIncorrect);
- return n;
- }
- private static int findSum(List<String> vertexes, Graph graph) {
- int sum = 0;
- for (int i = 0; i < graph.vertexLabels.length; i++) {
- if (vertexes.contains(graph.vertexLabels[i].label())) {
- sum += graph.vertexLabels[i].vertex();
- }
- }
- return sum;
- }
- private static String getLetter(int length) {
- boolean isIncorrect;
- String vertex;
- do {
- vertex = scan.nextLine();
- isIncorrect = false;
- if (vertex.length() > 1) {
- isIncorrect = true;
- System.out.println("Bro, just input letter of vertex.");
- }
- if ((vertex.charAt(0) < 'A' || vertex.charAt(0) >= (char) ('A' + length)) && !isIncorrect) {
- System.out.println("Please, enter a letter of vertex from A to " + (char) ('A' + length - 1));
- isIncorrect = true;
- }
- } while (isIncorrect);
- return vertex;
- }
- private static int inputArraySize() {
- boolean isIncorrect;
- final int MIN_SIZE = 1;
- final int MAX_SIZE = 5;
- int num;
- do {
- num = inputData();
- isIncorrect = false;
- if ((num < MIN_SIZE) || (num > MAX_SIZE)) {
- System.out.println("Please, enter a number in the range from 1 to 5:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return num;
- }
- private static element[][] inputArray(int n, int m) {
- element[][] arr = new element[m][n];
- byte index = 1;
- for (int i = 0; i < m; i++)
- for (int j = 0; j < n; j++) {
- arr[i][j] = new element(inputData(), index);
- index++;
- }
- return arr;
- }
- public static byte[][] transformMatrixToAdjacencyMatrix (element[][] arr, int n, int m) {
- final byte ONE = 1;
- adjacencyMatrix = new byte[n*m][n*m]; //можно и boolean, но он, как и byte, 1 байт занимает
- for(int i = 0; i < m; i++){
- for(int j = 0; j < n; j++){
- if (i+1 < m){
- adjacencyMatrix[arr[i][j].i - 1][arr[i+1][j].i - 1] = ONE;
- adjacencyMatrix[arr[i+1][j].i - 1][arr[i][j].i - 1] = ONE;
- }
- if (i > 0){
- adjacencyMatrix[arr[i][j].i - 1][arr[i-1][j].i - 1] = ONE;
- adjacencyMatrix[arr[i-1][j].i - 1][arr[i][j].i - 1] = ONE;
- }
- if (j+1 < n){
- adjacencyMatrix[arr[i][j].i - 1][arr[i][j+1].i - 1] = ONE;
- adjacencyMatrix[arr[i][j+1].i - 1][arr[i][j].i - 1] = ONE;
- }
- if (j > 0){
- adjacencyMatrix[arr[i][j].i - 1][arr[i][j-1].i - 1] = ONE;
- adjacencyMatrix[arr[i][j-1].i - 1][arr[i][j].i - 1] = ONE;
- }
- }
- }
- return adjacencyMatrix;
- }
- private static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path:");
- path = scan.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.println("Wrong way to file.");
- isIncorrect = true;
- }
- if (!file.canRead() && file.exists()) {
- System.out.println("Impossible to read a file.");
- isIncorrect = true;
- }
- if (!file.canWrite() && file.canRead()) {
- System.out.println("Impossible to write a file.");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- private static int inputSizeOfArrayFromFile(String path, byte time) {
- int num = 0;
- boolean isIncorrect;
- final int MIN = 1;
- final int MAX = 5;
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- if (time == 1)
- fileScanner.nextLine();
- num = fileScanner.nextInt();
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Check file.");
- path = inputFilePath();
- }
- if (!isIncorrect && ((num < MIN) || (num > MAX))) {
- isIncorrect = true;
- System.out.println("Array size should be in the range from 1 to 5:");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return num;
- }
- private static element[][] inputArrayFile(String path, int m, int n) {
- boolean isIncorrect;
- element[][] arr = new element[m][n];
- byte index = 1;
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- fileScanner.nextLine();
- fileScanner.nextLine();
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- arr[i][j] = new element(Integer.parseInt(fileScanner.next()), index);
- index++;
- }
- }
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Reading of array elements failed.");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return arr;
- }
- }
- ===============================================Graph=========================================================
- import java.util.*;
- public class Graph {
- public record vertexLabel (int vertex, String label) { }
- public vertexLabel[] vertexLabels;
- int i;
- public Graph() {
- i = 0;
- int zalupa = Main.adjacencyMatrix.length;
- vertexLabels = new vertexLabel[Main.adjacencyMatrix.length];
- }
- void addVertex(int vertexIndex, String label) {
- int hui = vertexLabels.length;
- vertexLabels[i] = new vertexLabel(vertexIndex, label);
- i++;
- }
- public List<List<String>> allPaths(String startLabel, String endLabel) {
- List<List<String>> allPaths = new ArrayList<>();
- int startIndex = -1;
- int endIndex = -1;
- for (int i = 0; i < vertexLabels.length; i++) {
- if (Objects.equals(vertexLabels[i].label, startLabel)) {
- startIndex = i;
- }
- if (Objects.equals(vertexLabels[i].label, endLabel)) {
- endIndex = i;
- }
- }
- if (startIndex == -1 || endIndex == -1) {
- return allPaths;
- }
- List<String> currentPath = new ArrayList<>();
- currentPath.add(startLabel);
- allPathsDFS(startIndex, endIndex, currentPath, allPaths);
- return allPaths;
- }
- private void allPathsDFS(int currentIndex, int endIndex, List<String> currentPath, List<List<String>> allPaths) {
- if (currentIndex == endIndex) {
- allPaths.add(new ArrayList<>(currentPath));
- } else {
- for (int i = 0; i < Main.adjacencyMatrix[currentIndex].length; i++) {
- if (Main.adjacencyMatrix[currentIndex][i] == 1 && !currentPath.contains(vertexLabels[i].label)) {
- currentPath.add(vertexLabels[i].label);
- allPathsDFS(i, endIndex, currentPath, allPaths);
- currentPath.remove(currentPath.size() - 1);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement