Java迷宫算法问题(用栈实现)有算法简述

Python010

Java迷宫算法问题(用栈实现)有算法简述,第1张

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

}

LinkedList<TwoTuple<Integer, Integer>> trace = new LinkedList<TwoTuple<Integer, Integer>>()

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

System.out.println(trace)

}

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

List<TwoTuple<Integer, Integer>> trace) {

LinkedList<TwoTuple<Integer, Integer>> path = new LinkedList<TwoTuple<Integer, Integer>>()

HashSet<TwoTuple<Integer, Integer>> traverse = new HashSet<TwoTuple<Integer, 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 TwoTuple<Integer, Integer>(i, j))

}

}

}

while (!path.isEmpty()) {

TwoTuple<Integer, 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 TwoTuple<Integer, Integer>(temp.first,

temp.second + 1))

if (temp.second - 1 > 0

&& maze[temp.first][temp.second - 1] != 'W')

path.add(new TwoTuple<Integer, Integer>(temp.first,

temp.second - 1))

if (temp.first + 1 < maze.length

&& maze[temp.first + 1][temp.second] != 'W')

path.add(new TwoTuple<Integer, Integer>(temp.first + 1,

temp.second))

if (temp.first - 1 > 0

&& maze[temp.first - 1][temp.second] != 'W')

path.add(new TwoTuple<Integer, Integer>(temp.first - 1,

temp.second))

traverse.add(temp)

trace.add(temp)

}

}

trace.clear()

return false

}

}

class TwoTuple<A, 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

     LinkedList<MyPoint> stack=new LinkedList<MyPoint>()

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

WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOO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OOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW

WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

//作者: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

//邮箱:[email protected]

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

}

}

这个没啥含义啊,一个对象类

private int x

private int y

两个属性,从字面意义上说x是横坐标,y是纵坐标。

public Move(){

}无参数的构造方法

public Move(int x, int y){

this.x = x

this.y = y

}有参数的构造方法

下面那些get方法时用来访问对象属性的,set方法是给对象属性赋值的。

给你举个例子你就知道了

Move m1=new Move();这里声明了一个Move类的对象,调用的是无参数的构造方法,也就是说m1的x和y都是没有赋值的。但是呢,int类型默认值是0;

然后你可以通过set方法去给属性赋值m1.setX(2),然后通过get方法取值m1.getX()这个时候就能取到2了。

而有参数的构造方法就是在声明对象的时候直接赋值给元素

Move m2=new Move(1,2);这个时候你再用get方法去取,就会有x=1,y=2了,就是省掉了用set方法赋值的一步。

你还是先看看什么是对象,什么是构造函数,还有get,set方法的作用吧