導航:首頁 > 編程語言 > 迷宮演算法java

迷宮演算法java

發布時間:2022-09-05 17:05:42

A. 急求大佬幫忙寫一下java程序

遞歸的話就是深度優先搜索(可以理解成不撞南牆不回頭,撞了牆就原路返回)可以加上剪枝(就是做標記,如果之前某一次走過但不通的路下次再走到就不用走了)
用棧的話應該是廣度優先搜索(大概就是分裂無數個你,每次向所有方向走一步),不過廣搜要用隊列實現,用棧本質還是深搜了
具體演算法可以搜網路

B. 用java語言控制小機器人走迷宮的演算法

我昨天剛寫了個走迷宮的界面(一個初始小球,一個目標小球,隨機在界面種生成障礙(迷宮圖),然後初始小球移動到目標小球那),不知道是否跟你的想法一樣。用的是回溯法(目前我只知道這個演算法走迷宮),你可以查下。PS:我電腦沒聯網不能把代碼給你…QQ254774042。

C. Java迷宮演算法問題(用棧實現)有演算法簡述

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.util.HashSet;
importjava.util.LinkedList;
importjava.util.List;
importjava.util.Scanner;

publicclassTest{

publicstaticvoidmain(String[]args)throwsFileNotFoundException{
Scannerinput=newScanner(newFile("Maze2tong.txt"));
introw=0;
char[][]Mazemap=newchar[12][58];
while(input.hasNext()){
Stringline=input.nextLine();
for(intcolumn=0;column<=line.length()-1;column++){
charc=line.charAt(column);
Mazemap[row][column]=c;
}
row++;
}
for(inti=0;i<12;i++){
for(intj=0;j<58;j++){
System.out.print(Mazemap[i][j]);
}
System.out.print(" ");
}
LinkedList<TwoTuple<Integer,Integer>>trace=newLinkedList<TwoTuple<Integer,Integer>>();
System.out.println(maze(Mazemap,trace));
System.out.println(trace);
}

publicstaticbooleanmaze(char[][]maze,
List<TwoTuple<Integer,Integer>>trace){
LinkedList<TwoTuple<Integer,Integer>>path=newLinkedList<TwoTuple<Integer,Integer>>();
HashSet<TwoTuple<Integer,Integer>>traverse=newHashSet<TwoTuple<Integer,Integer>>();

for(inti=0;i<maze.length;i++){
for(intj=0;j<maze[i].length;j++){
if(maze[i][j]=='S'){
path.add(newTwoTuple<Integer,Integer>(i,j));
}
}
}
while(!path.isEmpty()){
TwoTuple<Integer,Integer>temp=path.pop();

if(traverse.contains(temp)){
continue;
}elseif(maze[temp.first][temp.second]=='F'){
trace.add(temp);
returntrue;
}elseif(!traverse.contains(temp)){
if(temp.second+1<maze[temp.first].length
&&maze[temp.first][temp.second+1]!='W')
path.add(newTwoTuple<Integer,Integer>(temp.first,
temp.second+1));
if(temp.second-1>0
&&maze[temp.first][temp.second-1]!='W')
path.add(newTwoTuple<Integer,Integer>(temp.first,
temp.second-1));
if(temp.first+1<maze.length
&&maze[temp.first+1][temp.second]!='W')
path.add(newTwoTuple<Integer,Integer>(temp.first+1,
temp.second));
if(temp.first-1>0
&&maze[temp.first-1][temp.second]!='W')
path.add(newTwoTuple<Integer,Integer>(temp.first-1,
temp.second));
traverse.add(temp);
trace.add(temp);
}
}
trace.clear();
returnfalse;
}
}

classTwoTuple<A,B>{
publicfinalAfirst;
publicfinalBsecond;

publicTwoTuple(Aa,Bb){
first=a;
second=b;
}

@Override
publicinthashCode(){
returnfirst.hashCode()+second.hashCode();
}

@Override
publicbooleanequals(Objectobj){
if(!(objinstanceofTwoTuple)){

}
returnobjinstanceofTwoTuple&&first.equals(((TwoTuple)obj).first)
&&second.equals(((TwoTuple)obj).second);
}

publicStringtoString(){
return"("+first+","+second+")";
}
}///:-




