Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Maze Solver
- */
- class Analizza {
- private int xl, yl;
- private char[][] maze = { {'*','*','*','*','*','S','*','*','*'},
- {'*',' ',' ',' ',' ',' ','*',' ','E'},
- {'*','*','*',' ','*','*','*',' ','*'},
- {'*',' ',' ',' ','*',' ',' ',' ','*'},
- {'*','*',' ','*','*','*',' ','*','*'},
- {'*','*',' ','*',' ','*',' ',' ','*'},
- {'*','*',' ','*','*','*','*',' ','*'},
- {'*','*',' ',' ',' ',' ',' ',' ','*'},
- {'*','*','*','*','*','*','*','*','*'}
- };
- Analizza() {
- xl = maze.length-1;
- yl = maze[0].length-1;
- }
- boolean analizzaStruttura(int x, int y) {
- if(maze[x][y] == 'E') return true;
- if(maze[x][y] == '*') return false;
- if(maze[x][y] == '+') return false;
- maze[x][y] = '+';
- if(analizzaStruttura(x, y-1)) return true;
- if(analizzaStruttura(x+1,y)) return true;
- if(analizzaStruttura(x, y+1)) return true;
- if(analizzaStruttura(x-1, y)) return true;
- maze[x][y] = ' ';
- return false;
- }
- void stampa() {
- maze[0][5] = 'S';
- for(int i=0; i<maze.length; i++) {
- for(int j=0; j<maze[0].length; j++) {
- System.out.print(maze[i][j]+" ");
- }
- System.out.println();
- }
- }
- }
- class TestMaze {
- public static void main(String[] args) {
- Analizza a = new Analizza();
- System.out.println("\n\tLabirinto prima della ricerca della soluzione:\n");
- a.stampa();
- if(a.analizzaStruttura(0,5)) {
- System.out.println("\n\n\tSoluzione:\n\n");
- a.stampa();
- }
- else {
- System.out.println("\nLa soluzione non e' presente");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement