迷宮演算法,右手法則

來源:互聯網
上載者:User

package homeWork;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;

/*1。在你的節點上,若右手不為牆壁,就原地右轉在直走,爾後重複偵測右手情況。
  2。若右手為牆壁,就原地左轉,重複偵測右手情況。

但由於第2步的做法,會造成前進時變成先左轉,右轉再直走,所以有以下變化:
2。1。若右手為牆壁,前面不為牆壁,則直走後偵測右手情況。
2。2。若右手為牆壁,前面也為牆壁,就原地左轉兩次,再重複偵測右手情況。

如此重複摸牆壁,就可走出迷宮了。*/

/*東南西北四個方向,用一個變數標記,向東走時向右轉即是向南轉,西--北,北--東,南--西
 * 左轉 東--北,西--南,北--西,南--東*/
public class Maze extends JApplet implements ActionListener{//走迷宮,右手試探法
 private static final long serialVersionUID=1L;

 // JTextArea outputArea;
 JButton actionButton;
 // String outputString="";
 char maze[][]={{'#','#','#','#','#','#','#','#','#','#','#','#',},
   {'#','.','.','.','#','.','.','.','.','.','.','#',},
   {'.','.','#','.','#','.','#','#','#','#','.','#',},
   {'#','#','#','.','#','.','.','.','.','#','.','#',},
   {'#','.','.','.','.','#','#','#','.','#','.','.',},
   {'#','#','#','#','.','#','.','#','.','#','.','#',},
   {'#','.','.','#','.','#','.','#','.','#','.','#',},
   {'#','#','.','#','.','#','.','#','.','#','.','#',},
   {'#','.','.','.','.','.','.','.','.','#','.','#',},
   {'#','#','#','#','#','#','.','#','#','#','.','#',},
   {'#','.','.','.','.','.','.','#','.','.','.','#',},
   {'#','#','#','#','#','#','#','#','#','#','#','#',}};
 int direction=1;//表示方向,1--東,2--南,3--西,4--北,預設東
 int col,row;//當前行和列

 public void init() {
  Container container=getContentPane();
  container.setLayout(new FlowLayout());

  //  outputArea=new JTextArea();
  //  container.add(outputArea);

  row=2;
  col=0;

  actionButton=new JButton("GO!");
  actionButton.addActionListener(this);
  container.add(actionButton);
 }

 public void actionPerformed(ActionEvent event) {
  mazeTraverse(maze,row,col);
  repaint();
 }

 public void mazeTraverse(char maze[][],int startRow,int startCol) {
  if(row!=4 || col!=12) {
   if(righthand(direction,row,col)) {//右邊為牆壁
    if(beforeHand(direction,row,col)) {//前面為牆壁
     TurnLeft(direction);//左轉兩次
     TurnLeft(direction);
     mazeTraverse(maze, row, col);
    }
    else{//前面不是牆壁
     forthRight(direction);
     mazeTraverse(maze, row, col);
    }
   }
   else{//右邊不是牆壁
    TurnRight(direction);
    forthRight(direction);
    mazeTraverse(maze, row, col);
   }
  }
 }

 public void East() {
  direction=1;
  col++;//行不變,列加一
 }

 public void South() {
  direction=2;
  row++;//列不變,行加一
 }

 public void West() {
  direction=3;
  col--;
 }

 public void North() {
  direction=4;
  row--;
 }

 public boolean righthand(int d,int r,int c) {//判斷右手的情況,是否為牆壁,是則返回真
  switch (d) {
  case 1:
   if(maze[++r][c]=='#')
    return true;
   break;
  case 2:
   if(maze[r][--c]=='#')
    return true;
   break;
  case 3:
   if(maze[--r][c]=='#')
    return true;
   break;
  case 4:
   if(maze[r][++c]=='#')
    return true;
   break;
  }

  return false;
 }

 public boolean beforeHand(int d,int r,int c) {//若前面為牆壁,則返回真
  switch (d) {
  case 1:
   if(maze[r][++c]=='#')
    return true;
   break;
  case 2:
   if(maze[++r][c]=='#')
    return true;
   break;
  case 3:
   if(maze[r][--c]=='#')
    return true;
   break;
  case 4:
   if(maze[--r][c]=='#')
    return true;
   break;
  }

  return false;
 }

 public void forthRight(int d) {
  switch (d) {
  case 1:
   col++;
   maze[row][col]='*';
   break;
  case 2:
   row++;
   maze[row][col]='*';
   break;
  case 3:
   col--;
   maze[row][col]='*';
   break;
  case 4:
   row--;
   maze[row][col]='*';
   break;
  default:
   break;
  }
 }

 public void TurnRight(int d) {
  switch(d) {
  case 1:
   direction=2;
   break;
  case 2:
   direction=3;
   break;
  case 3:
   direction=4;
   break;
  case 4:
   direction=1;
   break;
  default:
   break;
  }
 }

 public void TurnLeft(int d) {
  switch (d) {
  case 1:
   direction=4;
   break;
  case 2:
   direction=1;
   break;
  case 3:
   direction=2;
   break;
  case 4:
   direction=3;
   break;
  default:
   break;
  }
 }

 public void paint(Graphics g) {
  //  DecimalFormat twoDights = new DecimalFormat("");
  super.paint(g);
  int x=5,y=30;
  for (int r = 0; r < maze.length; r++){
   for(int c=0;c<maze[r].length;c++)
    g.drawString(""+maze[r][c],x+=20,y);
   y+=15;
   x=5;
   //    outputString+=twoDights.format(array[r][c])+" ";
   //   outputString+="/n";
  }
  //  outputArea.setText(outputString);
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.