Javaは迷路を歩くアルゴリズムを実現します。


一つでお願いします×Nの長方陣は迷宮を表し、0と1はそれぞれ迷宮中の通路と障害を表します。プログラムを設計して、任意に設定した迷路に対して、入り口から出口までの通路を求めたり、通路がないという結論を出したりします。
(1)二次元配列に従って、迷路の図形を出力します。
(2)迷路の4つの方向を探る:RIGHTは右、DOWNは下、LEFTは左、UPは上に、入り口から出口までの走行経路を出力する。
例:
左上(1,1)は入り口で、右下(8,9)は出口です。

遡る方法を使ってもいいです。つまり入り口から出発して、ある方向に探索して、もし通れば、引き続き前進します。さもなくばもとの道に沿って戻して、1つの方向を変えて引き続き探求して、出口の位置まで、1本の通路を求めます。可能なすべての通路を探索して出口に到達できない場合、設定された迷路には通路がありません。

import java.util.*;

class Position{
 public Position(){

 }

 public Position(int row, int col){
  this.col = col;
  this.row = row;
 }

 public String toString(){
  return "(" + row + " ," + col + ")";
 }

 int row;
 int col;
}

class Maze{
 public Maze(){
  maze = new int[15][15];
  stack = new Stack<Position>();
  p = new boolean[15][15];
 }

 /*
  *     
  */
 public void init(){
  Scanner scanner = new Scanner(System.in);
  System.out.println("        ");
  row = scanner.nextInt();
  System.out.println("        ");
  col = scanner.nextInt();
  System.out.println("   " + row + " " + col + "    ");
  int temp = 0;
  for(int i = 0; i < row; ++i) {
   for(int j = 0; j < col; ++j) {
    temp = scanner.nextInt();
    maze[i][j] = temp;
    p[i][j] = false;
   }
  }
 }

 /*
  *     ,       
  */
 public void findPath(){
  //              
  int temp[][] = new int[row + 2][col + 2];
  for(int i = 0; i < row + 2; ++i) {
   for(int j = 0; j < col + 2; ++j) {
    temp[0][j] = 1;
    temp[row + 1][j] = 1;
    temp[i][0] = temp[i][col + 1] = 1;
   }
  }
  //              
  for(int i = 0; i < row; ++i) {
   for(int j = 0; j < col; ++j) {
    temp[i + 1][j + 1] = maze[i][j];
   }
  }
  //                

  int i = 1;
  int j = 1;
  p[i][j] = true;
  stack.push(new Position(i, j));
  while (!stack.empty() && (!(i == (row) && (j == col)))) {

   if ((temp[i][j + 1] == 0) && (p[i][j + 1] == false)) {
    p[i][j + 1] = true;
    stack.push(new Position(i, j + 1));
    j++;
   } else if ((temp[i + 1][j] == 0) && (p[i + 1][j] == false)) {
    p[i + 1][j] = true;
    stack.push(new Position(i + 1, j));
    i++;
   } else if ((temp[i][j - 1] == 0) && (p[i][j - 1] == false)) {
    p[i][j - 1] = true;
    stack.push(new Position(i, j - 1));
    j--;
   } else if ((temp[i - 1][j] == 0) && (p[i - 1][j] == false)) {
    p[i - 1][j] = true;
    stack.push(new Position(i - 1, j));
    i--;
   } else {
    stack.pop();
    if(stack.empty()){
     break;
    }
    i = stack.peek().row;
    j = stack.peek().col;
   }

  }

  Stack<Position> newPos = new Stack<Position>();
  if (stack.empty()) {
   System.out.println("    ");
  } else {
   System.out.println("   ");
   System.out.println("    :");
   while (!stack.empty()) {
    Position pos = new Position();
    pos = stack.pop();
    newPos.push(pos);
   }
  }

  /*
   *        
   * */

  String resault[][]=new String[row+1][col+1];
  for(int k=0;k<row;++k){
   for(int t=0;t<col;++t){
    resault[k][t]=(maze[k][t])+"";
   }
  }
  while (!newPos.empty()) {
   Position p1=newPos.pop();
   resault[p1.row-1][p1.col-1]="#";

  }

  for(int k=0;k<row;++k){
   for(int t=0;t<col;++t){
    System.out.print(resault[k][t]+"\t");
   }
   System.out.println();
  }


 }

 int maze[][];
 private int row = 9;
 private int col = 8;
 Stack<Position> stack;
 boolean p[][] = null;
}

class hello{
 public static void main(String[] args){
  Maze demo = new Maze();
  demo.init();
  demo.findPath();
 }
}
実行例:
迷路の行数を入力してください。
3
迷路の列数を入力してください。
3
3行3列の迷宮を入力してください。
0 1 1
0 0 0 1
1 0
パスがあります
パスは以下の通りです

迷路の行数を入力してください。
9
迷路の列数を入力してください。
8
9行8列の迷宮を入力してください。
0 0 1 0 0 0 0 1 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0 0 1 0
0 0 1 0 1 1 1 0 1 1 1 0 1
0 1 1 1 1 0 1 0 1 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 1 0 1 0 1 1 1 1 1
0 1 1 1 1 1 0 1
1 0 0 0 1 0 1 0 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0
パスがあります
パスは以下の通りです

以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。