我在编码时遇到了困难,并且想知道是否有人可以帮我编写这个程序。 如果有人可以帮助我开始,那就太好了。谢谢。 另外,我想知道哪些部分需要我需要研究的主题,因为我正在尝试编码,并且很难对此进行编码。我知道,很奇怪,因为我没有遇到这个困难,因为我首先开始CS。
//Homework: revise this file, see TODO items below.
//Given a maze of size N, the method findPath(maze) finds an open
//path from (0,0) to (N-1,N-1), if such a path exists.
//
//See main() comments for its command line usage.
//TODO: the current solution is recursive (dfs() calls itself). You
//need to make it non-recursive. One way is to use an explicit stack
//instead of the runtime stack. You do not need to find exactly the
//same open paths as found by the given code.
//TODO(EC): modify findPath so it finds a *shortest* open path from
//(0,0) to (N-1,N-1), when one exists. You can read about a method
//for this, "breadth first search", in Section 4.1 of your book.
//TODO(EC): define the method findWallPath. Whenever findPath fails to
//find a path, there should be a "blocking" path of walls. This path
//can start at any wall on the top or right sides of the maze, and
//end at any wall at the bottom or left sides of the maze. Two walls
//can be adjacent by a cardinal OR diagonal step. Again, recursion
//is not allowed. Finding a wall path is good, shortest is better.
//For grading, we ignore the main() method, so do what you like with
//that. We only test your findPath and findWallPath methods.
public class PathFinder
{
// Any data fields here should be private and static. They exist
// only as a convenient way to share search context between your
// static methods here. It should be possible to call your
// findPath() method more than once, on different mazes, and
// to get valid results for each maze.
// The maze we are currently searching.
private static Maze m;
// GOAL: for each reachable open position p, parent[p.i][p.j]
// should be an open position that is closer to the start position
// S. These parent links should form a tree, rooted at S.
private static Position[][] parent;
public static Deque<Position> findPath(Maze maze)
{
m = maze; // save the maze
int N = m.size(); // the maze is N by N
parent = new Position[N][N]; // initially all null
Position S = new Position(0,0); // start of search
Position T = new Position(N-1,N-1); // goal of search
// Compute parent for each position reachable from S.
// Since S is the root, we will let S be its own parent.
// Compute parent links, by recursive depth-first-search!
dfs(S, S);
// If T has no parent, it is not reachable, so no path.
if (parent[T.i][T.j]==null)
return null;
// Otherwise, we can reconstruct a path from S to T.
LinkedDeque<Position> path = new LinkedDeque<Position>();
for (Position u=T; !u.equals(S); u=parent[u.i][u.j])
path.addFirst(u);
path.addFirst(S);
return path;
}
// depth-first-search: set parent for each reachable p.
private static void dfs(Position p, Position from)
{
if (!m.inRange(p) || !m.isOpen(p) || parent[p.i][p.j] != null)
return;
// System.out.println("found " + p + " via parent " + from);
parent[p.i][p.j] = from;
// Now recursively try the four neighbors of p.
for (int dir=0; dir<4; ++dir)
dfs(p.neighbor(dir), p);
}
// This method is entirely up to you.
public static Deque<Position> findWallPath(Maze maze) { return null; }
// Usage:
// java PathFinder MAZEFILE
// Reads maze, finds path, prints maze with path.
public static void main(String[] args)
throws java.io.IOException
{
Maze m = new Maze(args[0]);
System.out.println(m);
Deque<Position> path = findPath(m);
if (path==null) {
System.out.println("No path found");
return;
}
System.out.println("Found path of " +
path.size() +
" positions");
System.out.println(path);
char[][] map = m.copyArray();
// Mark the path with X's
for (Position p: path)
{
assert map[p.i][p.j] == '0';
map[p.i][p.j] = 'X';
}
// Now print the marked map.
System.out.println(Maze.toString(map));
}
// Java "best practice": this class is all static, so we should
// never instantiate it. To enforce that, we make its default
// constructor private.
private PathFinder() {}
}
答案 0 :(得分:0)
解决这个问题的最简单方法是将迷宫(如果还没有)转换成图形,然后实现搜索迷宫退出的深度或广度优先搜索。顺便说一下。你不应该在你的帖子上写任何有关家庭作业的内容。