Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.datastructure.maze.cs04;
- import java.awt.Point;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.Queue;
- import java.util.Scanner;
- import eg.edu.alexu.csd.datastructure.maze.IMazeSolver;
- import eg.edu.alexu.csd.datastructure.linkedList.cs04.SinglyLinkedList;
- import eg.edu.alexu.csd.datastructure.queue.ILinkedBased;
- import eg.edu.alexu.csd.datastructure.queue.IQueue;
- import eg.edu.alexu.csd.datastructure.queue.cs04.QueueList;
- import eg.edu.alexu.csd.datastructure.stack.cs04.Stack;
- public class Maze implements IMazeSolver {
- int queueSize;
- int stackSize;
- QueueList queue = new QueueList();
- QueueList queueR = new QueueList();
- Stack stack = new Stack();
- Scanner scan = new Scanner(System.in);
- static String rows;
- static String cols;
- static int rowsInt;
- static int colInt;
- static boolean flagNext = false;
- static char[][] array1;
- static char[][] array;
- static boolean[][] bolli;
- static int xStart;
- static int yStart;
- static int xEnd;
- static int yEnd;
- static boolean notFound = false;
- static boolean boundries = false;
- static boolean flag = false;
- static boolean flagFirst = false;
- static boolean found = false;
- public void printDebugInt(int i) {
- System.out.println(i);
- }
- public void printDebug(String s) {
- System.out.println(s);
- }
- private void printDebugPoint(Point point) {
- // TODO Auto-generated method stub
- System.out.println(point);
- }
- private void printDebugBool(boolean b) {
- // TODO Auto-generated method stub
- System.out.println(b);
- }
- public static void main(String[] args) throws IOException {
- Maze mazy = new Maze();
- File s = (new File("E:\\2nd_sem_1st_year\\Code Masri\\java projects\\maze Try\\src\\aia.txt"));
- mazy.solveBFS(s);
- }
- @Override
- public int[][] solveBFS(File maze) {
- // TODO Auto-generated method stub
- int[][] returnedArray = null;
- Scanner s;
- int counter = 0;
- try {
- s = new Scanner(maze);
- while (s.hasNext()) {
- // System.out.println(str);
- boolean flag1 = false;
- if (counter == 0) {
- String str = s.nextLine();
- printDebug(str);
- String tempy = null;
- for (int i = 0; i < str.length(); i++) {
- if (tempy == null && str.charAt(i) != ' ') {
- tempy = "" + str.charAt(i);
- while (i + 1 != str.length() && str.charAt(i + 1) != ' ' && (i + 1 != str.length() - 1)) {
- tempy += "" + str.charAt(i + 1);
- i++;
- }
- if (flag1 == false) {
- rows = tempy;
- printDebug("rows" + rows);
- tempy = null;
- flag1 = true;
- } else {
- cols = tempy;
- printDebug("cols" + cols);
- tempy = null;
- }
- }
- }
- rowsInt = Integer.parseInt(rows);
- colInt = Integer.parseInt(cols);
- // array1 = new char [rowsInt][colInt];
- array = new char[rowsInt][colInt];
- bolli = new boolean[rowsInt][colInt];
- printDebug(Integer.toString(rowsInt));
- printDebug(Integer.toString(colInt));
- counter++;
- } else {
- printDebug("req array");
- for (int x = 0; x < rowsInt; x++) {
- String text = s.nextLine().trim();
- printDebug(text);
- for (int y = 0; y < colInt; y++) {
- array[x][y] = text.charAt(y);
- // array[x][y] = array1[x][y] ;
- printDebug("" + array[x][y]);
- // printDebug(""+array[x][y]);
- if (array[x][y] == 'S') {
- yStart = x;
- xStart = y;
- } else if (array[x][y] == 'E') {
- yEnd = x;
- xEnd = y;
- }
- }
- }
- // array = array1 ;
- printDebug("*******");
- // printDebug(""+array[0][1]);
- for (int m = 0; m < rowsInt; m++) {
- for (int mm = 0; mm < colInt; mm++) {
- printDebug("" + array[m][mm]);
- }
- }
- // hena
- int yy = xStart;
- int xx = yStart;
- queue.enqueue(new Point(xx, yy));
- Point point2 = new Point(xx, yy);
- printDebugPoint(point2);
- // bolli[xx][yy] = true;
- flagFirst = true;
- while (notFound == false && boundries == false && found == false) {
- Point pointy = new Point();
- if (queue.size() == 0) {
- // break;
- throw new RuntimeException("empty ya yoyo");
- }
- // if (flagNext == false && flagFirst == false) {
- // pointy = (Point) queue.dequeue();
- // pointy = (Point) queue.peek();
- // } else {
- // pointy = (Point) queue.peek();
- // }
- flagFirst = false;
- flagNext = false;
- pointy = (Point) queue.dequeue();
- xx = pointy.x;
- yy = pointy.y;
- printDebugPoint(pointy);
- if (bolli[xx][yy] == false) {
- queueR.enqueue(pointy);
- bolli[xx][yy] = true;
- }
- printDebug("" + array[xx][yy]);
- if (xx == 0 && yy + 1 == colInt) {
- if (array[xx + 1][yy] != '#' && bolli[xx + 1][yy] == false) {
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- }
- if (array[xx][yy - 1] != '#') {
- printDebug("" + array[xx][yy - 1]);
- flagNext = true;
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- } else if (xx == 0 && yy == 0) {
- if (array[xx][yy + 1] != '#' && bolli[xx][yy + 1] == false) {
- flagNext = true;
- printDebug("" + array[xx][yy + 1]);
- printDebugBool(bolli[xx][yy + 1]);
- printDebugPoint(new Point(xx, yy + 1));
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- } if (array[xx + 1][yy] != '#'&& bolli[xx + 1][yy] == false) {
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- }
- } else if (yy == 0 && xx + 1 == rowsInt) {
- if (array[xx - 1][yy] != '#'&& bolli[xx - 1][yy] == false) {
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } if (array[xx][yy + 1] != '#'&& bolli[xx][yy + 1] == false) {
- flagNext = true;
- printDebug("" + array[xx][yy + 1]);
- printDebugBool(bolli[xx][yy + 1]);
- printDebugPoint(new Point(xx, yy + 1));
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- }
- } else if (yy + 1 == colInt && xx + 1 == rowsInt) {
- if (array[xx - 1][yy] != '#'&& bolli[xx - 1][yy] == false) {
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } if (array[xx][yy - 1] != '#'&& bolli[xx][yy - 1] == false) {
- flagNext = true;
- printDebug("" + array[xx][yy - 1]);
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- } else if (yy + 1 == colInt && (xx != 0 && xx + 1 != rowsInt)) {
- if (array[xx - 1][yy] != '#'&& bolli[xx - 1][yy] == false) {
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } if (array[xx + 1][yy] != '#'&& bolli [xx + 1][yy]== false) {
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- } if (array[xx][yy - 1] != '#'&& bolli[xx][yy - 1] == false) {
- flagNext = true;
- printDebug("" + array[xx][yy - 1]);
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- } else if (yy == 0 && (xx != 0 && xx + 1 != rowsInt)) {
- if (array[xx - 1][yy] != '#'&& bolli [xx - 1][yy]== false) {
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } if (array[xx][yy + 1] != '#'&& bolli [xx][yy + 1]== false) {
- flagNext = true;
- printDebug("" + array[xx][yy + 1]);
- printDebugBool(bolli[xx][yy + 1]);
- printDebugPoint(new Point(xx, yy + 1));
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- } if (array[xx + 1][yy] != '#'&& bolli[xx + 1][yy] == false) {
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- }
- } else if (xx + 1 == rowsInt && (yy != 0 && yy + 1 != colInt)) {
- if (array[xx - 1][yy] != '#'&& bolli[xx - 1][yy] == false) {
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } if (array[xx][yy + 1] != '#'&& bolli [xx][yy + 1]== false) {
- flagNext = true;
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- } if (array[xx][yy - 1] != '#'&& bolli[xx][yy - 1] == false) {
- flagNext = true;
- printDebug("" + array[xx][yy - 1]);
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- } else if (xx == 0 && (yy != 0 && yy + 1 != colInt)) {
- if (array[xx][yy + 1] != '#'&& bolli[xx][yy + 1] == false) {
- flagNext = true;
- printDebug("" + array[xx][yy + 1]);
- printDebugBool(bolli[xx][yy + 1]);
- printDebugPoint(new Point(xx, yy + 1));
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- } if (array[xx + 1][yy] != '#'&& bolli [xx + 1][yy]== false) {
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- } if (array[xx][yy - 1] != '#'&& bolli[xx][yy - 1] == false) {
- flagNext = true;
- printDebug("" + array[xx][yy - 1]);
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- } else {
- if (array[xx - 1][yy] != '#'&& bolli[xx - 1][yy] == false) {
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } if (array[xx][yy + 1] != '#'&& bolli[xx][yy + 1] == false) {
- flagNext = true;
- printDebug("" + array[xx][yy + 1]);
- printDebugBool(bolli[xx][yy + 1]);
- printDebugPoint(new Point(xx, yy + 1));
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- } if (array[xx + 1][yy] != '#'&& bolli[xx + 1][yy] == false) {
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- } if (array[xx][yy - 1] != '#'&& bolli[xx][yy - 1] == false) {
- flagNext = true;
- printDebug("" + array[xx][yy - 1]);
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- queue.enqueue(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- }
- }
- //Point newpoint = new Point();
- queueSize = queueR.size();
- printDebug("here is my queue");
- printDebug(Integer.toString(queueSize) + "sizy");
- //queueSize++;
- int newSize = queueSize ;
- returnedArray = new int[queueSize][2];
- int counterFull = 0;
- // if (!(queueSize == 0 || queueSize == 1)) {
- // Point pointy = new Point(yEnd, xEnd);
- // // pointy = (Point) queue.dequeue();
- // printDebugPoint(pointy);
- // returnedArray[newSize][0] = pointy.x;
- // returnedArray[newSize][1] = pointy.y;
- // counterFull++;
- // }
- while (!(queueR.isEmpty())) {
- Point pointy = new Point();
- // if(counterFull)
- pointy = (Point) queueR.dequeue();
- printDebugPoint(pointy);
- returnedArray[ counterFull][0] = pointy.x;
- returnedArray[ counterFull][1] = pointy.y;
- counterFull++;
- }
- printDebug("output ya yoyo");
- for (int i = 0; i < queueSize; i++) {
- printDebug(Integer.toString(returnedArray[i][0]) + " " + Integer.toString(returnedArray[i][1]));
- }
- }
- }
- } catch (Exception e) {
- System.out.println("momken trmi exp queue");
- }
- return returnedArray;
- }
- @Override
- public int[][] solveDFS(File maze) {
- // TODO Auto-generated method stub
- int[][] returnedArray = null;
- Scanner s;
- int counter = 0;
- try {
- s = new Scanner(maze);
- while (s.hasNext()) {
- // System.out.println(str);
- boolean flag1 = false;
- if (counter == 0) {
- String str = s.nextLine();
- printDebug(str);
- String tempy = null;
- for (int i = 0; i < str.length(); i++) {
- if (tempy == null && str.charAt(i) != ' ') {
- tempy = "" + str.charAt(i);
- while (i + 1 != str.length() && str.charAt(i + 1) != ' ' && (i + 1 != str.length() - 1)) {
- tempy += "" + str.charAt(i + 1);
- i++;
- }
- if (flag1 == false) {
- rows = tempy;
- printDebug("rows" + rows);
- tempy = null;
- flag1 = true;
- } else {
- cols = tempy;
- printDebug("cols" + cols);
- tempy = null;
- }
- }
- }
- rowsInt = Integer.parseInt(rows);
- colInt = Integer.parseInt(cols);
- // array1 = new char [rowsInt][colInt];
- array = new char[rowsInt][colInt];
- bolli = new boolean[rowsInt][colInt];
- printDebug(Integer.toString(rowsInt));
- printDebug(Integer.toString(colInt));
- counter++;
- } else {
- printDebug("req array");
- for (int x = 0; x < rowsInt; x++) {
- String text = s.nextLine().trim();
- printDebug(text);
- for (int y = 0; y < colInt; y++) {
- array[x][y] = text.charAt(y);
- // array[x][y] = array1[x][y] ;
- printDebug("" + array[x][y]);
- // printDebug(""+array[x][y]);
- if (array[x][y] == 'S') {
- yStart = x;
- xStart = y;
- } else if (array[x][y] == 'E') {
- yEnd = x;
- xEnd = y;
- }
- }
- }
- // array = array1 ;
- printDebug("*******");
- // printDebug(""+array[0][1]);
- for (int m = 0; m < rowsInt; m++) {
- for (int mm = 0; mm < colInt; mm++) {
- printDebug("" + array[m][mm]);
- }
- }
- // hena
- int yy = xStart;
- int xx = yStart;
- stack.push(new Point(xx, yy));
- Point point2 = new Point(xx, yy);
- printDebugPoint(point2);
- bolli[xx][yy] = true;
- flagFirst = true;
- while (notFound == false && boundries == false && found == false) {
- Point pointy = new Point();
- if (stack.size() == 0) {
- // break;
- throw new RuntimeException("empty ya yoyo");
- }
- if (flagNext == false && flagFirst == false) {
- pointy = (Point) stack.pop();
- pointy = (Point) stack.peek();
- } else {
- pointy = (Point) stack.peek();
- }
- flagFirst = false;
- flagNext = false;
- printDebugPoint(pointy);
- xx = pointy.x;
- yy = pointy.y;
- printDebug("" + array[xx][yy]);
- if (xx == 0 && yy + 1 == colInt) {
- if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- bolli[xx + 1][yy] = true;
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
- bolli[xx][yy - 1] = true;
- printDebug("" + array[xx][yy - 1]);
- flagNext = true;
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- } else if (xx == 0 && yy == 0) {
- if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
- bolli[xx][yy + 1] = true;
- flagNext = true;
- printDebug("" + array[xx][yy + 1]);
- printDebugBool(bolli[xx][yy + 1]);
- printDebugPoint(new Point(xx, yy + 1));
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- } else if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
- bolli[xx + 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- }
- } else if (yy == 0 && xx + 1 == rowsInt) {
- if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
- bolli[xx - 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } else if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
- bolli[xx][yy + 1] = true;
- flagNext = true;
- printDebug("" + array[xx][yy + 1]);
- printDebugBool(bolli[xx][yy + 1]);
- printDebugPoint(new Point(xx, yy + 1));
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- }
- } else if (yy + 1 == colInt && xx + 1 == rowsInt) {
- if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
- bolli[xx - 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
- bolli[xx][yy - 1] = true;
- flagNext = true;
- printDebug("" + array[xx][yy - 1]);
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- } else if (yy + 1 == colInt && (xx != 0 && xx + 1 != rowsInt)) {
- if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
- bolli[xx - 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } else if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
- bolli[xx + 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
- bolli[xx][yy - 1] = true;
- flagNext = true;
- printDebug("" + array[xx][yy - 1]);
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- } else if (yy == 0 && (xx != 0 && xx + 1 != rowsInt)) {
- if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
- bolli[xx - 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } else if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
- bolli[xx][yy + 1] = true;
- flagNext = true;
- printDebug("" + array[xx][yy + 1]);
- printDebugBool(bolli[xx][yy + 1]);
- printDebugPoint(new Point(xx, yy + 1));
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- } else if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
- bolli[xx + 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- }
- } else if (xx + 1 == rowsInt && (yy != 0 && yy + 1 != colInt)) {
- if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
- bolli[xx - 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } else if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
- bolli[xx][yy + 1] = true;
- flagNext = true;
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
- bolli[xx][yy - 1] = true;
- flagNext = true;
- printDebug("" + array[xx][yy - 1]);
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- } else if (xx == 0 && (yy != 0 && yy + 1 != colInt)) {
- if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
- bolli[xx][yy + 1] = true;
- flagNext = true;
- printDebug("" + array[xx][yy + 1]);
- printDebugBool(bolli[xx][yy + 1]);
- printDebugPoint(new Point(xx, yy + 1));
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- } else if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
- bolli[xx + 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
- bolli[xx][yy - 1] = true;
- flagNext = true;
- printDebug("" + array[xx][yy - 1]);
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- } else {
- if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
- bolli[xx - 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx - 1][yy]);
- printDebugBool(bolli[xx - 1][yy]);
- printDebugPoint(new Point(xx - 1, yy));
- if (array[xx - 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx - 1, yy));
- printDebugPoint(new Point(xx - 1, yy));
- } else if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
- bolli[xx][yy + 1] = true;
- flagNext = true;
- printDebug("" + array[xx][yy + 1]);
- printDebugBool(bolli[xx][yy + 1]);
- printDebugPoint(new Point(xx, yy + 1));
- if (array[xx][yy + 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy + 1));
- printDebugPoint(new Point(xx, yy + 1));
- } else if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
- bolli[xx + 1][yy] = true;
- flagNext = true;
- printDebug("" + array[xx + 1][yy]);
- printDebugBool(bolli[xx + 1][yy]);
- printDebugPoint(new Point(xx + 1, yy));
- if (array[xx + 1][yy] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx + 1, yy));
- printDebugPoint(new Point(xx + 1, yy));
- } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
- bolli[xx][yy - 1] = true;
- flagNext = true;
- printDebug("" + array[xx][yy - 1]);
- printDebugBool(bolli[xx][yy - 1]);
- printDebugPoint(new Point(xx, yy - 1));
- if (array[xx][yy - 1] == 'E') {
- found = true;
- break;
- }
- stack.push(new Point(xx, yy - 1));
- printDebugPoint(new Point(xx, yy - 1));
- }
- }
- }
- stackSize = stack.size();
- printDebug("here is my stack");
- printDebug(Integer.toString(stackSize) + "sizy");
- stackSize++;
- int newSize = stackSize - 1;
- returnedArray = new int[stackSize][2];
- int counterFull = 0;
- if (!(stackSize == 0 || stackSize == 1)) {
- Point pointy = new Point(yEnd, xEnd);
- // pointy = (Point) stack.pop();
- printDebugPoint(pointy);
- returnedArray[newSize][0] = pointy.x;
- returnedArray[newSize][1] = pointy.y;
- counterFull++;
- }
- while (!(stack.isEmpty())) {
- Point pointy = new Point();
- // if(counterFull)
- pointy = (Point) stack.pop();
- printDebugPoint(pointy);
- returnedArray[newSize - counterFull][0] = pointy.x;
- returnedArray[newSize - counterFull][1] = pointy.y;
- counterFull++;
- }
- printDebug("output ya yoyo");
- for (int i = 0; i < stackSize; i++) {
- printDebug(Integer.toString(returnedArray[i][0]) + " " + Integer.toString(returnedArray[i][1]));
- }
- }
- }
- } catch (Exception e) {
- System.out.println("momken trmi exp");
- }
- return returnedArray;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement