「迷宫路线java」迷宫路线设计

博主:adminadmin 2023-01-14 15:30:09 370

本篇文章给大家谈谈迷宫路线java,以及迷宫路线设计对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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迷宫路径总条数问题

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高手帮忙写一迷宫程序

给你代码,你给出的那两个类,不能满足,我的需要,我就没有使用。

你看一下吧。

----------------------------------------

package stackpackage;

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Maze {

// 上

private static Point directionTop = new Point(-1, 0);

// 下

private static Point directionBottom = new Point(1, 0);

// 左

private static Point directionLeft = new Point(0, -1);

// 右

private static Point directionRight = new Point(0, 1);

private static Point[] directions = { directionTop, directionRight,

directionBottom, directionLeft };

private static boolean isStop = false;

private static int row = 0;

private static int col = 0;

private static Point startPoint = new Point();

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

FileReader fr = new FileReader("data.txt");

BufferedReader br = new BufferedReader(fr);

int rowIndex = 1;

int[][] maze = null;

while (br.ready()) {

String line = br.readLine();

Scanner sc = new Scanner(line);

if (rowIndex == 1) {

row = sc.nextInt();

col = sc.nextInt();

maze = new int[row][col];

} else {

if (rowIndex row + 2) {

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

maze[rowIndex - 2][i] = sc.nextInt();

}

} else {

startPoint.x = sc.nextInt();

startPoint.y = sc.nextInt();

}

}

rowIndex++;

}

ListPoint route = new ArrayListPoint();

route.add(startPoint);

findNext(startPoint);

puzzle(maze, startPoint, route);

System.out.println(route);

}

private static void puzzle(int[][] maze, Point p, ListPoint route) {

if (isStop) {

return;

}

Point[] nextDirections = p.nextDirections;

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

if (isStop) {

return;

}

Point direction = nextDirections[i];

Point newP = new Point(p.x + direction.x, p.y + direction.y);

if (newP.isEffective() maze[newP.x][newP.y] == 0

!route.contains(newP)) {

newP.before = p;

findNext(newP);

route.add(newP);

if (isExit(newP)) {

isStop = true;

break;

}

puzzle(maze, newP, route);

}

}

if (isStop) {

return;

}

route.remove(route.size() - 1);

}

private static void findNext(Point p) {

int index = 0;

Point[] nextDirections = new Point[3];

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

nextDirections[i] = new Point(0, 0);

}

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

Point direction = directions[i];

Point newP = new Point(p.x + direction.x, p.y + direction.y);

if (newP.isEffective() !newP.equals(p.before) newP.x row

newP.y col) {

nextDirections[index++] = direction;

}

}

p.nextDirections = nextDirections;

}

private static boolean isExit(Point p) {

if (startPoint.equals(p)) {

return false;

}

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

Point direction = directions[i];

Point newP = new Point(p.x + direction.x, p.y + direction.y);

if (!newP.equals(p.before)

(newP.x = row || newP.y = col || newP.x 0 || newP.y 0)) {

return true;

}

}

return false;

}

}

class Point {

int x = 0;

int y = 0;

Point[] nextDirections = null;

Point before = null;

public Point() {

}

public Point(int x, int y) {

this.x = x;

this.y = y;

}

public String toString() {

return "" + x + "," + y + "";

}

public boolean isEffective() {

return x = 0 y = 0;

}

public boolean equals(Object obj) {

return equals((Point) obj);

}

public boolean equals(Point p) {

if (p == null) {

return false;

}

return this.x == p.x this.y == p.y;

}

}

JAVA走迷宫问题-怎么使迷宫一定走通?

可以逆向思考,怎么说呢,就是说。

假设现在咱们就在迷宫的出口,那么你往回走(这个往回走,就是随机产生的,可以任意一个方向),最后到达入口,那么这么一个路线就一定可以走的通 呀。。。

JAVA迷宫问题,怎么输出所走路径的坐标

在Way函数的if(Maza[Loci][Locj]==0) 语句里面Maza[Loci][Locj]=2;前面加一句

System.out.println("The Maza route is ("+Loci+","+Locj+")");就打印出程序所走路径的坐标了.

求用java语言寻找走出迷宫路线的算法

首先给定一个初始坐标,然后构建一个容器保存坐标值,之后进行迭代,横坐标+1,或者纵坐标+1,这个顺寻自己定义(四个方向),经过的“路径”保存在那个容器中,如果遇到死角,以此往回迭代,在容器中将遇到死角的那个坐标删除。最后找到自己定义的那个迷宫出口坐标。

迷宫路线java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于迷宫路线设计、迷宫路线java的信息别忘了在本站进行查找喔。