Advertisement
Lusien_Lashans

Laba 2 Main

Apr 30th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.06 KB | None | 0 0
  1. package com.company;
  2. import java.nio.file.Files;
  3. import java.nio.file.Paths;
  4. import java.util.ArrayList;
  5. import java.io.InputStream;
  6. import java.nio.file.Path;
  7. import java.io.File;
  8. import java.io.*;
  9. import java.util.Stack;
  10. import java.util.List;
  11.  
  12. public class Main {
  13.     public static String output = "";
  14.     public static String out = "";
  15.  
  16.     public static int HEIGHT;
  17.     public static int WIDTH;
  18.     public static Point START;
  19.     public static Point END;
  20.  
  21.  
  22.     public static ArrayList<Boolean> list = new ArrayList<>();
  23.     public static Stack<Point> stack = new Stack<>();
  24.     public static int[] tor;
  25.     public static int[][] matrix;
  26.     public static Point[][] arr;
  27.     public static Labirint labirint;
  28.     public static Point[][] potr;
  29.  
  30.     public static void main(String[] args) {
  31.         Path p = Paths.get("C:\\Users\\RomanNumberOne\\IdeaProjects\\Laba2\\src\\com\\company\\Input");
  32.         String[] s;
  33.         s = read(p);
  34.         START = setPoint(s , Integer.parseInt(s[s.length-4]), Integer.parseInt(s[s.length-3]));
  35.         END = setPoint(s , Integer.parseInt(s[s.length-2]), Integer.parseInt(s[s.length-1]));
  36.  
  37.         labirint = labirintInitialize(s);
  38.         potr = labirint.getMatrix();
  39.         Point current;
  40.         current = potr[START.getX()-1][START.getY()-1];
  41.  
  42.         for (int i = 0; i<HEIGHT; i++){
  43.             for (int j = 0; j<WIDTH; j++){
  44.                 if (labirint.getMatrix()[i][j].getSignification()){
  45.                     list.add(true);
  46.                 }
  47.             }
  48.         }
  49.         list.remove(0);
  50.         stack.push(current);
  51.         output += current.getX()+1;
  52.         output += " ";
  53.         output += current.getY()+1;
  54.         output += "\n";
  55.         while (!list.isEmpty()) {
  56.             Point up = potr[current.getX()-1][current.getY()];
  57.             Point down = potr[current.getX()+1][current.getY()];
  58.             Point left = potr[current.getX()][current.getY()-1];
  59.             Point right = potr[current.getX()][current.getY()+1];
  60.             current.setVisitation(true);
  61.  
  62.  
  63.  
  64.             if (up.getSignification() && !up.getVisitation()){
  65.                 current = up;
  66.                 current.setVisitation(true);
  67.                 list.remove(0);
  68.                 stack.push(current);
  69.                 //System.out.println("up");
  70.                 output += current.getX()+1;
  71.                 output += " ";
  72.                 output += current.getY()+1;
  73.                 output += "\n";
  74.             }else if(down.getSignification() && !down.getVisitation()){
  75.                 current = down;
  76.                 current.setVisitation(true);
  77.                 list.remove(0);
  78.                 stack.push(current);
  79.                 //System.out.println("down");
  80.                 output += current.getX()+1;
  81.                 output += " ";
  82.                 output += current.getY()+1;
  83.                 output += "\n";
  84.             }else if (left.getSignification() && !left.getVisitation()){
  85.                 current = left;
  86.                 current.setVisitation(true);
  87.                 stack.push(current);
  88.                 list.remove(0);
  89.                 //System.out.println("left");
  90.                 output += current.getX()+1;
  91.                 output += " ";
  92.                 output += current.getY()+1;
  93.                 output += "\n";
  94.             }else if (right.getSignification() && !right.getVisitation()){
  95.                 current = right;
  96.                 current.setVisitation(true);
  97.                 list.remove(0);
  98.                 stack.push(current);
  99.                 //System.out.println("right");
  100.                 output += current.getX()+1;
  101.                 output += " ";
  102.                 output += current.getY()+1;
  103.                 output += "\n";
  104.             }else if (!stack.empty()){
  105.                 stack.pop();
  106.                 if (!stack.empty()) {
  107.                     current = stack.pop();
  108.                     output = output.substring(0, output.length() - 4);
  109.                 }else {
  110.                     break;
  111.                 }
  112.             }else {
  113.                 break;
  114.             }
  115.  
  116.         }
  117.  
  118.         if (list.isEmpty()){
  119.             //System.out.println("Y");
  120.             out+="Y";
  121.             out+="\n";
  122.             out += output;
  123.         }else {
  124.             out+="N";
  125.             //System.out.println("N");
  126.         }
  127.         //System.out.println(out);
  128.  
  129.         write("C:\\Users\\RomanNumberOne\\IdeaProjects\\Laba2\\src\\com\\company\\Output", out);
  130.     }
  131.  
  132.     public static String[] read(Path p){
  133.         String str;
  134.         InputStream in;
  135.         try{
  136.             in = Files.newInputStream(p);
  137.             byte[] bytes = new byte[in.available()];
  138.             in.read(bytes);
  139.             String s = new String(bytes, "cp1251");
  140.             str = s;
  141.         }
  142.         catch (Exception e){
  143.             return null;
  144.         }
  145.         String lolo = str.replaceAll("\\p{Cntrl}", ",");
  146.         String help = lolo.replaceAll(" ", ";");
  147.         String d = help.replaceAll(",,", ";");
  148.         String[] s = d.split(";");
  149.         return s;
  150.     }
  151.     public static Labirint labirintInitialize(String[] s){
  152.         tor = new int[s.length];
  153.  
  154.         int c = 0;
  155.         for (String i: s){
  156.             tor[c] = Integer.parseInt(i);
  157.             c++;
  158.         }
  159.         HEIGHT = tor[0];
  160.         WIDTH = tor [1];
  161.  
  162.         matrix = new int[HEIGHT][WIDTH];
  163.  
  164.         int yo = 2;
  165.         for (int i=0; i<HEIGHT; i++){
  166.             for (int j=0; j<WIDTH; j++){
  167.                 matrix[i][j] = tor[yo];
  168.                 yo++;
  169.             }
  170.         }
  171.  
  172.         arr = new Point[HEIGHT][WIDTH];
  173.         for (int i = 0; i<HEIGHT; i++){
  174.             for (int j = 0; j<WIDTH; j++){
  175.                 Point point = new Point(i,j);
  176.                 if (matrix[i][j] == 0){
  177.                     point.setSignification(true);
  178.                 }else{
  179.                     point.setSignification(false);
  180.                 }
  181.                 arr[i][j] = point;
  182.             }
  183.         }
  184.         Labirint labirint = new Labirint(arr);
  185.         return labirint;
  186.     }
  187.     public static Point setPoint(String[] s, int x, int y){
  188.         Point point = new Point(x,y);
  189.         return point;
  190.     }
  191.     static void write(String fileName, String text) {
  192.         //Определяем файл
  193.         File file = new File(fileName);
  194.  
  195.         try {
  196.             //проверяем, что если файл не существует то создаем его
  197.             if(!file.exists()){
  198.                 file.createNewFile();
  199.             }
  200.  
  201.             //PrintWriter обеспечит возможности записи в файл
  202.             PrintWriter out = new PrintWriter(file.getAbsoluteFile());
  203.  
  204.             try {
  205.                 //Записываем текст у файл
  206.                 out.print(text);
  207.             } finally {
  208.                 //После чего мы должны закрыть файл
  209.                 //Иначе файл не запишется
  210.                 out.close();
  211.             }
  212.         } catch(IOException e) {
  213.             throw new RuntimeException(e);
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement