欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

迷宮java源代碼,JAVA迷宮

求走迷宮問題的算法,要求用Java寫的?

public class Maze { private int[][] maze = null;

成都創(chuàng)新互聯(lián)專注于望城企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,電子商務(wù)商城網(wǎng)站建設(shè)。望城網(wǎng)站建設(shè)公司,為望城等地區(qū)提供建站服務(wù)。全流程按需求定制網(wǎng)站,專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

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.StackNode stack = new java.util.StackNode();

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 + ")";

}

}

}

迷宮問題的求解源代碼

typedef struct{

int Col,Row;//迷宮的大小

int arr[Rangle][Rangle]; //0表示障礙,1表示是可走的通道,-1表示外界的圍墻

}MazeType;

void InitMaze(MazeType

M,int col,int row)

{

//按照用戶的輸入的行數(shù)row和列數(shù)col列的二維數(shù)組(元素值為1或0)

//設(shè)置迷宮的初值,加上邊緣的一圈的值

}

void PrintMaze(MazeType M)

{

//根據(jù)已經(jīng)進行二維數(shù)組的標記值來輸出迷宮(或者其通路)

}

bool Pass(MazeType M,PosType pos)

{//求解迷宮M中,從Start到end的一條路徑

//若存在則返回true,否則返回false

Stack S;

InitStack(S);

PosType curpos=start;//設(shè)置當前坐標為入口位置;

int curstep=1;

//當前的步數(shù)

bool Find=false;

//是否找到出口

ElemType e;

do{

if(Pass(M,curpos))

{

FootPrint(M,curpos);//在當前位置標記為2

e.step=1;

e.seat=curpos;

e.di=1;//初始化為向右邊位置移動

Push(S,e);

if(curpos.c==end.ccurpos.r==end.r)//如果找到了出口則終止,并返回true

{

Find=true;

return Find;

}

else{

curpos=NextPos(curpos,1);

curstep++;

}

}

else{//當前位置不能通過

if(!StackEmpty(S)){

Pop(S,e);//將已經(jīng)走過的最近位置彈出,數(shù)據(jù)保存在e中

while(e.di==4!(StackEmpty(S))){

MarkPrint(M,e.seat);//留下不能通過的標記

Pop(S,e);

curstep--;

}//while

if(e.di4)//不能通過則改變方向

{

e.di++;//方向順時針改變一下

Push(S,e);

curpos = NextPos(e.seat,e.di); //求下一個節(jié)點

}

}

}

}while(!StackEmpty(S)!Find);

//(!StackEmpty(S)!Find);//當棧不為空且沒有找到出口

return

false;//沒有找到出口則返回false

請幫忙用數(shù)據(jù)結(jié)構(gòu)(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;im;i++)

for(j=0;jn;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;im;i++)

for(j=0;jn;j++)

maze[i][j]=rand()%2;

//由于rand()產(chǎn)生的隨機數(shù)是從0到RAND_MAX

//RAND_MAX是定義在stdlib.h中的,其值至少為32767)

//要產(chǎn)生從X到Y(jié)的數(shù),只需要這樣寫:k=rand()%(Y-X+1)+X;

}

void print_maze(int m,int n){

int i,j;

printf("\n迷宮生成結(jié)果如下:\n\n");

printf("迷宮入口\n");

printf("↓");

for(i=0;im;i++)

{printf("\n");

for(j=0;jn;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;im;i++)

{ printf("\n");

for(j=0;jn;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+1n)(maze[p.row][p.col+1]==0)) visit(p.row,p.col+1,maze);

if((p.row+1m)(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-1p.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(" ☆歡迎進入迷宮求解系統(tǒng)☆\n");

printf(" 設(shè)計者:尹旭 林靜波(信息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請輸入行數(shù):");

scanf("%d",m);

printf("\n");

printf("請輸入列數(shù):");

scanf("%d",n);

while((m=0||m50)||(n=0||n50))

{ printf("\n抱歉,你輸入的行列數(shù)超出預(yù)設(shè)范圍(0-50,0-50),請重新輸入:\n\n");

printf("請輸入行數(shù):");

scanf("%d",m);

printf("\n");

printf("請輸入列數(shù):");

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請輸入行數(shù):");

scanf("%d",m);

printf("\n");

printf("請輸入列數(shù):");

scanf("%d",n);

while((m=0||m50)||(n=0||n50))

{printf("\n抱歉,你輸入的行列數(shù)超出預(yù)設(shè)范圍(0-50,0-50),請重新輸入:\n\n");

printf("請輸入行數(shù):");

scanf("%d",m);

printf("\n");

printf("請輸入列數(shù):");

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;

}

}

}

網(wǎng)頁題目:迷宮java源代碼,JAVA迷宮
網(wǎng)站URL:http://chinadenli.net/article26/heiccg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、App設(shè)計、云服務(wù)器、網(wǎng)站制作網(wǎng)站內(nèi)鏈、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)