Advertisement
Aiaa

maze

May 18th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.72 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.maze.cs04;
  2.  
  3. import java.awt.Point;
  4.  
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.util.Iterator;
  9. import java.util.Queue;
  10. import java.util.Scanner;
  11.  
  12. import eg.edu.alexu.csd.datastructure.maze.IMazeSolver;
  13. import eg.edu.alexu.csd.datastructure.linkedList.cs04.SinglyLinkedList;
  14.  
  15. import eg.edu.alexu.csd.datastructure.queue.ILinkedBased;
  16. import eg.edu.alexu.csd.datastructure.queue.IQueue;
  17. import eg.edu.alexu.csd.datastructure.queue.cs04.QueueList;
  18. import eg.edu.alexu.csd.datastructure.stack.cs04.Stack;
  19.  
  20. public class Maze implements IMazeSolver {
  21. int queueSize;
  22. int stackSize;
  23. QueueList queue = new QueueList();
  24. QueueList queueR = new QueueList();
  25. Stack stack = new Stack();
  26. Scanner scan = new Scanner(System.in);
  27. static String rows;
  28. static String cols;
  29. static int rowsInt;
  30. static int colInt;
  31. static boolean flagNext = false;
  32. static char[][] array1;
  33. static char[][] array;
  34. static boolean[][] bolli;
  35. static int xStart;
  36. static int yStart;
  37. static int xEnd;
  38. static int yEnd;
  39. static boolean notFound = false;
  40. static boolean boundries = false;
  41. static boolean flag = false;
  42. static boolean flagFirst = false;
  43. static boolean found = false;
  44.  
  45. public void printDebugInt(int i) {
  46. System.out.println(i);
  47. }
  48.  
  49. public void printDebug(String s) {
  50. System.out.println(s);
  51. }
  52.  
  53. private void printDebugPoint(Point point) {
  54. // TODO Auto-generated method stub
  55. System.out.println(point);
  56.  
  57. }
  58.  
  59. private void printDebugBool(boolean b) {
  60. // TODO Auto-generated method stub
  61. System.out.println(b);
  62. }
  63.  
  64. public static void main(String[] args) throws IOException {
  65. Maze mazy = new Maze();
  66. File s = (new File("E:\\2nd_sem_1st_year\\Code Masri\\java projects\\maze Try\\src\\aia.txt"));
  67. mazy.solveBFS(s);
  68. }
  69.  
  70. @Override
  71. public int[][] solveBFS(File maze) {
  72. // TODO Auto-generated method stub
  73.  
  74. int[][] returnedArray = null;
  75. Scanner s;
  76. int counter = 0;
  77. try {
  78. s = new Scanner(maze);
  79. while (s.hasNext()) {
  80.  
  81. // System.out.println(str);
  82. boolean flag1 = false;
  83. if (counter == 0) {
  84. String str = s.nextLine();
  85. printDebug(str);
  86. String tempy = null;
  87. for (int i = 0; i < str.length(); i++) {
  88. if (tempy == null && str.charAt(i) != ' ') {
  89. tempy = "" + str.charAt(i);
  90.  
  91. while (i + 1 != str.length() && str.charAt(i + 1) != ' ' && (i + 1 != str.length() - 1)) {
  92. tempy += "" + str.charAt(i + 1);
  93. i++;
  94. }
  95. if (flag1 == false) {
  96. rows = tempy;
  97. printDebug("rows" + rows);
  98. tempy = null;
  99. flag1 = true;
  100. } else {
  101. cols = tempy;
  102. printDebug("cols" + cols);
  103. tempy = null;
  104. }
  105.  
  106. }
  107. }
  108. rowsInt = Integer.parseInt(rows);
  109. colInt = Integer.parseInt(cols);
  110. // array1 = new char [rowsInt][colInt];
  111. array = new char[rowsInt][colInt];
  112. bolli = new boolean[rowsInt][colInt];
  113. printDebug(Integer.toString(rowsInt));
  114. printDebug(Integer.toString(colInt));
  115. counter++;
  116.  
  117. } else {
  118. printDebug("req array");
  119. for (int x = 0; x < rowsInt; x++) {
  120. String text = s.nextLine().trim();
  121. printDebug(text);
  122. for (int y = 0; y < colInt; y++) {
  123.  
  124. array[x][y] = text.charAt(y);
  125. // array[x][y] = array1[x][y] ;
  126. printDebug("" + array[x][y]);
  127. // printDebug(""+array[x][y]);
  128. if (array[x][y] == 'S') {
  129. yStart = x;
  130. xStart = y;
  131. } else if (array[x][y] == 'E') {
  132. yEnd = x;
  133. xEnd = y;
  134. }
  135. }
  136.  
  137. }
  138. // array = array1 ;
  139. printDebug("*******");
  140. // printDebug(""+array[0][1]);
  141. for (int m = 0; m < rowsInt; m++) {
  142. for (int mm = 0; mm < colInt; mm++) {
  143.  
  144. printDebug("" + array[m][mm]);
  145. }
  146.  
  147. }
  148. // hena
  149. int yy = xStart;
  150. int xx = yStart;
  151.  
  152. queue.enqueue(new Point(xx, yy));
  153. Point point2 = new Point(xx, yy);
  154. printDebugPoint(point2);
  155. // bolli[xx][yy] = true;
  156. flagFirst = true;
  157. while (notFound == false && boundries == false && found == false) {
  158. Point pointy = new Point();
  159.  
  160. if (queue.size() == 0) {
  161. // break;
  162. throw new RuntimeException("empty ya yoyo");
  163.  
  164. }
  165. // if (flagNext == false && flagFirst == false) {
  166. // pointy = (Point) queue.dequeue();
  167. // pointy = (Point) queue.peek();
  168. // } else {
  169. // pointy = (Point) queue.peek();
  170. // }
  171.  
  172.  
  173. flagFirst = false;
  174. flagNext = false;
  175. pointy = (Point) queue.dequeue();
  176. xx = pointy.x;
  177.  
  178. yy = pointy.y;
  179. printDebugPoint(pointy);
  180.  
  181. if (bolli[xx][yy] == false) {
  182.  
  183. queueR.enqueue(pointy);
  184. bolli[xx][yy] = true;
  185. }
  186. printDebug("" + array[xx][yy]);
  187. if (xx == 0 && yy + 1 == colInt) {
  188. if (array[xx + 1][yy] != '#' && bolli[xx + 1][yy] == false) {
  189. flagNext = true;
  190. printDebug("" + array[xx + 1][yy]);
  191. printDebugBool(bolli[xx + 1][yy]);
  192. printDebugPoint(new Point(xx + 1, yy));
  193.  
  194. if (array[xx + 1][yy] == 'E') {
  195. found = true;
  196. break;
  197. }
  198. queue.enqueue(new Point(xx + 1, yy));
  199. printDebugPoint(new Point(xx + 1, yy));
  200. printDebugPoint(new Point(xx + 1, yy));
  201.  
  202. }
  203. if (array[xx][yy - 1] != '#') {
  204.  
  205. printDebug("" + array[xx][yy - 1]);
  206. flagNext = true;
  207. printDebugBool(bolli[xx][yy - 1]);
  208. printDebugPoint(new Point(xx, yy - 1));
  209. if (array[xx][yy - 1] == 'E') {
  210. found = true;
  211. break;
  212. }
  213. queue.enqueue(new Point(xx, yy - 1));
  214. printDebugPoint(new Point(xx, yy - 1));
  215.  
  216. }
  217.  
  218. } else if (xx == 0 && yy == 0) {
  219.  
  220. if (array[xx][yy + 1] != '#' && bolli[xx][yy + 1] == false) {
  221.  
  222. flagNext = true;
  223. printDebug("" + array[xx][yy + 1]);
  224. printDebugBool(bolli[xx][yy + 1]);
  225. printDebugPoint(new Point(xx, yy + 1));
  226. if (array[xx][yy + 1] == 'E') {
  227. found = true;
  228. break;
  229. }
  230. queue.enqueue(new Point(xx, yy + 1));
  231. printDebugPoint(new Point(xx, yy + 1));
  232. } if (array[xx + 1][yy] != '#'&& bolli[xx + 1][yy] == false) {
  233.  
  234. flagNext = true;
  235. printDebug("" + array[xx + 1][yy]);
  236. printDebugBool(bolli[xx + 1][yy]);
  237. printDebugPoint(new Point(xx + 1, yy));
  238. if (array[xx + 1][yy] == 'E') {
  239. found = true;
  240. break;
  241. }
  242. queue.enqueue(new Point(xx + 1, yy));
  243. printDebugPoint(new Point(xx + 1, yy));
  244. }
  245. } else if (yy == 0 && xx + 1 == rowsInt) {
  246. if (array[xx - 1][yy] != '#'&& bolli[xx - 1][yy] == false) {
  247.  
  248. flagNext = true;
  249. printDebug("" + array[xx - 1][yy]);
  250. printDebugBool(bolli[xx - 1][yy]);
  251. printDebugPoint(new Point(xx - 1, yy));
  252. if (array[xx - 1][yy] == 'E') {
  253. found = true;
  254. break;
  255. }
  256. queue.enqueue(new Point(xx - 1, yy));
  257. printDebugPoint(new Point(xx - 1, yy));
  258. } if (array[xx][yy + 1] != '#'&& bolli[xx][yy + 1] == false) {
  259.  
  260. flagNext = true;
  261. printDebug("" + array[xx][yy + 1]);
  262. printDebugBool(bolli[xx][yy + 1]);
  263. printDebugPoint(new Point(xx, yy + 1));
  264. if (array[xx][yy + 1] == 'E') {
  265. found = true;
  266. break;
  267. }
  268. queue.enqueue(new Point(xx, yy + 1));
  269. printDebugPoint(new Point(xx, yy + 1));
  270. }
  271.  
  272. } else if (yy + 1 == colInt && xx + 1 == rowsInt) {
  273.  
  274. if (array[xx - 1][yy] != '#'&& bolli[xx - 1][yy] == false) {
  275.  
  276. flagNext = true;
  277. printDebug("" + array[xx - 1][yy]);
  278. printDebugBool(bolli[xx - 1][yy]);
  279. printDebugPoint(new Point(xx - 1, yy));
  280. if (array[xx - 1][yy] == 'E') {
  281. found = true;
  282. break;
  283. }
  284. queue.enqueue(new Point(xx - 1, yy));
  285. printDebugPoint(new Point(xx - 1, yy));
  286. } if (array[xx][yy - 1] != '#'&& bolli[xx][yy - 1] == false) {
  287.  
  288. flagNext = true;
  289. printDebug("" + array[xx][yy - 1]);
  290. printDebugBool(bolli[xx][yy - 1]);
  291. printDebugPoint(new Point(xx, yy - 1));
  292. if (array[xx][yy - 1] == 'E') {
  293. found = true;
  294. break;
  295. }
  296. queue.enqueue(new Point(xx, yy - 1));
  297. printDebugPoint(new Point(xx, yy - 1));
  298. }
  299. } else if (yy + 1 == colInt && (xx != 0 && xx + 1 != rowsInt)) {
  300. if (array[xx - 1][yy] != '#'&& bolli[xx - 1][yy] == false) {
  301.  
  302. flagNext = true;
  303. printDebug("" + array[xx - 1][yy]);
  304. printDebugBool(bolli[xx - 1][yy]);
  305. printDebugPoint(new Point(xx - 1, yy));
  306. if (array[xx - 1][yy] == 'E') {
  307. found = true;
  308. break;
  309. }
  310. queue.enqueue(new Point(xx - 1, yy));
  311. printDebugPoint(new Point(xx - 1, yy));
  312. } if (array[xx + 1][yy] != '#'&& bolli [xx + 1][yy]== false) {
  313.  
  314. flagNext = true;
  315. printDebug("" + array[xx + 1][yy]);
  316. printDebugBool(bolli[xx + 1][yy]);
  317. printDebugPoint(new Point(xx + 1, yy));
  318. if (array[xx + 1][yy] == 'E') {
  319. found = true;
  320. break;
  321. }
  322. queue.enqueue(new Point(xx + 1, yy));
  323. printDebugPoint(new Point(xx + 1, yy));
  324. } if (array[xx][yy - 1] != '#'&& bolli[xx][yy - 1] == false) {
  325.  
  326. flagNext = true;
  327. printDebug("" + array[xx][yy - 1]);
  328. printDebugBool(bolli[xx][yy - 1]);
  329. printDebugPoint(new Point(xx, yy - 1));
  330. if (array[xx][yy - 1] == 'E') {
  331. found = true;
  332. break;
  333. }
  334. queue.enqueue(new Point(xx, yy - 1));
  335. printDebugPoint(new Point(xx, yy - 1));
  336. }
  337.  
  338. } else if (yy == 0 && (xx != 0 && xx + 1 != rowsInt)) {
  339. if (array[xx - 1][yy] != '#'&& bolli [xx - 1][yy]== false) {
  340.  
  341. flagNext = true;
  342. printDebug("" + array[xx - 1][yy]);
  343. printDebugBool(bolli[xx - 1][yy]);
  344. printDebugPoint(new Point(xx - 1, yy));
  345. if (array[xx - 1][yy] == 'E') {
  346. found = true;
  347. break;
  348. }
  349. queue.enqueue(new Point(xx - 1, yy));
  350. printDebugPoint(new Point(xx - 1, yy));
  351. } if (array[xx][yy + 1] != '#'&& bolli [xx][yy + 1]== false) {
  352.  
  353. flagNext = true;
  354. printDebug("" + array[xx][yy + 1]);
  355. printDebugBool(bolli[xx][yy + 1]);
  356. printDebugPoint(new Point(xx, yy + 1));
  357. if (array[xx][yy + 1] == 'E') {
  358. found = true;
  359. break;
  360. }
  361. queue.enqueue(new Point(xx, yy + 1));
  362. printDebugPoint(new Point(xx, yy + 1));
  363. } if (array[xx + 1][yy] != '#'&& bolli[xx + 1][yy] == false) {
  364.  
  365. flagNext = true;
  366. printDebug("" + array[xx + 1][yy]);
  367. printDebugBool(bolli[xx + 1][yy]);
  368. printDebugPoint(new Point(xx + 1, yy));
  369. if (array[xx + 1][yy] == 'E') {
  370. found = true;
  371. break;
  372. }
  373. queue.enqueue(new Point(xx + 1, yy));
  374. printDebugPoint(new Point(xx + 1, yy));
  375. }
  376. } else if (xx + 1 == rowsInt && (yy != 0 && yy + 1 != colInt)) {
  377. if (array[xx - 1][yy] != '#'&& bolli[xx - 1][yy] == false) {
  378.  
  379. flagNext = true;
  380. printDebug("" + array[xx - 1][yy]);
  381. printDebugBool(bolli[xx - 1][yy]);
  382. printDebugPoint(new Point(xx - 1, yy));
  383. if (array[xx - 1][yy] == 'E') {
  384. found = true;
  385. break;
  386. }
  387. queue.enqueue(new Point(xx - 1, yy));
  388. printDebugPoint(new Point(xx - 1, yy));
  389. } if (array[xx][yy + 1] != '#'&& bolli [xx][yy + 1]== false) {
  390.  
  391. flagNext = true;
  392. if (array[xx][yy + 1] == 'E') {
  393. found = true;
  394. break;
  395. }
  396. queue.enqueue(new Point(xx, yy + 1));
  397. printDebugPoint(new Point(xx, yy + 1));
  398. } if (array[xx][yy - 1] != '#'&& bolli[xx][yy - 1] == false) {
  399.  
  400. flagNext = true;
  401. printDebug("" + array[xx][yy - 1]);
  402. printDebugBool(bolli[xx][yy - 1]);
  403. printDebugPoint(new Point(xx, yy - 1));
  404. if (array[xx][yy - 1] == 'E') {
  405. found = true;
  406. break;
  407. }
  408. queue.enqueue(new Point(xx, yy - 1));
  409. printDebugPoint(new Point(xx, yy - 1));
  410. }
  411. } else if (xx == 0 && (yy != 0 && yy + 1 != colInt)) {
  412.  
  413. if (array[xx][yy + 1] != '#'&& bolli[xx][yy + 1] == false) {
  414.  
  415. flagNext = true;
  416. printDebug("" + array[xx][yy + 1]);
  417. printDebugBool(bolli[xx][yy + 1]);
  418. printDebugPoint(new Point(xx, yy + 1));
  419. if (array[xx][yy + 1] == 'E') {
  420. found = true;
  421. break;
  422. }
  423. queue.enqueue(new Point(xx, yy + 1));
  424. printDebugPoint(new Point(xx, yy + 1));
  425. } if (array[xx + 1][yy] != '#'&& bolli [xx + 1][yy]== false) {
  426.  
  427. flagNext = true;
  428. printDebug("" + array[xx + 1][yy]);
  429. printDebugBool(bolli[xx + 1][yy]);
  430. printDebugPoint(new Point(xx + 1, yy));
  431. if (array[xx + 1][yy] == 'E') {
  432. found = true;
  433. break;
  434. }
  435. queue.enqueue(new Point(xx + 1, yy));
  436. printDebugPoint(new Point(xx + 1, yy));
  437. } if (array[xx][yy - 1] != '#'&& bolli[xx][yy - 1] == false) {
  438.  
  439. flagNext = true;
  440. printDebug("" + array[xx][yy - 1]);
  441. printDebugBool(bolli[xx][yy - 1]);
  442. printDebugPoint(new Point(xx, yy - 1));
  443. if (array[xx][yy - 1] == 'E') {
  444. found = true;
  445. break;
  446. }
  447. queue.enqueue(new Point(xx, yy - 1));
  448. printDebugPoint(new Point(xx, yy - 1));
  449. }
  450.  
  451. } else {
  452.  
  453. if (array[xx - 1][yy] != '#'&& bolli[xx - 1][yy] == false) {
  454.  
  455. flagNext = true;
  456. printDebug("" + array[xx - 1][yy]);
  457. printDebugBool(bolli[xx - 1][yy]);
  458. printDebugPoint(new Point(xx - 1, yy));
  459. if (array[xx - 1][yy] == 'E') {
  460. found = true;
  461. break;
  462. }
  463. queue.enqueue(new Point(xx - 1, yy));
  464. printDebugPoint(new Point(xx - 1, yy));
  465. } if (array[xx][yy + 1] != '#'&& bolli[xx][yy + 1] == false) {
  466.  
  467. flagNext = true;
  468. printDebug("" + array[xx][yy + 1]);
  469. printDebugBool(bolli[xx][yy + 1]);
  470. printDebugPoint(new Point(xx, yy + 1));
  471. if (array[xx][yy + 1] == 'E') {
  472. found = true;
  473. break;
  474. }
  475. queue.enqueue(new Point(xx, yy + 1));
  476. printDebugPoint(new Point(xx, yy + 1));
  477. } if (array[xx + 1][yy] != '#'&& bolli[xx + 1][yy] == false) {
  478.  
  479. flagNext = true;
  480. printDebug("" + array[xx + 1][yy]);
  481. printDebugBool(bolli[xx + 1][yy]);
  482. printDebugPoint(new Point(xx + 1, yy));
  483. if (array[xx + 1][yy] == 'E') {
  484. found = true;
  485. break;
  486. }
  487. queue.enqueue(new Point(xx + 1, yy));
  488. printDebugPoint(new Point(xx + 1, yy));
  489. } if (array[xx][yy - 1] != '#'&& bolli[xx][yy - 1] == false) {
  490.  
  491. flagNext = true;
  492. printDebug("" + array[xx][yy - 1]);
  493. printDebugBool(bolli[xx][yy - 1]);
  494. printDebugPoint(new Point(xx, yy - 1));
  495. if (array[xx][yy - 1] == 'E') {
  496. found = true;
  497. break;
  498. }
  499. queue.enqueue(new Point(xx, yy - 1));
  500. printDebugPoint(new Point(xx, yy - 1));
  501. }
  502. }
  503. }
  504. //Point newpoint = new Point();
  505.  
  506. queueSize = queueR.size();
  507.  
  508. printDebug("here is my queue");
  509.  
  510. printDebug(Integer.toString(queueSize) + "sizy");
  511. //queueSize++;
  512. int newSize = queueSize ;
  513. returnedArray = new int[queueSize][2];
  514.  
  515. int counterFull = 0;
  516.  
  517. // if (!(queueSize == 0 || queueSize == 1)) {
  518. // Point pointy = new Point(yEnd, xEnd);
  519. // // pointy = (Point) queue.dequeue();
  520. // printDebugPoint(pointy);
  521. // returnedArray[newSize][0] = pointy.x;
  522. // returnedArray[newSize][1] = pointy.y;
  523. // counterFull++;
  524. // }
  525.  
  526. while (!(queueR.isEmpty())) {
  527. Point pointy = new Point();
  528. // if(counterFull)
  529. pointy = (Point) queueR.dequeue();
  530. printDebugPoint(pointy);
  531. returnedArray[ counterFull][0] = pointy.x;
  532. returnedArray[ counterFull][1] = pointy.y;
  533. counterFull++;
  534. }
  535.  
  536. printDebug("output ya yoyo");
  537. for (int i = 0; i < queueSize; i++) {
  538.  
  539. printDebug(Integer.toString(returnedArray[i][0]) + " " + Integer.toString(returnedArray[i][1]));
  540.  
  541. }
  542. }
  543.  
  544. }
  545.  
  546. } catch (Exception e) {
  547. System.out.println("momken trmi exp queue");
  548. }
  549.  
  550. return returnedArray;
  551.  
  552. }
  553.  
  554. @Override
  555. public int[][] solveDFS(File maze) {
  556. // TODO Auto-generated method stub
  557. int[][] returnedArray = null;
  558. Scanner s;
  559. int counter = 0;
  560. try {
  561. s = new Scanner(maze);
  562. while (s.hasNext()) {
  563.  
  564. // System.out.println(str);
  565. boolean flag1 = false;
  566. if (counter == 0) {
  567. String str = s.nextLine();
  568. printDebug(str);
  569. String tempy = null;
  570. for (int i = 0; i < str.length(); i++) {
  571. if (tempy == null && str.charAt(i) != ' ') {
  572. tempy = "" + str.charAt(i);
  573.  
  574. while (i + 1 != str.length() && str.charAt(i + 1) != ' ' && (i + 1 != str.length() - 1)) {
  575. tempy += "" + str.charAt(i + 1);
  576. i++;
  577. }
  578. if (flag1 == false) {
  579. rows = tempy;
  580. printDebug("rows" + rows);
  581. tempy = null;
  582. flag1 = true;
  583. } else {
  584. cols = tempy;
  585. printDebug("cols" + cols);
  586. tempy = null;
  587. }
  588.  
  589. }
  590. }
  591. rowsInt = Integer.parseInt(rows);
  592. colInt = Integer.parseInt(cols);
  593. // array1 = new char [rowsInt][colInt];
  594. array = new char[rowsInt][colInt];
  595. bolli = new boolean[rowsInt][colInt];
  596. printDebug(Integer.toString(rowsInt));
  597. printDebug(Integer.toString(colInt));
  598. counter++;
  599.  
  600. } else {
  601. printDebug("req array");
  602. for (int x = 0; x < rowsInt; x++) {
  603. String text = s.nextLine().trim();
  604. printDebug(text);
  605. for (int y = 0; y < colInt; y++) {
  606.  
  607. array[x][y] = text.charAt(y);
  608. // array[x][y] = array1[x][y] ;
  609. printDebug("" + array[x][y]);
  610. // printDebug(""+array[x][y]);
  611. if (array[x][y] == 'S') {
  612. yStart = x;
  613. xStart = y;
  614. } else if (array[x][y] == 'E') {
  615. yEnd = x;
  616. xEnd = y;
  617. }
  618. }
  619.  
  620. }
  621. // array = array1 ;
  622. printDebug("*******");
  623. // printDebug(""+array[0][1]);
  624. for (int m = 0; m < rowsInt; m++) {
  625. for (int mm = 0; mm < colInt; mm++) {
  626.  
  627. printDebug("" + array[m][mm]);
  628. }
  629.  
  630. }
  631. // hena
  632. int yy = xStart;
  633. int xx = yStart;
  634. stack.push(new Point(xx, yy));
  635. Point point2 = new Point(xx, yy);
  636. printDebugPoint(point2);
  637. bolli[xx][yy] = true;
  638. flagFirst = true;
  639. while (notFound == false && boundries == false && found == false) {
  640. Point pointy = new Point();
  641.  
  642. if (stack.size() == 0) {
  643. // break;
  644. throw new RuntimeException("empty ya yoyo");
  645.  
  646. }
  647. if (flagNext == false && flagFirst == false) {
  648. pointy = (Point) stack.pop();
  649. pointy = (Point) stack.peek();
  650. } else {
  651. pointy = (Point) stack.peek();
  652. }
  653. flagFirst = false;
  654. flagNext = false;
  655. printDebugPoint(pointy);
  656. xx = pointy.x;
  657.  
  658. yy = pointy.y;
  659.  
  660. printDebug("" + array[xx][yy]);
  661. if (xx == 0 && yy + 1 == colInt) {
  662. if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
  663. flagNext = true;
  664. printDebug("" + array[xx + 1][yy]);
  665. printDebugBool(bolli[xx + 1][yy]);
  666. printDebugPoint(new Point(xx + 1, yy));
  667. bolli[xx + 1][yy] = true;
  668. if (array[xx + 1][yy] == 'E') {
  669. found = true;
  670. break;
  671. }
  672. stack.push(new Point(xx + 1, yy));
  673. printDebugPoint(new Point(xx + 1, yy));
  674. printDebugPoint(new Point(xx + 1, yy));
  675.  
  676. } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
  677. bolli[xx][yy - 1] = true;
  678. printDebug("" + array[xx][yy - 1]);
  679. flagNext = true;
  680. printDebugBool(bolli[xx][yy - 1]);
  681. printDebugPoint(new Point(xx, yy - 1));
  682. if (array[xx][yy - 1] == 'E') {
  683. found = true;
  684. break;
  685. }
  686. stack.push(new Point(xx, yy - 1));
  687. printDebugPoint(new Point(xx, yy - 1));
  688.  
  689. }
  690.  
  691. } else if (xx == 0 && yy == 0) {
  692.  
  693. if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
  694. bolli[xx][yy + 1] = true;
  695. flagNext = true;
  696. printDebug("" + array[xx][yy + 1]);
  697. printDebugBool(bolli[xx][yy + 1]);
  698. printDebugPoint(new Point(xx, yy + 1));
  699. if (array[xx][yy + 1] == 'E') {
  700. found = true;
  701. break;
  702. }
  703. stack.push(new Point(xx, yy + 1));
  704. printDebugPoint(new Point(xx, yy + 1));
  705. } else if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
  706. bolli[xx + 1][yy] = true;
  707. flagNext = true;
  708. printDebug("" + array[xx + 1][yy]);
  709. printDebugBool(bolli[xx + 1][yy]);
  710. printDebugPoint(new Point(xx + 1, yy));
  711. if (array[xx + 1][yy] == 'E') {
  712. found = true;
  713. break;
  714. }
  715. stack.push(new Point(xx + 1, yy));
  716. printDebugPoint(new Point(xx + 1, yy));
  717. }
  718. } else if (yy == 0 && xx + 1 == rowsInt) {
  719. if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
  720. bolli[xx - 1][yy] = true;
  721. flagNext = true;
  722. printDebug("" + array[xx - 1][yy]);
  723. printDebugBool(bolli[xx - 1][yy]);
  724. printDebugPoint(new Point(xx - 1, yy));
  725. if (array[xx - 1][yy] == 'E') {
  726. found = true;
  727. break;
  728. }
  729. stack.push(new Point(xx - 1, yy));
  730. printDebugPoint(new Point(xx - 1, yy));
  731. } else if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
  732. bolli[xx][yy + 1] = true;
  733. flagNext = true;
  734. printDebug("" + array[xx][yy + 1]);
  735. printDebugBool(bolli[xx][yy + 1]);
  736. printDebugPoint(new Point(xx, yy + 1));
  737. if (array[xx][yy + 1] == 'E') {
  738. found = true;
  739. break;
  740. }
  741. stack.push(new Point(xx, yy + 1));
  742. printDebugPoint(new Point(xx, yy + 1));
  743. }
  744.  
  745. } else if (yy + 1 == colInt && xx + 1 == rowsInt) {
  746.  
  747. if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
  748. bolli[xx - 1][yy] = true;
  749. flagNext = true;
  750. printDebug("" + array[xx - 1][yy]);
  751. printDebugBool(bolli[xx - 1][yy]);
  752. printDebugPoint(new Point(xx - 1, yy));
  753. if (array[xx - 1][yy] == 'E') {
  754. found = true;
  755. break;
  756. }
  757. stack.push(new Point(xx - 1, yy));
  758. printDebugPoint(new Point(xx - 1, yy));
  759. } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
  760. bolli[xx][yy - 1] = true;
  761. flagNext = true;
  762. printDebug("" + array[xx][yy - 1]);
  763. printDebugBool(bolli[xx][yy - 1]);
  764. printDebugPoint(new Point(xx, yy - 1));
  765. if (array[xx][yy - 1] == 'E') {
  766. found = true;
  767. break;
  768. }
  769. stack.push(new Point(xx, yy - 1));
  770. printDebugPoint(new Point(xx, yy - 1));
  771. }
  772. } else if (yy + 1 == colInt && (xx != 0 && xx + 1 != rowsInt)) {
  773. if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
  774. bolli[xx - 1][yy] = true;
  775. flagNext = true;
  776. printDebug("" + array[xx - 1][yy]);
  777. printDebugBool(bolli[xx - 1][yy]);
  778. printDebugPoint(new Point(xx - 1, yy));
  779. if (array[xx - 1][yy] == 'E') {
  780. found = true;
  781. break;
  782. }
  783. stack.push(new Point(xx - 1, yy));
  784. printDebugPoint(new Point(xx - 1, yy));
  785. } else if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
  786. bolli[xx + 1][yy] = true;
  787. flagNext = true;
  788. printDebug("" + array[xx + 1][yy]);
  789. printDebugBool(bolli[xx + 1][yy]);
  790. printDebugPoint(new Point(xx + 1, yy));
  791. if (array[xx + 1][yy] == 'E') {
  792. found = true;
  793. break;
  794. }
  795. stack.push(new Point(xx + 1, yy));
  796. printDebugPoint(new Point(xx + 1, yy));
  797. } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
  798. bolli[xx][yy - 1] = true;
  799. flagNext = true;
  800. printDebug("" + array[xx][yy - 1]);
  801. printDebugBool(bolli[xx][yy - 1]);
  802. printDebugPoint(new Point(xx, yy - 1));
  803. if (array[xx][yy - 1] == 'E') {
  804. found = true;
  805. break;
  806. }
  807. stack.push(new Point(xx, yy - 1));
  808. printDebugPoint(new Point(xx, yy - 1));
  809. }
  810.  
  811. } else if (yy == 0 && (xx != 0 && xx + 1 != rowsInt)) {
  812. if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
  813. bolli[xx - 1][yy] = true;
  814. flagNext = true;
  815. printDebug("" + array[xx - 1][yy]);
  816. printDebugBool(bolli[xx - 1][yy]);
  817. printDebugPoint(new Point(xx - 1, yy));
  818. if (array[xx - 1][yy] == 'E') {
  819. found = true;
  820. break;
  821. }
  822. stack.push(new Point(xx - 1, yy));
  823. printDebugPoint(new Point(xx - 1, yy));
  824. } else if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
  825. bolli[xx][yy + 1] = true;
  826. flagNext = true;
  827. printDebug("" + array[xx][yy + 1]);
  828. printDebugBool(bolli[xx][yy + 1]);
  829. printDebugPoint(new Point(xx, yy + 1));
  830. if (array[xx][yy + 1] == 'E') {
  831. found = true;
  832. break;
  833. }
  834. stack.push(new Point(xx, yy + 1));
  835. printDebugPoint(new Point(xx, yy + 1));
  836. } else if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
  837. bolli[xx + 1][yy] = true;
  838. flagNext = true;
  839. printDebug("" + array[xx + 1][yy]);
  840. printDebugBool(bolli[xx + 1][yy]);
  841. printDebugPoint(new Point(xx + 1, yy));
  842. if (array[xx + 1][yy] == 'E') {
  843. found = true;
  844. break;
  845. }
  846. stack.push(new Point(xx + 1, yy));
  847. printDebugPoint(new Point(xx + 1, yy));
  848. }
  849. } else if (xx + 1 == rowsInt && (yy != 0 && yy + 1 != colInt)) {
  850. if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
  851. bolli[xx - 1][yy] = true;
  852. flagNext = true;
  853. printDebug("" + array[xx - 1][yy]);
  854. printDebugBool(bolli[xx - 1][yy]);
  855. printDebugPoint(new Point(xx - 1, yy));
  856. if (array[xx - 1][yy] == 'E') {
  857. found = true;
  858. break;
  859. }
  860. stack.push(new Point(xx - 1, yy));
  861. printDebugPoint(new Point(xx - 1, yy));
  862. } else if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
  863. bolli[xx][yy + 1] = true;
  864. flagNext = true;
  865. if (array[xx][yy + 1] == 'E') {
  866. found = true;
  867. break;
  868. }
  869. stack.push(new Point(xx, yy + 1));
  870. printDebugPoint(new Point(xx, yy + 1));
  871. } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
  872. bolli[xx][yy - 1] = true;
  873. flagNext = true;
  874. printDebug("" + array[xx][yy - 1]);
  875. printDebugBool(bolli[xx][yy - 1]);
  876. printDebugPoint(new Point(xx, yy - 1));
  877. if (array[xx][yy - 1] == 'E') {
  878. found = true;
  879. break;
  880. }
  881. stack.push(new Point(xx, yy - 1));
  882. printDebugPoint(new Point(xx, yy - 1));
  883. }
  884. } else if (xx == 0 && (yy != 0 && yy + 1 != colInt)) {
  885.  
  886. if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
  887. bolli[xx][yy + 1] = true;
  888.  
  889. flagNext = true;
  890. printDebug("" + array[xx][yy + 1]);
  891. printDebugBool(bolli[xx][yy + 1]);
  892. printDebugPoint(new Point(xx, yy + 1));
  893. if (array[xx][yy + 1] == 'E') {
  894. found = true;
  895. break;
  896. }
  897. stack.push(new Point(xx, yy + 1));
  898. printDebugPoint(new Point(xx, yy + 1));
  899. } else if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
  900. bolli[xx + 1][yy] = true;
  901. flagNext = true;
  902. printDebug("" + array[xx + 1][yy]);
  903. printDebugBool(bolli[xx + 1][yy]);
  904. printDebugPoint(new Point(xx + 1, yy));
  905. if (array[xx + 1][yy] == 'E') {
  906. found = true;
  907. break;
  908. }
  909. stack.push(new Point(xx + 1, yy));
  910. printDebugPoint(new Point(xx + 1, yy));
  911. } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
  912. bolli[xx][yy - 1] = true;
  913. flagNext = true;
  914. printDebug("" + array[xx][yy - 1]);
  915. printDebugBool(bolli[xx][yy - 1]);
  916. printDebugPoint(new Point(xx, yy - 1));
  917. if (array[xx][yy - 1] == 'E') {
  918. found = true;
  919. break;
  920. }
  921. stack.push(new Point(xx, yy - 1));
  922. printDebugPoint(new Point(xx, yy - 1));
  923. }
  924.  
  925. } else {
  926.  
  927. if (bolli[xx - 1][yy] == false && array[xx - 1][yy] != '#') {
  928. bolli[xx - 1][yy] = true;
  929. flagNext = true;
  930. printDebug("" + array[xx - 1][yy]);
  931. printDebugBool(bolli[xx - 1][yy]);
  932. printDebugPoint(new Point(xx - 1, yy));
  933. if (array[xx - 1][yy] == 'E') {
  934. found = true;
  935. break;
  936. }
  937. stack.push(new Point(xx - 1, yy));
  938. printDebugPoint(new Point(xx - 1, yy));
  939. } else if (bolli[xx][yy + 1] == false && array[xx][yy + 1] != '#') {
  940. bolli[xx][yy + 1] = true;
  941. flagNext = true;
  942. printDebug("" + array[xx][yy + 1]);
  943. printDebugBool(bolli[xx][yy + 1]);
  944. printDebugPoint(new Point(xx, yy + 1));
  945. if (array[xx][yy + 1] == 'E') {
  946. found = true;
  947. break;
  948. }
  949. stack.push(new Point(xx, yy + 1));
  950. printDebugPoint(new Point(xx, yy + 1));
  951. } else if (bolli[xx + 1][yy] == false && array[xx + 1][yy] != '#') {
  952. bolli[xx + 1][yy] = true;
  953. flagNext = true;
  954. printDebug("" + array[xx + 1][yy]);
  955. printDebugBool(bolli[xx + 1][yy]);
  956. printDebugPoint(new Point(xx + 1, yy));
  957. if (array[xx + 1][yy] == 'E') {
  958. found = true;
  959. break;
  960. }
  961. stack.push(new Point(xx + 1, yy));
  962. printDebugPoint(new Point(xx + 1, yy));
  963. } else if (bolli[xx][yy - 1] == false && array[xx][yy - 1] != '#') {
  964. bolli[xx][yy - 1] = true;
  965. flagNext = true;
  966. printDebug("" + array[xx][yy - 1]);
  967. printDebugBool(bolli[xx][yy - 1]);
  968. printDebugPoint(new Point(xx, yy - 1));
  969. if (array[xx][yy - 1] == 'E') {
  970. found = true;
  971. break;
  972. }
  973. stack.push(new Point(xx, yy - 1));
  974. printDebugPoint(new Point(xx, yy - 1));
  975. }
  976. }
  977. }
  978. stackSize = stack.size();
  979.  
  980. printDebug("here is my stack");
  981.  
  982. printDebug(Integer.toString(stackSize) + "sizy");
  983. stackSize++;
  984. int newSize = stackSize - 1;
  985. returnedArray = new int[stackSize][2];
  986.  
  987. int counterFull = 0;
  988.  
  989. if (!(stackSize == 0 || stackSize == 1)) {
  990. Point pointy = new Point(yEnd, xEnd);
  991. // pointy = (Point) stack.pop();
  992. printDebugPoint(pointy);
  993. returnedArray[newSize][0] = pointy.x;
  994. returnedArray[newSize][1] = pointy.y;
  995. counterFull++;
  996. }
  997.  
  998. while (!(stack.isEmpty())) {
  999. Point pointy = new Point();
  1000. // if(counterFull)
  1001. pointy = (Point) stack.pop();
  1002. printDebugPoint(pointy);
  1003. returnedArray[newSize - counterFull][0] = pointy.x;
  1004. returnedArray[newSize - counterFull][1] = pointy.y;
  1005. counterFull++;
  1006. }
  1007.  
  1008. printDebug("output ya yoyo");
  1009. for (int i = 0; i < stackSize; i++) {
  1010.  
  1011. printDebug(Integer.toString(returnedArray[i][0]) + " " + Integer.toString(returnedArray[i][1]));
  1012.  
  1013. }
  1014. }
  1015.  
  1016. }
  1017.  
  1018. } catch (Exception e) {
  1019. System.out.println("momken trmi exp");
  1020. }
  1021.  
  1022. return returnedArray;
  1023. }
  1024.  
  1025. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement