正方形网格中所有行走路径的Java程序
我正在尝试编写一个程序,该程序(在控制台中)输出所有可能的路径(以正方形网格中所有行走路径的Java程序,java,algorithm,recursion,Java,Algorithm,Recursion,我正在尝试编写一个程序,该程序(在控制台中)输出所有可能的路径(以(x1,x1)-->(x2,x2)-->等格式)用于导航大小为NxN的网格,从左上到右下;i、 e.从(0,0)到(N-1,N-1)。你只能向下或向右移动;i、 e.从(0,0)到(0,1)或到(1,0)。我希望程序在每次找到完整的路径集(即从左上到右下的所有移动)时输出该路径集是什么 似乎最好的编写方法是使用递归方法将每个移动输入arrayList(请参阅buildPath方法-程序中的最后一个方法),这就是我遇到的问题 为了使
(x1,x1)-->(x2,x2)-->等格式)
用于导航大小为NxN的网格,从左上到右下;i、 e.从(0,0)到(N-1,N-1)。你只能向下或向右移动;i、 e.从(0,0)到(0,1)或到(1,0)。我希望程序在每次找到完整的路径集(即从左上到右下的所有移动)时输出该路径集是什么
似乎最好的编写方法是使用递归方法将每个移动输入arrayList(请参阅buildPath
方法-程序中的最后一个方法),这就是我遇到的问题
为了使它稍微复杂一点,我还生成了随机网格位置,这些位置是“禁止进入”的,因此无法通过。
这就是说,一旦我们/我弄清楚如何实际使用任何路径,我可能可以自己解决这一部分
如何实现递归方法来确定哪些路径是可能的?非常感谢您的帮助(即使是伪代码也比没有好)
以下是我迄今为止的代码(简单的位是伪代码,以使其更容易完成,但请告诉我是否应该将完整的代码放入):
import java.util.*;
公共类递归算法{
public static ArrayList allpath=new ArrayList();
公共静态ArrayList路径集=新建ArrayList();
公共静态int路径;
公共静态整数N,M,x=0,y=0;
公共静态字符串nString、mString;
公共静态布尔网格[];
公共静态int右下;
@抑制警告(“资源”)
公共静态void main(字符串[]args){
//将当前位置设置为(0,0)
右=0;
向下=0;
N的输入值(网格大小)
M的输入值(禁区位置的数量)
offLimits(N,M);//调用offLimits方法随机生成offLimits位置
buildPath(right,down,allpath,N);//调用buildPath方法
}
公共静态无效限制(整数N,整数M){
int mCount=0;
如果(M==0){
}否则{
而(mCount<(M+1)){
//整数范围1=(最大-最小)+1;
int range1=((N-1)-1)+1;
int range2=((N-1)-0)+1;
int random1=(int)((Math.random()*range1)+1);
int random2=(int)(Math.random()*range2);
//如果在终点生成了“禁区”点,则将其移动到左侧1处或上方1处
如果((random1==N-1)和&(random2==N-1)){
int switchSelect=(int)(Math.random()*2);
同时(开关选择>0){
开关(开关选择){
案例1:随机1-;
打破
案例2:随机2-;
打破
}
}
}
//将栅格位置设置为“禁区”(即,random1=1,random2=2-->(1,2)为“禁区”)
网格[random1][random2]=真;
//统计生成的禁区网格位置的数量
mCount++;
}
}
}
公共静态ArrayList构建路径(int-right、int-down、ArrayList-AllPath、int-N){
//使用当前位置更新路径(右、下)
/*****从现在起我就有麻烦了*****/
//停止条件
如果((右==N-1)和(&(下==N-1)){//机器人不能向右移动
添加(路径);
返回路径集;
}
//递归步骤
如果(right==N-1){//机器人不能向右移动
构建路径(右下+1,所有路径,N);
}否则,如果(down==N-1){//机器人无法下降
构建路径(右+1,下,所有路径,N);
}否则{//机器人可以向右或向下移动
构建路径(右+1,下,所有路径,N);
//add(Integer.toString(右));
//add(Integer.toString(down));
构建路径(右下+1,所有路径,N);
if(网格[x][y]==false){
//有效的新位置(用x和y代替建议的新路径步长)
}else if(网格[x][y]==true){
//禁区位置(用x和y代替建议的新路径步骤)
}
}
返回路径集;
}
}
您走上了正确的道路,但正朝着一个比需要更复杂的解决方案前进。这里有一种方法发现它们允许所有4个指南针方向(不仅仅是右下)。看看通过删除代码可以使它变得多么简单
import java.util.LinkedHashSet;
班级实验{
静态类探路者{
最终整数网格大小;
最终布尔值[][]被阻止;
最终合作目标;
最终LinkedHashSet路径=新LinkedHashSet();
最终随机生成=新随机生成();
PathFinder(int gridSize,int Nblock){
this.gridSize=gridSize;
this.isBlocked=新布尔值[gridSize][gridSize];
this.goal=新坐标(gridSize-1,gridSize-1);
//如果nblock太大,这会变得非常低效。
对于(int n=0;n0&&!被阻止[coord.x-1][coord.y])
searchFrom(newcoord(Coord.x-1,Coord.y));
如果(coord.y>0&&!被阻止[coord.x][coord.y-1])
searchFrom(新合作伙伴),
import java.util.*;
public class RecursiveAlgorithm {
public static ArrayList<Integer> allPaths = new ArrayList<Integer>();
public static ArrayList<String> pathSet = new ArrayList<String>();
public static int path;
public static int N, M, x = 0, y = 0;
public static String nString, mString;
public static boolean grid[][];
public static int right, down;
@SuppressWarnings("resource")
public static void main(String[] args) {
//sets the current position to (0,0)
right = 0;
down = 0;
Input value of N (size of grid)
Input value of M (number of off-limits locations)
offLimits(N, M); //calls offLimits method to randomly generate off-limits locations
buildPath(right, down, allPaths, N); //calls buildPath method
}
public static void offLimits (int N, int M) {
int mCount = 0;
if (M == 0){
} else {
while (mCount < (M + 1)) {
//int range1 = (max - min) + 1;
int range1 = ((N-1) - 1) + 1;
int range2 = ((N-1) - 0) + 1;
int random1 = (int)((Math.random() * range1) + 1);
int random2 = (int)(Math.random() * range2);
//if an 'off-limits' point is generated at the finish point, move it to either 1 place to the left or 1 place above
if ((random1 == N-1) && (random2 == N-1)) {
int switchSelect = (int)(Math.random() * 2);
while (switchSelect > 0) {
switch (switchSelect){
case 1: random1--;
break;
case 2: random2--;
break;
}
}
}
//sets a grid position to 'off-limits' (i.e. random1 = 1, random2 = 2 --> (1, 2) is 'off-limits')
grid[random1][random2] = true;
//counts the amount of off-limits grid locations generated
mCount++;
}
}
}
public static ArrayList<String> buildPath (int right, int down, ArrayList<Integer> allPaths, int N) {
//Updates path with current location (right, down)
/***** FROM HERE ON IS WHERE I AM HAVING TROUBLE *****/
//Stopping Condition
if ((right == N-1) && (down == N-1)) { //robot cannot go right
allPaths.add(path);
return pathSet;
}
//Recursive Steps
if (right == N-1) { //robot cannot go right
buildPath (right, down + 1, allPaths, N);
} else if (down == N-1) { //robot cannot go down
buildPath (right + 1, down, allPaths, N);
} else { //robot CAN go right or go down
buildPath (right + 1, down, allPaths, N);
//pathSet.add(Integer.toString(right));
//pathSet.add(Integer.toString(down));
buildPath (right, down + 1, allPaths, N);
if (grid[x][y] == false) {
//valid new position (substitute x and y for proposed new path step)
} else if (grid[x][y] == true) {
//off-limits position (substitute x and y for proposed new path step)
}
}
return pathSet;
}
}
import java.util.LinkedHashSet;
class Experimental {
static class PathFinder {
final int gridSize;
final boolean [] [] isBlocked;
final Coord goal;
final LinkedHashSet<Coord> path = new LinkedHashSet<>();
final Random gen = new Random();
PathFinder(int gridSize, int nBlocked) {
this.gridSize = gridSize;
this.isBlocked = new boolean[gridSize][gridSize];
this.goal = new Coord(gridSize - 1, gridSize - 1);
// This gets really inefficient if nBlocked is too big.
for (int n = 0; n < nBlocked; ++n) {
int x, y;
do {
x = gen.nextInt(gridSize);
y = gen.nextInt(gridSize);
} while (isBlocked[x][y] || (x == gridSize - 1 && y == gridSize - 1));
isBlocked[x][y] = true;
}
}
void searchFrom(Coord coord) {
if (path.contains(coord)) return;
path.add(coord);
if (coord.equals(goal)) System.out.println(path);
if (coord.x > 0 && !isBlocked[coord.x - 1][coord.y])
searchFrom(new Coord(coord.x - 1, coord.y));
if (coord.y > 0 && !isBlocked[coord.x][coord.y - 1])
searchFrom(new Coord(coord.x, coord.y - 1));
if (coord.x < gridSize - 1 && !isBlocked[coord.x + 1][coord.y])
searchFrom(new Coord(coord.x + 1, coord.y));
if (coord.y < gridSize - 1 && !isBlocked[coord.x][coord.y + 1])
searchFrom(new Coord(coord.x, coord.y + 1));
path.remove(coord);
}
void printAllPaths() {
searchFrom(new Coord(0, 0));
}
static class Coord {
final int x, y;
public Coord(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Coord) {
Coord other = (Coord) obj;
return x == other.x && y == other.y;
}
return false;
}
@Override
public int hashCode() {
return Integer.hashCode(x) ^ Integer.hashCode(-y);
}
@Override
public String toString() {
return '(' + Integer.toString(x) + ',' + Integer.toString(y) + ')';
}
}
}
public static void main(String[] args) {
new PathFinder(4, new boolean [] [] {
{ false, false, false, false },
{ false, false, true, false },
{ true, false, false, false },
{ false, false, false, false },
}).printAllPaths();
}
}