本文目錄一覽:
java迷宮路徑總條數問題
int[][] data是你的迷宮數組,返回值是路徑總條數,不需要遞歸
public int findWayCount(int[][] data) {
int[][] way = new int[data.length][];
for (int m = 0; m data.length; m++) {
way[m] = new int[data[m].length];
for (int n = 0; n data[m].length; n++) {
if (data[m][n] == 0) {
way[m][n] = 0;
} else if (m == 0 n == 0) {
way[m][n] = data[0][0];
} else if (m == 0) {
way[m][n] = way[m][n – 1];
} else if (n == 0) {
way[m][n] = way[m – 1][n];
} else {
way[m][n] = way[m][n – 1] + way[m – 1][n];
}
}
}
JAVA迷宮問題,怎麼輸出所走路徑的坐標
在Way函數的if(Maza[Loci][Locj]==0) 語句裡面Maza[Loci][Locj]=2;前面加一句
System.out.println(“The Maza route is (“+Loci+”,”+Locj+”)”);就打印出程序所走路徑的坐標了.
java自動走迷宮線程問題
以一個M×N的長方陣表示迷宮,0和1分別表示迷宮中的通路和障礙。設計一個程序,對任意設定的迷宮,求出一條從入口到出口的通路,或得出沒有通路的結論。
(1) 根據二維數組,輸出迷宮的圖形。
(2) 探索迷宮的四個方向:RIGHT為向右,DOWN向下,LEFT向左,UP向上,輸出從入口到出口的行走路徑。
求Java關於迷宮的算法(用棧實現)
package com.Albert.LabyringhStack;
public class Point {
int x;
int y;
int direction; //direction指向此點附近的一個點 應該有四個 編號為1 2 3 4
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void addDirection(){
this.direction++;
}
public Point() {
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
this.direction = 1;
}
public Point(int x, int y, int direction) {
super();
this.x = x;
this.y = y;
this.direction = direction;
}
}
package com.Albert.LabyringhStack;
import java.util.*;
public class LabyringhStack {
public Point S;
public Point F;
char[][] mazemap;
StackPoint path;
public LabyringhStack() {
}
public LabyringhStack(char[][] ma) { //初始化 存入數組
this.mazemap = new char[ma.length][ma[0].length];
for(int i=0;ima.length;i++){
for(int j=0;jma[0].length;j++){ //mazemap[0]必須有元素 不可為空
this.mazemap[i][j] = ma[i][j];
}
}
S = returnPlace(‘S’);
F = returnPlace(‘F’);
}
public Point returnPlace(char s){ //返回數組中字符的位置
Point point = new Point();
for(int i=0;ithis.mazemap.length;i++){
for(int j=0;jthis.mazemap[0].length;j++){ //mazemap[0]必須有元素 不可為空
if(this.mazemap[i][j]==s)
{ point.setX(i);
point.setY(j);
point.setDirection(1);
}
}
}
return point;
}
public char returnChar(Point point){
if(point.getX()=0point.getY()=0)
return this.mazemap[point.getX()][point.getY()];
else
return ‘#’;
}
public void replacePlace(Point point, char s){ //更改特定位置處的字符
mazemap[point.getX()][point.getY()] = s;
}
public void printPath(){
StackPoint tempPath = new StackPoint();
while(!path.empty()){ //對棧進行反序
tempPath.push(path.pop());
}
while(!tempPath.empty()){
System.out.print(“(“+tempPath.peek().getX()+”,”+tempPath.pop().getY()+”)”);
}
}
public boolean getPath(){ //取得路徑的算法 如果有路徑就返回真
path = new StackPoint();
S.setDirection(1);
path.push(S);
replacePlace(S, ‘X’);
while(!path.empty()){
Point nowPoint = path.peek(); //取得當前位置
if(nowPoint.getX()==F.getX()nowPoint.getY()==F.getY()){
//printPath();
return true;
}
Point temp = new Point(); //存放下一個可走的位置
int find = 0; //標誌 是否可向下走
while(nowPoint.getDirection()5find==0){
switch(nowPoint.getDirection()){
case 1:temp = new Point(nowPoint.getX(),nowPoint.getY()-1,1); break; //取得當前位置左邊的位置
case 2:temp = new Point(nowPoint.getX()+1,nowPoint.getY(),1); break;//取得當前位置下邊的位置
case 3:temp = new Point(nowPoint.getX(),nowPoint.getY()+1,1); break;//取得當前位置右邊的位置
case 4:temp = new Point(nowPoint.getX()-1,nowPoint.getY(),1); break;//取得當前位置上邊的位置
}
nowPoint.addDirection(); //指向下一個需要驗證的點
if(returnChar(temp)==’O’||returnChar(temp)==’F’) find = 1; //如果能向下走則置為1
}
if(find==1){ //如果可走就進棧
replacePlace(temp, ‘X’); //設置成X 防止回走
// printArr();
path.push(temp);
}else{ //如果不可走就退棧
replacePlace(nowPoint, ‘O’);
path.pop();
}
}
return false;
}
public void printArr(){
for(int i=0;imazemap.length;i++){
for(int j=0;jmazemap[0].length;j++){ //mazemap[0]必須有元素 不可為空
System.out.print(mazemap[i][j]);
}
System.out.println();
}
System.out.println();
}
}
package com.Albert.LabyringhStack;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] mazemap = {
{‘M’,’M’,’M’,’M’,’M’,’M’,’M’,’M’},
{‘M’,’S’,’O’,’O’,’M’,’M’,’M’,’M’},
{‘M’,’M’,’M’,’O’,’M’,’M’,’M’,’M’},
{‘M’,’M’,’O’,’O’,’O’,’O’,’M’,’M’},
{‘M’,’M’,’M’,’M’,’M’,’F’,’M’,’M’},
{‘M’,’M’,’M’,’M’,’M’,’M’,’M’,’M’},
{‘M’,’M’,’M’,’M’,’M’,’M’,’M’,’M’}
};
LabyringhStack solution = new LabyringhStack(mazemap);
if(solution.getPath()){
System.out.print(“迷宮路徑如下:”);
solution.printPath();
}
else {
System.out.println(“沒有可走的路”);
}
}
}
求用java語言尋找走出迷宮路線的算法
首先給定一個初始坐標,然後構建一個容器保存坐標值,之後進行迭代,橫坐標+1,或者縱坐標+1,這個順尋自己定義(四個方向),經過的“路徑”保存在那個容器中,如果遇到死角,以此往回迭代,在容器中將遇到死角的那個坐標刪除。最後找到自己定義的那個迷宮出口坐標。
原創文章,作者:FAOW,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/137459.html