Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package solvemazes;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- /**
- *
- * @author bcaunter5094
- */
- public class SolveMazes {
- /**
- * 7,11
- %e%%%%%%%%%
- %...%.%...%
- %.%.%.%.%%%
- %.%.......%
- %.%%%%.%%.%
- %.%.....%.%
- %%%%%%%%%x%
- */
- static boolean found = false;
- static char[][] solution;
- public static void main(String[] args) throws FileNotFoundException {
- Scanner fileInput = new Scanner(new File("maze1.txt"));
- int dimX = 0, dimY = 0;
- String temp = "";
- if (fileInput.hasNextLine()) {
- temp = fileInput.nextLine();
- dimY = Integer.parseInt(temp.substring(0, temp.indexOf(",")));
- dimX = Integer.parseInt(temp.substring(temp.indexOf(",") + 1, temp.length()));
- }
- char[][] maze = new char[dimX][dimY];
- int ya = 0;
- while (fileInput.hasNextLine()) {
- temp = fileInput.nextLine();
- for (int xa = 0; xa < temp.length(); xa++) {
- maze[xa][ya] = temp.charAt(xa);
- }
- ya++;
- }
- boolean bigbreak = false;
- found = false;
- for (int yy = 0; yy < dimY; yy++) {
- if (bigbreak) {
- break;
- }
- for (int xx = 0; xx < dimX; xx++) {
- if (bigbreak) {
- break;
- }
- System.out.print(maze[xx][yy]);
- if (maze[xx][yy] == 'e') {
- findSolution(xx, yy, maze, dimX, dimY);
- bigbreak = true;
- break;
- }
- }
- }
- // if (found) {
- // for (int yy = 0; yy < dimY; yy++) {
- // for (int xx = 0; xx < dimX; xx++) {
- // System.out.print(solution[xx][yy]);
- // }
- // System.out.println("");
- // }
- // }else{
- // System.out.println("no solution found");
- // }
- }
- public static void findSolution(int x, int y, char[][] maze, int dimX, int dimY) {
- //if (!found) {
- for (int yy = -1; yy <= 1; yy++) {
- for (int xx = -1; xx <= 1; xx++) {
- if (xx == 0 || yy == 0 && (xx != yy)) {//only left right up down
- if (x + xx >= 0 && x + xx < dimX && y + yy >= 0 && y + yy < dimY) { //in range
- if (maze[x + xx][y + yy] != '%' && maze[x + xx][y + yy] != '+') {//valid move
- maze[x][y] = '+';
- //System.out.println("moving dir"+xx+","+yy);
- // for (int yt = 0; yt < dimY; yt++) {
- // for (int xt = 0; xt < dimX; xt++) {
- // System.out.print(maze[xt][yt]);
- // }
- // System.out.println("");
- // }
- if (maze[x + xx][y + yy] == 'x') {
- System.out.println("SOLUTIONnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
- for (int yt = 0; yt < dimY; yt++) {
- for (int xt = 0; xt < dimX; xt++) {
- System.out.print(maze[xt][yt]);
- }
- System.out.println("");
- }
- xx = 1;
- yy = 1;
- found = true;
- } else {
- findSolution(x + xx, y + yy, maze, dimX, dimY);
- }
- }
- }
- }
- }
- }
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement