如何用java自动生成迷宫_用Java创建迷宫求解算法

我被赋予了在

Java中创建迷宫求解器的任务.这是作业:

Write an application that finds a path through a maze.

The maze should be read from a file. A sample maze is shown below.

O O O O O X O

X X O X O O X

O X O O X X X

X X X O O X O

X X X X O O X

O O O O O O O

X X O X X X O

字符“X”表示墙壁或阻挡位置,字符“O”表示字符

空缺职位.你可以假设迷宫的入口总是在右下方

角落,出口始终位于左上角.你的程序应该发送它

输出到文件.如果找到路径,则输出文件应包含路径.如果路径是

没有找到应该发送到该文件的消息.请注意,迷宫可能超过

一个解决方案路径,但在本练习中,您只需要找到一个解决方案,而不是

所有解决方案

您的程序应该使用堆栈来记录它正在探索的路径并在其时回溯

到达阻挡位置.

在编写代码之前,请务必编写完整的算法.随意创建任何额外的

可帮助您完成作业的课程.

Here's my Algorithm:

1)Initialize array list to hold maze

2)Read text file holding maze in format

o x x x x o

o o x o x x

o o o o o x

x x x x o o

3)Create variables to hold numbers of columns and rows

3)While text file has next line

A. Read next line

B. Tokenize line

C. Create a temporary ArrayList

D. While there are tokens

i. Get token

ii. create a Point

iii. add point to temp ArrayList

iv. increment maximum number of columns

E. Add temp to maze arraylist, increment max rows

F. initialize a hold of points as max rows - 1

G. Create a start point with x values as maximum number of rows - 1, and y values as maximum number of columns - 1

H. Create stack of points and push starting location

I. While finished searching is not done

i. Look at top of stack and check for finish

ii. check neighbors

iii. is there an open neighbor?

- if yes, update flags and push

- if no, pop from stack

J. Print solution

4. Done is true

无论如何,我设置的是一个Points类,它设置/获取在所有基本方向上行进的方法,这些方法将返回如图所示的布尔值:

public class Points

{

private int xCoord;

private int yCoord;

private char val;

private boolean N;

private boolean S;

private boolean E;

private boolean W;

public Points()

{

xCoord =0;

yCoord =0;

val =' ';

N = true;

S = true;

E = true;

W = true;

}

public Points (int X, int Y)

{

xCoord = X;

yCoord = Y;

}

public void setX(int x)

{

xCoord = x;

}

public void setY(int y)

{

yCoordinate = y;

}

public void setNorth(boolean n)

{

N = n;

}

public void setSouth(boolean s)

{

S= s;

}

public void setEast(boolean e)

{

E = e;

}

public void setWest(boolean w)

{

W = w;

}

public int getX()

{

return xCoord;

}

public int getY()

{

return yCoord;

}

public char getVal()

{

return val;

}

public boolean getNorth()

{

return N;

}

public boolean getSouth()

{

return S;

}

public boolean getEast()

{

return E;

}

public boolean getWest()

{

return W;

}

public String toString1()

{

String result = "(" + xCoord + ", " +yCoord + ")";

return result;

}

}

我只是在主要的实际解决方面遇到了问题.这就是我所拥有的:

import java.io.*;

import java.util.*;

import java.lang.*;

import java.text.*;

public class MazeSolve1

{

public static void main(String[] args)

{

//Create arrayList of Points

ArrayList> MAZE = new ArrayList>();

Scanner in = new Scanner(System.in);

//Read File in

System.out.print("Enter the file name: ");

String fileName = in.nextLine();

fileName = fileName.trim();

FileReader reader = new FileReader(fileName+".txt");

Scanner in2 = new Scanner(reader);

//Write file out

FileWriter writer = new FileWriter("Numbers.out");

PrintWriter out = new PrintWriter(writer);

boolean done = false;

int maxCol = 0;

int maxRow = 0;

while(!done) {

//creating array lists

while (in2.hasNextLine()) {

//Read next line

String nextLine = in2.nextLine();

//Tokenize Line

StringTokenizer st = new StringTokenizer(nextLine, " ");

//Create temp ArrayList

ArrayList> temp = new ArrayList>();

//While there are more tokens

while (st.hasNextToken()) {

String token = st.nextToken();

Points pt = new Points();

temp.add(pt);

maxCol++

}

MAZE.add(temp);

maxRow++;

}

//create hold arraylist for max rows of maze -1

//create variables for start x and y coordinates

ArrayList> hold = new ArrayList>();

hold = MAZE.get(maxRow - 1);

int startColumn = hold.get(maxCol - 1);

int startRow = hold.get(maxRow - 1);

Point start = new Point();

start.setX(startColumn);

start.setY(startRow);

//initialize stack, and push the start position

MyStack st = new ArrayStack();

st.push(start.toString1());

//south and east of start are edges of array

start.setSouth(false);

start.setEast(false);

//while your position is not equal to point (0,0) [finish]

while (st.peek() != "(0, 0)") {

//getting the next coordinate to the North

int nextY = start.getY() - 1;

int nextX = start.getX();

//if character to the North is an O it's open and the North flag is true

if (hold.get(nextY) = 'O' && start.getNorth() == true) {

//set flags and push coordinate

start.setNorth(false);

st.push(start.toString1());

}

//else pop from stack

else { st.pop(); }

//look at coordinate to the East

nextX = start.getX() + 1;

//if character to the East is a O and the East flag is true

if (hold.get(nextX) = 'O' && start.getEast() == true) {

//set flags and push coordinate

start.setEast(false);

st.push(start.toString1());

}

//else pop from stack

else { st.pop(); }

//look at coordinate to the South

nextY = start.getY() + 1;

//if character to the South is a O and the West flag is true

if (hold.get(nextY) = 'O' && start.getSouth() == true) {

//set flags and push coordinate

start.setSouth(false);

st.push(start.toString1());

}

//else pop from stack

else { st.pop() }

//keep looping until the top of the stack reads (0, 0)

}

done = true;

}

//Print the results

System.out.println("---Path taken---");

for (int i = 0; i< st.size(); i++) {

System.out.println(st.pop);

i++

}

除了任何语法错误,你们可以给我一些帮助吗?非常感谢.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值