Java 嘿,我正在编写这个,我有困难与它
我很难编写这个程序,不知道是否有人能帮我编写这个程序。 如果有人能帮我开始,那就太好了。非常感谢。 另外,我想知道我需要学习哪些部分的主题,因为我正在尝试编写代码,并且在编写代码时遇到了很大的困难。我知道,很奇怪,因为自从我第一次开始CS以来,我就没有遇到过这种困难Java 嘿,我正在编写这个,我有困难与它,java,recursion,Java,Recursion,我很难编写这个程序,不知道是否有人能帮我编写这个程序。 如果有人能帮我开始,那就太好了。非常感谢。 另外,我想知道我需要学习哪些部分的主题,因为我正在尝试编写代码,并且在编写代码时遇到了很大的困难。我知道,很奇怪,因为自从我第一次开始CS以来,我就没有遇到过这种困难 //Homework: revise this file, see TODO items below. //Given a maze of size N, the method fi
//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() {}
}
//作业:修改此文件,请参阅下面的待办事项。
//给定一个大小为N的迷宫,findPath(maze)方法会找到一个打开的
//如果存在从(0,0)到(N-1,N-1)的路径。
//
//有关其命令行用法,请参见main()注释。
//TODO:当前解决方案是递归的(dfs()调用自身)。你
//需要使其非递归。一种方法是使用显式堆栈
//而不是运行时堆栈。您不需要准确地找到
//与给定代码找到的开放路径相同。
//TODO(EC):修改findPath,以便它从
//(0,0)到(N-1,N-1),如果存在。你可以阅读一个方法
//对于本书第4.1节中的“广度优先搜索”。
//TODO(EC):定义findWallPath方法。每当findPath失败时
//找到一条路,就应该有一条“堵”墙的路。这条路
//可以从迷宫顶部或右侧的任何墙壁开始
//在迷宫底部或左侧的任何墙壁上结束。两面墙
//可以通过基数步或对角步相邻。同样,递归
//这是不允许的。找到一条墙的路径是好的,最短的更好。
//对于放坡,我们忽略main()方法,因此可以对其执行您喜欢的操作
//那个。我们只测试findPath和findWallPath方法。
公共类探路者
{
//这里的任何数据字段都应该是私有的和静态的。它们是存在的
//仅作为在用户之间共享搜索上下文的方便方式
//这里是静态方法。应该可以调用
//在不同的迷宫上多次使用findPath()方法
//以获得每个迷宫的有效结果。
//我们正在搜索的迷宫。
私有静态迷宫m;
//目标:对于每个可到达的打开位置p,父级[p.i][p.j]
//应为接近起始位置的打开位置
//这些父链接应该形成一棵树,根在S。
私有静态位置[][]父级;
公共静态Deque findPath(迷宫)
{
m=迷宫;//保存迷宫
int N=m.size();//迷宫是N乘N的
父项=新位置[N][N];//最初全部为空
位置S=新位置(0,0);//开始搜索
位置T=新位置(N-1,N-1);//搜索目标
//计算从S到达的每个位置的父级。
//既然S是根,我们就让S成为它自己的父。
//通过递归深度优先搜索计算父链接!
dfs(S,S);
//如果T没有父级,则它不可访问,因此没有路径。
if(父[T.i][T.j]==null)
返回null;
//否则,我们可以重建从S到T的路径。
LinkedQue路径=新建LinkedQue();
对于(位置u=T;!u.equals(S);u=parent[u.i][u.j])
路径addFirst(u);
path.addFirst(S);
返回路径;
}
//深度优先搜索:为每个可到达的p设置父级。
专用静态无效dfs(位置p,位置from)
{
如果(!m.inRange(p)| |!m.isOpen(p)| | parent[p.i][p.j]!=null)
返回;
//System.out.println(“通过父项“+from”找到“+p+”);
父母[p.i][p.j]=来自;
//现在递归地尝试p的四个邻居。
对于(int dir=0;dir这个问题最简单的方法是转换迷宫(如果还没有)进入图表,然后执行深度或广度优先搜索,搜索迷宫的出口。顺便说一句,你不应该在帖子中写任何关于家庭作业的内容。你有什么具体问题吗?你在问这个问题时犯了几个错误,可能导致被否决或关闭,包括没有问一个具体的问题,基本上是把工作交给我们为你做(不会发生),并发布一个没有总结您实际问题的标题。请浏览、和部分,了解该网站的工作原理,并帮助您改进当前和未来的问题,这可以帮助您获得更好的答案。事实上,我相信指导原则是,您应该声明这是家庭作业。它是否是hom无关紧要不管是不是作业,因为原始问题读起来更像是工作指令,而不是实际问题,应该加以改进。总比说,“你不应该在帖子中写任何关于作业的内容”要好。
将他引向