importjava.io.File;
importjava.io.FileNotFoundException;
importjava.util.LinkedList;
importjava.util.Scanner;
classMyPoint
{
publicbooleanvisited=false;
publicintparentRow=-1;
publicintparentColumn=-1;
publicfinalcharcontent;
publicintx;
publicinty;
publicMyPoint(charc,intx,inty)
{
this.content=c;
this.x=x;
this.y=y;
}
}
publicclassMaze
{

publicstaticMyPoint[][]getMazeArray(){
Scannerinput=null;
MyPoint[][]mazemap=newMyPoint[12][58];
try{
input=newScanner(newFile("Maze2tong.txt"));
introw=0;
while(input.hasNext()){
Stringline=input.nextLine();
for(intcolumn=0;column<=line.length()-1;column++){
charc=line.charAt(column);
MyPointpoint=newMyPoint(c,row,column);
mazemap[row][column]=point;
}
row++;
}
input.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
returnmazemap;
}
publicstaticbooleantomRun(MyPoint[][]maze,MyPointend)
{
intx=maze.length;
inty=maze[0].length;
LinkedList<MyPoint>stack=newLinkedList<MyPoint>();
for(inti=0;i<maze.length;i++){
for(intj=0;j<maze[i].length;j++){
if(maze[i][j].content=='S'){
stack.push(maze[i][j]);
maze[i][j].visited=true;
}
}
}
booleanresult=false;
while(!stack.isEmpty())
{
MyPointt=stack.pop();
//System.out.println("poppoint:"+t.x+""+t.y+"value:"+maze[t.x][t.y]);
if(t.content=='F')
{
result=true;
end.x=t.x;
end.y=t.y;
break;
}
if(t.x-1>0&&maze[t.x-1][t.y].visited==false&&maze[t.x-1][t.y].content!='W')
{
stack.push(maze[t.x-1][t.y]);
maze[t.x-1][t.y].parentRow=t.x;
maze[t.x-1][t.y].parentColumn=t.y;
maze[t.x-1][t.y].visited=true;
}
if(t.x+1<x&&maze[t.x+1][t.y].visited==false&&maze[t.x+1][t.y].content!='W')
{
stack.push(maze[t.x+1][t.y]);
maze[t.x+1][t.y].parentRow=t.x;
maze[t.x+1][t.y].parentColumn=t.y;
maze[t.x+1][t.y].visited=true;
}
if(t.y-1>0&&maze[t.x][t.y-1].visited==false&&maze[t.x][t.y-1].content!='W')
{
stack.push(maze[t.x][t.y-1]);
maze[t.x][t.y-1].parentRow=t.x;
maze[t.x][t.y-1].parentColumn=t.y;
maze[t.x][t.y-1].visited=true;
}
if(t.y+1<y&&maze[t.x][t.y+1].visited==false&&maze[t.x][t.y+1].content!='W')
{
stack.push(maze[t.x][t.y+1]);
maze[t.x][t.y+1].parentRow=t.x;
maze[t.x][t.y+1].parentColumn=t.y;
maze[t.x][t.y+1].visited=true;
}

}
returnresult;
}
publicstaticvoidshow(intx,inty,MyPoint[][]visited)
{
if(visited[x][y].parentRow==-1)
{
System.out.println("["+x+","+y+"]");
return;
}
show(visited[x][y].parentRow,visited[x][y].parentColumn,visited);
System.out.println("->"+"["+x+","+y+"]");
}
publicstaticvoidmain(String[]args)
{
MyPoint[][]maze=getMazeArray();
MyPointpoint=newMyPoint('c',1,1);
if(tomRun(maze,point))
{
System.out.println("逃生路徑如下:");
show(point.x,point.y,maze);
}
else
System.out.println("無法走出迷宮!");
}
}







D. 求用java語言尋找走出迷宮路線的演算法

首先給定一個初始坐標,然後構建一個容器保存坐標值,之後進行迭代,橫坐標+1,或者縱坐標+1,這個順尋自己定義(四個方向),經過的「路徑」保存在那個容器中,如果遇到死角,以此往回迭代,在容器中將遇到死角的那個坐標刪除。最後找到自己定義的那個迷宮出口坐標。

E. java迷宮程序代碼

qq你了

F. 請幫忙用數據結構(java版)的知識解決這道迷宮問題的程序代碼。

我這是用c寫的。你可以看看,希望能幫助到你。
#include"stdlib.h"
#include"stdio.h"
#define N 50
#define M 50
int X;
int maze[N+2][M+2];
struct point{
int row,col,predecessor;
}queue[512];
int head=0,tail=0;
void shoudong_maze(int m,int n){
int i,j;
printf("\n\n");
printf("請按行輸入迷宮,0表示通路,1表示障礙:\n\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&maze[i][j]);
}
void zidong_maze(int m,int n){
int i,j;
printf("\n迷宮生成中……\n\n");
system("pause");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
maze[i][j]=rand()%2;
//由於rand()產生的隨機數是從0到RAND_MAX
//RAND_MAX是定義在stdlib.h中的,其值至少為32767)
//要產生從X到Y的數,只需要這樣寫:k=rand()%(Y-X+1)+X;
}
void print_maze(int m,int n){
int i,j;
printf("\n迷宮生成結果如下:\n\n");
printf("迷宮入口\n");
printf("↓");
for(i=0;i<m;i++)
{printf("\n");
for(j=0;j<n;j++)
{if(maze[i][j]==0) printf("□");
if(maze[i][j]==1) printf("■");}
}
printf("→迷宮出口\n");
}
void result_maze(int m,int n)
{ int i,j;
printf("迷宮通路(用☆表示)如下所示:\n\t");
for(i=0;i<m;i++)
{ printf("\n");
for(j=0;j<n;j++)
{if(maze[i][j]==0||maze[i][j]==2) printf("□");
if(maze[i][j]==1) printf("■");
if(maze[i][j]==3) printf("☆");
}
}
}
void enqueue(struct point p)
{ queue[tail]=p;
tail++;
}
struct point dequeue()
{ head++;
return queue[head-1];
}
int is_empty()
{ return head==tail;
}
void visit(int row,int col,int maze[52][52])
{ struct point visit_point={row,col,head-1};
maze[row][col]=2;
enqueue(visit_point);
}
int mgpath(int maze[52][52],int m,int n)
{ X=1;
struct point p={0,0,-1};
if(maze[p.row][p.col]==1)
{ printf("\n===============================================\n");
printf("此迷宮無解\n\n");X=0;return 0;}
maze[p.row][p.col]=2;
enqueue(p);
while(!is_empty())
{p=dequeue();
if((p.row==m-1)&&(p.col==n-1)) break;
if((p.col+1<n)&&(maze[p.row][p.col+1]==0)) visit(p.row,p.col+1,maze);
if((p.row+1<m)&&(maze[p.row+1][p.col]==0)) visit(p.row+1,p.col,maze);
if((p.col-1>=0)&&(maze[p.row][p.col-1]==0)) visit(p.row,p.col-1,maze);
if((p.row-1>=0)&&(maze[p.row-1][p.col]==0)) visit(p.row-1,p.col,maze);
}
if(p.row==m-1&&p.col==n-1)
{printf("\n==================================================================\n");
printf("迷宮路徑為:\n");
printf("(%d,%d)\n",p.row,p.col);
maze[p.row][p.col]=3;
while(p.predecessor!=-1)
{p=queue[p.predecessor];
printf("(%d,%d)\n",p.row,p.col);
maze[p.row][p.col]=3;
}
}
else {printf("\n=============================================================\n");
printf("此迷宮無解!\n\n");X=0;}
return 0;
}
int main()
{int i,m,n,cycle=0;
while(cycle!=(-1))
{
printf("********************************************************************************\n");
printf(" ☆歡迎進入迷宮求解系統☆\n");
printf(" 設計者:尹旭 林靜波(信息2班)\n");
printf("********************************************************************************\n");
printf(" 手動生成迷宮 請按:1\n");
printf(" 自動生成迷宮 請按:2\n");
printf(" 退出 請按:3\n\n");
printf("********************************************************************************\n");
printf("\n");
printf("請選擇你的操作:\n");
scanf("%d",&i);
switch(i)
{case 1:printf("\n請輸入行數:");
scanf("%d",&m);
printf("\n");
printf("請輸入列數:");
scanf("%d",&n);
while((m<=0||m>50)||(n<=0||n>50))
{ printf("\n抱歉,你輸入的行列數超出預設范圍(0-50,0-50),請重新輸入:\n\n");
printf("請輸入行數:");
scanf("%d",&m);
printf("\n");
printf("請輸入列數:");
scanf("%d",&n);
}
shoudong_maze(m,n);
print_maze(m,n);
mgpath(maze,m,n);
if(X!=0)
result_maze(m,n);
printf("\n\nPress Enter Contiue!\n");
getchar();
while(getchar()!='\n');
break;
case 2:printf("\n請輸入行數:");
scanf("%d",&m);
printf("\n");
printf("請輸入列數:");
scanf("%d",&n);
while((m<=0||m>50)||(n<=0||n>50))
{printf("\n抱歉,你輸入的行列數超出預設范圍(0-50,0-50),請重新輸入:\n\n");
printf("請輸入行數:");
scanf("%d",&m);
printf("\n");
printf("請輸入列數:");
scanf("%d",&n);
}
zidong_maze(m,n);
print_maze(m,n);
mgpath(maze,m,n);
if(X!=0)
result_maze(m,n);
printf("\n\nPress Enter Contiue!\n");getchar();while(getchar()!='\n');break;
case 3:cycle=(-1);
break;
default:printf("\n");
printf("你的輸入有誤!\n");
printf("\nPress Enter Contiue!\n");
getchar();
while(getchar()!='\n');break;
}
}
}

G. 求助,關於用java實現A*演算法的迷宮尋路

首先,你要知道走迷宮的思路:就是遇到岔路都往一個方向,比如往右,遇到死路就回頭,回頭遇到岔路繼續往右。

H. 求走迷宮問題的演算法,要求用Java寫的

public class Maze { private int[][] maze = null;
private int[] xx = { 1, 0, -1, 0 };
private int[] yy = { 0, 1, 0, -1 };
private Queue queue = null; public Maze(int[][] maze) {
this.maze = maze;
queue = new Queue(maze.length * maze.length);
} public void go() {
Point outPt = new Point(maze.length - 1, maze[0].length - 1);
Point curPt = new Point(0, 0);
Node curNode = new Node(curPt, null);
maze[curPt.x][curPt.y] = 2;
queue.entryQ(curNode); while (!queue.isEmpty()) {
curNode = queue.outQ();
for (int i = 0; i < xx.length; ++i) {
Point nextPt = new Point();
nextPt.x = (curNode.point).x + xx[i];
nextPt.y = (curNode.point).y + yy[i];
if (check(nextPt)) {
Node nextNode = new Node(nextPt, curNode);
queue.entryQ(nextNode);
maze[nextPt.x][nextPt.y] = 2;
if (nextPt.equals(outPt)) {
java.util.Stack<Node> stack = new java.util.Stack<Node>();
stack.push(nextNode);
while ((curNode = nextNode.previous) != null) {
nextNode = curNode;
stack.push(curNode);
}
System.out.println("A Path is:");
while (!stack.isEmpty()) {
curNode = stack.pop();
System.out.println(curNode.point);
}
return;
}
}
}
}
System.out.println("Non solution!");
} private boolean check(Point p) {
if (p.x < 0 || p.x >= maze.length || p.y < 0 || p.y >= maze[0].length) {
return false;
}
if (maze[p.x][p.y] != 0) {
return false;
}
return true;
} public static void main(String[] args) {
int[][] maze = {
{ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 0, 0, 0, 1, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 1 },
{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 0, 1, 0, 0 }
};
new Maze(maze).go();
} private class Queue { Node[] array = null;
int size = 0;
int len = 0;
int head = 0;
int tail = 0; public Queue(int n) {
array = new Node[n + 1];
size = n + 1;
} public boolean entryQ(Node node) {
if (isFull()) {
return false;
}
tail = (tail + 1) % size;
array[tail] = node;
len++;
return true;
} public Node outQ() {
if (isEmpty()) {
return null;
}
head = (head + 1) % size;
len--;
return array[head];
} public boolean isEmpty() {
return (len == 0 || head == tail) ? true : false;
} public boolean isFull() {
return ((tail + 1) % size == head) ? true : false;
}
} private class Node { Point point = null;
Node previous = null; public Node() {
this(null,null);
} public Node(Point point, Node node) {
this.point = point;
this.previous = node;
}
} private class Point { int x = 0;
int y = 0; public Point() {
this(0, 0);
} public Point(int x, int y) {
this.x = x;
this.y = y;
} public boolean equals(Point p) {
return (x == p.x) && (y == p.y);
} @Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
}

I. 求Java關於迷宮的演算法(用棧實現)

packagecom.Albert.LabyringhStack;

publicclassPoint{
intx;
inty;
intdirection;//direction指向此點附近的一個點應該有四個編號為1234
publicintgetX(){
returnx;
}
publicvoidsetX(intx){
this.x=x;
}
publicintgetY(){
returny;
}
publicvoidsetY(inty){
this.y=y;
}

publicintgetDirection(){
returndirection;
}
publicvoidsetDirection(intdirection){
this.direction=direction;
}
publicvoidaddDirection(){
this.direction++;

}
publicPoint(){
}
publicPoint(intx,inty){
super();
this.x=x;
this.y=y;
this.direction=1;
}
publicPoint(intx,inty,intdirection){
super();
this.x=x;
this.y=y;
this.direction=direction;
}

}
packagecom.Albert.LabyringhStack;

importjava.util.*;

publicclassLabyringhStack{

publicPointS;
publicPointF;
char[][]mazemap;
Stack<Point>path;

publicLabyringhStack(){
}
publicLabyringhStack(char[][]ma){//初始化存入數組
this.mazemap=newchar[ma.length][ma[0].length];
for(inti=0;i<ma.length;i++){
for(intj=0;j<ma[0].length;j++){//mazemap[0]必須有元素不可為空
this.mazemap[i][j]=ma[i][j];
}
}
S=returnPlace('S');
F=returnPlace('F');
}
publicPointreturnPlace(chars){//返回數組中字元的位置
Pointpoint=newPoint();
for(inti=0;i<this.mazemap.length;i++){
for(intj=0;j<this.mazemap[0].length;j++){//mazemap[0]必須有元素不可為空
if(this.mazemap[i][j]==s)
{point.setX(i);
point.setY(j);
point.setDirection(1);
}
}
}
returnpoint;
}
publiccharreturnChar(Pointpoint){
if(point.getX()>=0&&point.getY()>=0)
returnthis.mazemap[point.getX()][point.getY()];
else
return'#';
}
publicvoidreplacePlace(Pointpoint,chars){//更改特定位置處的字元
mazemap[point.getX()][point.getY()]=s;
}
publicvoidprintPath(){
Stack<Point>tempPath=newStack<Point>();
while(!path.empty()){//對棧進行反序
tempPath.push(path.pop());
}
while(!tempPath.empty()){
System.out.print("("+tempPath.peek().getX()+","+tempPath.pop().getY()+")");
}
}
publicbooleangetPath(){//取得路徑的演算法如果有路徑就返回真
path=newStack<Point>();
S.setDirection(1);
path.push(S);
replacePlace(S,'X');
while(!path.empty()){
PointnowPoint=path.peek();//取得當前位置
if(nowPoint.getX()==F.getX()&&nowPoint.getY()==F.getY()){
//printPath();
returntrue;
}
Pointtemp=newPoint();//存放下一個可走的位置
intfind=0;//標志是否可向下走
while(nowPoint.getDirection()<5&&find==0){
switch(nowPoint.getDirection()){
case1:temp=newPoint(nowPoint.getX(),nowPoint.getY()-1,1);break;//取得當前位置左邊的位置
case2:temp=newPoint(nowPoint.getX()+1,nowPoint.getY(),1);break;//取得當前位置下邊的位置
case3:temp=newPoint(nowPoint.getX(),nowPoint.getY()+1,1);break;//取得當前位置右邊的位置
case4:temp=newPoint(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();
}
}
returnfalse;
}
publicvoidprintArr(){
for(inti=0;i<mazemap.length;i++){
for(intj=0;j<mazemap[0].length;j++){//mazemap[0]必須有元素不可為空
System.out.print(mazemap[i][j]);
}
System.out.println();
}
System.out.println();
}

}
packagecom.Albert.LabyringhStack;

publicclassMain{

/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
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'}
};
LabyringhStacksolution=newLabyringhStack(mazemap);
if(solution.getPath()){
System.out.print("迷宮路徑如下:");
solution.printPath();
}
else{
System.out.println("沒有可走的路");
}
}
}

J. JAVA機器人走迷宮的編程——急求!

深度優先的迷宮圖演算法。
N年前的老問題了。
10分不值得做。

閱讀全文

與迷宮演算法java相關的資料

熱點內容
體重小本app怎麼樣 瀏覽:776
編程語言需求 瀏覽:935
當兵體重怎麼個演算法 瀏覽:169
加密的電腦文件帶出去怎麼打開 瀏覽:849
死循環會在編譯的時候出錯嗎 瀏覽:986
c51單片機特殊寄存器的原理 瀏覽:578
閃耀永恆特利加密鑰 瀏覽:761
如何誇程序員 瀏覽:778
天津期貨python招聘 瀏覽:265
單片機機器語言寫的程序 瀏覽:552
韓國直播軟體app叫什麼名 瀏覽:918
軍營訓練不聽教官的命令 瀏覽:262
v開頭的音樂播放器是什麼APP 瀏覽:120
單片機是怎麼做出來的 瀏覽:317
博圖怎麼作為opc伺服器 瀏覽:102
編譯做題軟體 瀏覽:296
橋梁檢測pdf 瀏覽:688
化解壓力的一種方法 瀏覽:684
路由器和DSN伺服器有什麼區別 瀏覽:551
android伸縮控制項 瀏覽:861