java迷宮,java迷宮演算法

本文目錄一覽:

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

import java.io.File;

import java.io.FileNotFoundException;

import java.util.HashSet;

import java.util.LinkedList;

import java.util.List;

import java.util.Scanner;

public class Test {

public static void main(String[] args) throws FileNotFoundException {

Scanner input = new Scanner(new File(“Maze2tong.txt”));

int row = 0;

char[][] Mazemap = new char[12][58];

while (input.hasNext()) {

String line = input.nextLine();

for (int column = 0; column = line.length() – 1; column++) {

char c = line.charAt(column);

Mazemap[row][column] = c;

}

row++;

}

for (int i = 0; i  12; i++) {

for (int j = 0; j  58; j++) {

System.out.print(Mazemap[i][j]);

}

System.out.print(“\n”);

}

LinkedListTwoTupleInteger, Integer trace = new LinkedListTwoTupleInteger, Integer();

System.out.println(maze(Mazemap, trace));

System.out.println(trace);

}

public static boolean maze(char[][] maze,

ListTwoTupleInteger, Integer trace) {

LinkedListTwoTupleInteger, Integer path = new LinkedListTwoTupleInteger, Integer();

HashSetTwoTupleInteger, Integer traverse = new HashSetTwoTupleInteger, Integer();

for (int i = 0; i  maze.length; i++) {

for (int j = 0; j  maze[i].length; j++) {

if (maze[i][j] == ‘S’) {

path.add(new TwoTupleInteger, Integer(i, j));

}

}

}

while (!path.isEmpty()) {

TwoTupleInteger, Integer temp = path.pop();

if (traverse.contains(temp)) {

continue;

} else if (maze[temp.first][temp.second] == ‘F’) {

trace.add(temp);

return true;

} else if (!traverse.contains(temp)) {

if (temp.second + 1  maze[temp.first].length

 maze[temp.first][temp.second + 1] != ‘W’)

path.add(new TwoTupleInteger, Integer(temp.first,

temp.second + 1));

if (temp.second – 1  0

 maze[temp.first][temp.second – 1] != ‘W’)

path.add(new TwoTupleInteger, Integer(temp.first,

temp.second – 1));

if (temp.first + 1  maze.length

 maze[temp.first + 1][temp.second] != ‘W’)

path.add(new TwoTupleInteger, Integer(temp.first + 1,

temp.second));

if (temp.first – 1  0

 maze[temp.first – 1][temp.second] != ‘W’)

path.add(new TwoTupleInteger, Integer(temp.first – 1,

temp.second));

traverse.add(temp);

trace.add(temp);

}

}

trace.clear();

return false;

}

}

class TwoTupleA, B {

public final A first;

public final B second;

public TwoTuple(A a, B b) {

first = a;

second = b;

}

@Override

public int hashCode() {

return first.hashCode() + second.hashCode();

}

@Override

public boolean equals(Object obj) {

if (!(obj instanceof TwoTuple)) {

}

return obj instanceof TwoTuple  first.equals(((TwoTuple) obj).first)

 second.equals(((TwoTuple) obj).second);

}

public String toString() {

return “(” + first + “, ” + second + “)”;

}

} // /:-

import java.io.File;

import java.io.FileNotFoundException;

import java.util.LinkedList;

import java.util.Scanner;

class MyPoint

{

    public boolean visited=false;

    public int parentRow=-1;

    public int parentColumn=-1;

    public final char content;

    public int x;

    public int y;

    public MyPoint(char c,int x,int y)

    {

     this.content = c;

     this.x = x;

     this.y = y;

    }

}

public class Maze

{

    public static MyPoint[][] getMazeArray(){

Scanner input = null;

MyPoint[][] mazemap = new MyPoint[12][58];

try {

input = new Scanner(new File(“Maze2tong.txt”));

int row = 0;

while (input.hasNext()) {

String line = input.nextLine();

for (int column = 0; column = line.length() – 1; column++) {

char c = line.charAt(column);

MyPoint point = new MyPoint(c,row,column);

mazemap[row][column] = point;

}

row++;

}

input.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return mazemap;

    }

    public static boolean tomRun(MyPoint[][] maze,MyPoint end)

    {

     int x = maze.length;

     int y = maze[0].length;

     LinkedListMyPoint stack=new LinkedListMyPoint();

for (int i = 0; i  maze.length; i++) {

for (int j = 0; j  maze[i].length; j++) {

if (maze[i][j].content == ‘S’) {

stack.push(maze[i][j]);

maze[i][j].visited=true;

}

}

}

        boolean result=false;

        while(!stack.isEmpty())

        {

            MyPoint t=stack.pop();

           //System.out.println(“pop point:”+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;

            }

 

        }

        return result;

    }

    public static void show(int x,int y,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+”]”);

    }

    public static void main(String[] args)

    {

     MyPoint[][] maze = getMazeArray();

     MyPoint point = new MyPoint(‘c’,1,1);

        if(tomRun(maze,point))

        {

            System.out.println(“逃生路徑如下:”);

            show(point.x,point.y,maze);

        }

        else

            System.out.println(“無法走出迷宮!”);

    }

}

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW

WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW

WWWWWWOOO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WWWWWWWOOOOOOOWWWWWWWWWWWOOW

WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW

WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

java怎麼生成迷宮地圖

//作者:zhongZw   

package cn.zhongZw.model;

import java.util.ArrayList;

import java.util.Random;

public class MazeModel {

private int width = 0;

private int height = 0;

private Random rnd = new Random();

public MazeModel() {

this.width = 50; //迷宮寬度

this.height = 50; //迷宮高度

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

this.width = width;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

public MazeModel(int width, int height) {

super();

this.width = width;

this.height = height;

}

public ArrayList  MazePoint  getMaze() {

ArrayList  MazePoint  maze = new ArrayList  MazePoint  ();

for (int h = 0; h  height; h++) {

for (int w = 0; w  width; w++) {

MazePoint point = new MazePoint(w, h);

maze.add(point);

}

}

return CreateMaze(maze);

}

private ArrayList  MazePoint  CreateMaze(ArrayList  MazePoint  maze) {

int top = 0;

int x = 0;

int y = 0;

ArrayList  MazePoint  team = new ArrayList  MazePoint  ();

team.add(maze.get(x + y * width));

while (top = 0) {

int[] val = new int[] {

-1, -1, -1, -1

};

int times = 0;

boolean flag = false;

MazePoint pt = (MazePoint) team.get(top);

x = pt.getX();

y = pt.getY();

pt.visted = true;

ro1: while (times  4) {

int dir = rnd.nextInt(4);

if (val[dir] == dir)

continue;

else

val[dir] = dir;

switch (dir) {

case 0: // 左邊

if ((x – 1) = 0  maze.get(x – 1 + y * width).visted == false) {

maze.get(x + y * width).setLeft();

maze.get(x – 1 + y * width).setRight();

team.add(maze.get(x – 1 + y * width));

top++;

flag = true;

break ro1;

}

break;

case 1: // 右邊

if ((x + 1)  width  maze.get(x + 1 + y * width).visted == false) {

maze.get(x + y * width).setRight();

maze.get(x + 1 + y * width).setLeft();

team.add(maze.get(x + 1 + y * width));

top++;

flag = true;

break ro1;

}

break;

case 2: // 上邊

if ((y – 1) = 0  maze.get(x + (y – 1) * width).visted == false) {

maze.get(x + y * width).setUp();

maze.get(x + (y – 1) * width).setDown();

team.add(maze.get(x + (y – 1) * width));

top++;

flag = true;

break ro1;

}

break;

case 3: // 下邊

if ((y + 1)  height  maze.get(x + (y + 1) * width).visted == false) {

maze.get(x + y * width).setDown();

maze.get(x + (y + 1) * width).setUp();

team.add(maze.get(x + (y + 1) * width));

top++;

flag = true;

break ro1;

}

break;

}

times += 1;

}

if (!flag) {

team.remove(top);

top -= 1;

}

}

return maze;

}

}

迷宮

[java] view plain copy

//作者:zhongZw

//郵箱:zhong317@126.com

package cn.zhongZw.model;

import java.util.*;

import java.lang.*;

public class MazePoint {

private int left = 0;

private int right = 0;

private int up = 0;

private int down = 0;

private int x;

private int y;

public boolean visted;

public MazePoint(int x, int y) {

this.x = x;

this.y = y;

}

public int getLeft() {

return left;

}

public void setLeft() {

this.left = 1;

}

public int getRight() {

return right;

}

public void setRight() {

this.right = 1;

}

public int getUp() {

return up;

}

public void setUp() {

this.up = 1;

}

public int getDown() {

return down;

}

public void setDown() {

this.down = 1;

}

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;

}

}

java老鼠迷宮代碼難嗎

非常難。思路:

1、設老鼠的行進路線都是優先選擇下-右-上-左。

2、設老鼠很聰明,走過的路線走撒泡尿,表示鼠大爺到此一游,我們可以把數組的值改為3,表示走過,但走不通。

3、這是一個int[8][8]的二位數組,那麼開始位置下標是1,1,結束位置是6,6。行和列分別用、j表示。

4、實際路線我們可以設置2表示,我們可以使用遞歸,讓老鼠不斷測試路線。

5、最後列印數組,看老鼠的實際路線。

原創文章,作者:XCVX,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/135405.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
XCVX的頭像XCVX
上一篇 2024-10-04 00:12
下一篇 2024-10-04 00:12

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • 蝴蝶優化演算法Python版

    蝴蝶優化演算法是一種基於仿生學的優化演算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化演算法Python版…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Python實現爬樓梯演算法

    本文介紹使用Python實現爬樓梯演算法,該演算法用於計算一個人爬n級樓梯有多少種不同的方法。 有一樓梯,小明可以一次走一步、兩步或三步。請問小明爬上第 n 級樓梯有多少種不同的爬樓梯…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29

發表回復

登錄後才能評論