迷宫递归撞墙

时间:2016-07-06 04:28:26

标签: recursion

嘿伙计们我在我的迷宫求解器上工作它必须使用递归解决的迷宫,我的问题是它一直向东走,它到达了一个点,N,E,S,W不是正确的方法,因为一堵墙是在右边,一个星(路径访问)在左边,有人可以帮助我我的代码,它到达墙不知道该怎么做继承我的代码

/*
 * 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 Assign4;
import java.util.Scanner;
import java.io.File;
/*COSC 1P03
 *ASSIGNMENT #4
 *Username: Rahul Chawla
 *Student #:  5785407
 *
 *This class coordinates the loading and parsing of a 'maze' datafile, the creation of
 *a Maze object (which you'll be writing), and the invocation of the Maze's solve()
 *method (which you'll obviously also be writing).
 *It also handles basic IO and tells the Maze object to output the maze status and
 *solution (again, tasks you'll be implementing yourself).
 *
 *Three sample mazes are included.
 *maze1.txt solves easily
 *maze2.txt is larger, but also solves
 *maze3.txt is unsolveable
 */

public class MazeSolver {
    char[][] charmap=null;
    int startRow=-1;//Starting row
    int startCol=-1;//Starting column
    //You can add extra stuff here if you like!

    public MazeSolver() {

    }



    private void loadMaze() {
        int height,width;
        String filename;
        Scanner console=new Scanner(System.in);
        Scanner file;
        String temp;

        System.out.print("Enter maze filename: ");
        filename=console.nextLine();
        try {
            file=new Scanner(new File(filename));

            height=file.nextInt();
            width=file.nextInt();
            charmap=new char[height][width];
            file.nextLine();
            for (int i=0;i<height;i++) {
                temp=file.nextLine();
                charmap[i]=temp.toCharArray();
                if (temp.indexOf('S')>=0) {
                    startRow=i;startCol=temp.indexOf('S');
                    System.out.println("Start at "+startRow+","+startCol+".");
                }
            }

            System.out.println("File transcription complete!\n");
        }
        catch (Exception exn) {
            System.out.println("\nFile transcription problem. Exiting now.");
            System.exit(0);
        }

        solve();
    }

    private void solve() {
        boolean solved=false;
        System.out.println("Initial State:");
        printMap();

        if (recursiveSolve(startRow,startCol)) {
            System.out.println("\nFinal Maze:");
            printMap();
            System.out.print("Solution Path: ");
            //Display solution path here
        }
                else System.out.println("Oops! No solution found!");
               printMap();
    }

    //Put your recursive solution here.
    //Feel free to add a parameter if you'd like it to keep track of your solution path
    private boolean recursiveSolve(int Row, int Col) {
           //base case - did I reach finish or not
            if(charmap[Row][Col]=='E'){
            charmap[Row][Col]='O';
            return true;
        }

            else{
                if(charmap[Row][Col]=='S'){

            charmap[Row][Col]='#';
           }
            }



           if(charmap[Row-1][Col]==' '){  // north   
                System.out.println("north");
               charmap[Row][Col]='*';
                recursiveSolve(Row-1,Col);
            }

           else if(charmap[Row][Col+1]==' '){
               /// east
               System.out.println("east");
              charmap[Row][Col]='*';
              recursiveSolve(Row,Col+1);
           }

           else 
                   if(charmap[Row+1][Col]==' '){    ///south
                       System.out.println("south");
               charmap[Row][Col]='*';
               recursiveSolve(Row+1,Col);
           }
           else 
                       if(charmap[Row][Col-1]==' '){  //west
                           System.out.println("west");
               charmap[Row][Col]='*';
               recursiveSolve(Row,Col-1);
           }





        return false;//this is just here as a placeholder so it can compile
    }

    private void printMap() {
        for (char[] row:charmap) {
            for (char c:row) System.out.print(c);
            System.out.println();
        }
    }

    public static void main(String args[]) {new MazeSolver().loadMaze();}
}
[/code]
  

,输出为Enter maze filename:maze1.txt从1,1开始。文件   转录完成!

     

初始状态:XXXXXXXXXXX XS X XXXXX XXXXX X X X X XXX
  XXX X XXX XXX X X X X X X X X X X X X X X X X X X XX   XXE X XXXXXXXXXXX东东东,东东东东哎呀!没有   找到解决方案XXXXXXXXXXX X ******** X XXXXX XXXXX X X X X XXX
  XXX X XXX XXX X X X X X X X X X X X X X X X X X X XX   XXE X XXXXXXXXXXX

我尝试修复recursiveSolve方法,并添加了算法,如果它遇到左边的墙和星星和周围的墙壁,基本上它必须倒退并取消星星但它不起作用请帮助。 / p>

private boolean recursiveSolve(int Row, int Col) {
           //base case - did I reach finish or not?
            if(charmap[Row][Col]=='E'){
            charmap[Row][Col]='O';
            return true;
        }

            else{
                if(charmap[Row][Col]=='S'){

            charmap[Row][Col]='#';
           }
            }



           if(charmap[Row-1][Col]==' '){  // north   
                System.out.println("north");
               charmap[Row][Col]='*';
                recursiveSolve(Row-1,Col);
            }

           else if(charmap[Row][Col+1]==' '){
               /// east

              charmap[Row][Col]='*';
              System.out.println("east");
              recursiveSolve(Row,Col+1);
           }

           else 
                   if(charmap[Row+1][Col]==' '){    ///south
                       System.out.println("south");
               charmap[Row][Col]='*';
               recursiveSolve(Row+1,Col);
           }
           else 
                       if(charmap[Row][Col-1]==' '){  //west
                           System.out.println("west");
               charmap[Row][Col]='*';
               recursiveSolve(Row,Col-1);
           }
        else   
            if(charmap[Row-1][Col]!=' '){
              if(charmap[Row][Col+1]!=' '){
                  if(charmap[Row+1][Col]!=' '){
                      if(charmap[Row][Col-1]!=' '){
                         if(charmap[Row-1][Col]=='*'){

                             recursiveSolve(Row-1,Col);
                         }
                         if(charmap[Row][Col+1]=='*'){
                             recursiveSolve(Row,Col+1);
                         }
                         if (charmap[Row+1][Col]=='*'){
                             recursiveSolve(Row+1,Col);
                         }
                         if (charmap[Row][Col-1]=='*'){
                             System.out.println("bitch");
                             recursiveSolve(Row,Col-1);
                                        System.out.println("bitch");
                         }                         

                      }
                  }
              }
          }






        return false;//this is just here as a placeholder so it can compile
    }

0 个答案:

没有答案