Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.io.InputStream;
- import java.nio.file.Path;
- import java.io.File;
- import java.io.*;
- import java.util.Stack;
- import java.util.List;
- public class Main {
- public static String output = "";
- public static String out = "";
- public static int HEIGHT;
- public static int WIDTH;
- public static Point START;
- public static Point END;
- public static ArrayList<Boolean> list = new ArrayList<>();
- public static Stack<Point> stack = new Stack<>();
- public static int[] tor;
- public static int[][] matrix;
- public static Point[][] arr;
- public static Labirint labirint;
- public static Point[][] potr;
- public static void main(String[] args) {
- Path p = Paths.get("C:\\Users\\RomanNumberOne\\IdeaProjects\\Laba2\\src\\com\\company\\Input");
- String[] s;
- s = read(p);
- START = setPoint(s , Integer.parseInt(s[s.length-4]), Integer.parseInt(s[s.length-3]));
- END = setPoint(s , Integer.parseInt(s[s.length-2]), Integer.parseInt(s[s.length-1]));
- labirint = labirintInitialize(s);
- potr = labirint.getMatrix();
- Point current;
- current = potr[START.getX()-1][START.getY()-1];
- for (int i = 0; i<HEIGHT; i++){
- for (int j = 0; j<WIDTH; j++){
- if (labirint.getMatrix()[i][j].getSignification()){
- list.add(true);
- }
- }
- }
- list.remove(0);
- stack.push(current);
- output += current.getX()+1;
- output += " ";
- output += current.getY()+1;
- output += "\n";
- while (!list.isEmpty()) {
- Point up = potr[current.getX()-1][current.getY()];
- Point down = potr[current.getX()+1][current.getY()];
- Point left = potr[current.getX()][current.getY()-1];
- Point right = potr[current.getX()][current.getY()+1];
- current.setVisitation(true);
- if (up.getSignification() && !up.getVisitation()){
- current = up;
- current.setVisitation(true);
- list.remove(0);
- stack.push(current);
- //System.out.println("up");
- output += current.getX()+1;
- output += " ";
- output += current.getY()+1;
- output += "\n";
- }else if(down.getSignification() && !down.getVisitation()){
- current = down;
- current.setVisitation(true);
- list.remove(0);
- stack.push(current);
- //System.out.println("down");
- output += current.getX()+1;
- output += " ";
- output += current.getY()+1;
- output += "\n";
- }else if (left.getSignification() && !left.getVisitation()){
- current = left;
- current.setVisitation(true);
- stack.push(current);
- list.remove(0);
- //System.out.println("left");
- output += current.getX()+1;
- output += " ";
- output += current.getY()+1;
- output += "\n";
- }else if (right.getSignification() && !right.getVisitation()){
- current = right;
- current.setVisitation(true);
- list.remove(0);
- stack.push(current);
- //System.out.println("right");
- output += current.getX()+1;
- output += " ";
- output += current.getY()+1;
- output += "\n";
- }else if (!stack.empty()){
- stack.pop();
- if (!stack.empty()) {
- current = stack.pop();
- output = output.substring(0, output.length() - 4);
- }else {
- break;
- }
- }else {
- break;
- }
- }
- if (list.isEmpty()){
- //System.out.println("Y");
- out+="Y";
- out+="\n";
- out += output;
- }else {
- out+="N";
- //System.out.println("N");
- }
- //System.out.println(out);
- write("C:\\Users\\RomanNumberOne\\IdeaProjects\\Laba2\\src\\com\\company\\Output", out);
- }
- public static String[] read(Path p){
- String str;
- InputStream in;
- try{
- in = Files.newInputStream(p);
- byte[] bytes = new byte[in.available()];
- in.read(bytes);
- String s = new String(bytes, "cp1251");
- str = s;
- }
- catch (Exception e){
- return null;
- }
- String lolo = str.replaceAll("\\p{Cntrl}", ",");
- String help = lolo.replaceAll(" ", ";");
- String d = help.replaceAll(",,", ";");
- String[] s = d.split(";");
- return s;
- }
- public static Labirint labirintInitialize(String[] s){
- tor = new int[s.length];
- int c = 0;
- for (String i: s){
- tor[c] = Integer.parseInt(i);
- c++;
- }
- HEIGHT = tor[0];
- WIDTH = tor [1];
- matrix = new int[HEIGHT][WIDTH];
- int yo = 2;
- for (int i=0; i<HEIGHT; i++){
- for (int j=0; j<WIDTH; j++){
- matrix[i][j] = tor[yo];
- yo++;
- }
- }
- arr = new Point[HEIGHT][WIDTH];
- for (int i = 0; i<HEIGHT; i++){
- for (int j = 0; j<WIDTH; j++){
- Point point = new Point(i,j);
- if (matrix[i][j] == 0){
- point.setSignification(true);
- }else{
- point.setSignification(false);
- }
- arr[i][j] = point;
- }
- }
- Labirint labirint = new Labirint(arr);
- return labirint;
- }
- public static Point setPoint(String[] s, int x, int y){
- Point point = new Point(x,y);
- return point;
- }
- static void write(String fileName, String text) {
- //Определяем файл
- File file = new File(fileName);
- try {
- //проверяем, что если файл не существует то создаем его
- if(!file.exists()){
- file.createNewFile();
- }
- //PrintWriter обеспечит возможности записи в файл
- PrintWriter out = new PrintWriter(file.getAbsoluteFile());
- try {
- //Записываем текст у файл
- out.print(text);
- } finally {
- //После чего мы должны закрыть файл
- //Иначе файл не запишется
- out.close();
- }
- } catch(IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement