递归:解决进口迷宫? [英] Recursion: Solving an imported maze?
问题描述
我工作的一个迷宫,我坚持。 JFileChooser的使用,我可以导入和读取到一个txt文件迷宫,在一系列的空间和主题标签确定的路径和墙壁。 字符串路径=;
和字符串墙=#;
这是该方法现在所做的一切:
@覆盖
公共无效readMaze()抛出FileNotFoundException异常
{
INT行;
INT关口; JFileChooser的选择器=新的JFileChooser();
扫描仪在= NULL; 如果(chooser.showOpenDialog(空)== JFileChooser.APPROVE_OPTION)
{
文件selectedFile = chooser.getSelectedFile();
在=新扫描仪(selectedFile); / *读取文件* /
扫描仪的readLine =新的扫描仪(新的FileReader(selectedFile)); COL = readLine.nextInt(); //一读
的System.out.println(COL);
行= readLine.nextInt(); //二读
的System.out.println(行); 数组[] []数组=新的Array [行] [COL]; 做
{
串I = readLine.nextLine(); //System.out.println(i); 对于(INT J = 0; J< i.length(); J ++)
{
串比较= i.substring(J,J + 1); 如果(compare.equals(墙))
{
//什么都不做,看看是否有相邻的路径。
的System.out.println(墙);
}
如果(compare.equals(路径))
{
的System.out.println(路径);
//检查其他路径。
//不,如果探索
//别的探索
}
} }而(readLine.hasNextLine()==真); readLine.close(); } }
这是我们作为接口现在,如果它的事项:
公共接口lab3MVCInterface
{
枚举方向{N,S,E,W}; 无效readMaze()抛出FileNotFoundException异常;
无效solveMaze(); JFileChooser的选择器=新的JFileChooser();
扫描仪在= NULL; 字符串墙=#;
字符串路径=;
无效readMaze(选配JFileChooser的扫描仪中)
抛出FileNotFoundException异常;
}
但是,我们没有任何线索如何确定如何确定是否有一个路径或没有。这是一类任务,所以我不寻找答案,但我会在适当的方式向正确的方向AP preciate指针。对于分配,我们可以使用递归或栈来完成它......我们决定递归因为栈是在我们结束了一场噩梦。
感谢您的帮助! :)我知道在过去,我还没有通过与螺纹跟随,但我在努力更新它们(我把自己锁了电子邮件,失去了我的账户密码后,进入如图),因为我们说话!
编辑:这里是code的样品从一个.txt文件打开:
(第一个数字将数组[COL] []值,第二组数组[] [行]值大小。)
7
7
#######
###
### ###
### ###
#
### ###
#######
首先,我不认为数组[] []
是使用了正确的数据类型二维数组的字符。也许你可以使用的char [] []
来代替,因为每个元素将是要么或
'#'
。
其次,你可以做的是开始在迷宫的进入点,并更改到
。
,表示我可以在这里;然后更改每到
。
如果是相邻的''。
。
车床,冲洗和重复,直到你到达终点,或耗尽的字符更改为。
(在这种情况下,迷宫是不可能的)
I'm working on a maze and am stuck. Using JFileChooser, I can import and read into the maze in a .txt file, where a series of spaces and hashtags determine paths and walls. String path = " ";
and String wall = "#";
This is the method as of now doing everything:
@Override
public void readMaze() throws FileNotFoundException
{
int row;
int col;
JFileChooser chooser = new JFileChooser();
Scanner in = null;
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
File selectedFile = chooser.getSelectedFile();
in = new Scanner(selectedFile);
/*READ FILE*/
Scanner readLine = new Scanner (new FileReader(selectedFile));
col = readLine.nextInt();//first reading
System.out.println(col);
row = readLine.nextInt(); //second reading
System.out.println(row);
Array[][] array = new Array[row][col];
do
{
String i = readLine.nextLine();
//System.out.println(i);
for (int j = 0; j < i.length(); j++)
{
String compare = i.substring(j, j+1);
if(compare.equals(wall))
{
//Do nothing, see if there's a path adjacent.
System.out.println("Wall");
}
if(compare.equals(path))
{
System.out.println("Path");
//Check for additional paths.
// don't if explored
//else explore
}
}
} while(readLine.hasNextLine() == true);
readLine.close();
}
}
This is our interface as of now, if it matters: public interface lab3MVCInterface
{
enum direction {N, S, E, W};
void readMaze() throws FileNotFoundException;
void solveMaze();
JFileChooser chooser = new JFileChooser();
Scanner in = null;
String wall = "#";
String path = " ";
void readMaze(JFileChooser chooser, Scanner in)
throws FileNotFoundException;
}
But we don't have any clue how to determine how to determine if there's a path or not. This is a class assignment, so I'm not looking for answers, but I would appreciate pointers in the right direction on a proper approach. For the assignment, we can use recursion or stacks to complete it... we decided on recursion since stacks was a nightmare on our end.
Thank you for your assistance!! :) I know in the past I have not followed through with threads, but I AM making an effort to update them (I locked myself out of the email, after losing the password for my account. Go figure) as we speak!
Edit: Here's a sample of code opened from a .txt file: (The first number sets the array[col][] value, the second sets the array[][row] value for sizing.)
7
7
#######
###
### ###
### ###
#
### ###
#######
Firstly, I don't think Array[][]
is the right datatype to use for a 2D array of characters. Maybe you could use a char[][]
instead, since each element will be either ' '
or '#'
.
Secondly, what you could do is start at the "entry" point of the maze, and change the ' '
to '.'
to indicate "I can get here"; then change every ' '
to '.'
if it's adjacent to a '.'
.
Lather, rinse and repeat, until you EITHER reach the end point, OR run out of characters to change to '.'
(in which case the maze is impossible).
这篇关于递归:解决进口迷宫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!