【问题标题】:Print any exit path, from any arbitrary point to outside grid?打印任何退出路径,从任意点到外部网格?
【发布时间】:2016-11-13 08:13:55
【问题描述】:

这个问题是面试时问的。给定网格 4X4,假设点 (2,2)(从 0 开始的索引),您将给出一些任意点,从那里开始,打印一条任意路径,您可以从那里退出网格。您可以移动LEFTRIGHTTOPBOTTON。在下面的网格中,1 - 代表块,0:你可以移动。

1 0 1 1
0 0 0 0
1 1 0 1
1 1 1 1

上述网格的退出路径是,(2, 2)=> (1, 2) => (1, 1) => (0, 1)

我试图通过 DFS 解决这个问题(下面),但我无法找到如何打印任何一个路径的解决方案,以下是打印所有路径,我只想打印任何路径。

public boolean isValidMove(Point point){
   if(array[point.getX()][point.getY()]==0){
      return true;
   }
   return false;
}

public void printTraversal(int [][] array, int M, int N, Point start){
    boolean [] visited = boolean[M*N];
    Arrays.fill(visited, false);
    int index = start.getX()*M+ start.getY();
    boolean[index] = true;
    Stack<Point> stack = new Stack<Point>();
    stack.push(start);
    while(!stack.isEmpty()){
        Point point = stack.pop();
        System.out.println(point.getX()+", "+ point.getY());
        int x = point.getX();
        int y = point.getY();
        if(isValidMove(x-1, y-1)){
            stack.push(new Point(x-1, y-1));
        }
        if(isValidMove(x-1, y)){
            stack.push(new Point(x-1, y));
        }
        if(isValidMove(x, y-1)){
            stack.push(new Point(x, y-1));
        }
        if(isValidMove(x+1, y+1)){
            stack.push(new Point(x+1, y+1));
        }
    }
}
class Point{
   private int X;
   private int Y;
   public Point(int X, int Y){
      this.X = X;
      this.Y = Y;
   }
   public int getX(){
      return this.X;
   }
   public int getY(){
      return this.Y;
   }

}

【问题讨论】:

  • 请查看我的最新答案。我已经简化了。

标签: java algorithm graph depth-first-search


【解决方案1】:

您需要另一个实用函数来检查当前点是否即将退出。

public boolean isExit(Point point, int M, int N) {
  int x = Point.getX();
  int y = point.getY();

  return (x == 0 || y == 0 || x == M - 1 || y == N - 1);
}

现在在 while 循环中,当您遇到退出点时,打印并退出循环。此外,您还需要更正isValidMoveprintTraversal 函数。

public boolean isValidMove(Point point, int M, int N) {
    int x = Point.getX();
    int y = point.getY();

    if(x >= 0 && y >= 0 && x < M && y < N && array[x][y] == 0 ) {
       return true;
    }
    return false;
}

private int getIndx(Point p, int M) {
    return p.getX() * M + p.getY();
}

public void printTraversal(int [][] array, int M, int N, Point start){
    if(M == 0 || N == 0) {
        return;
    }
    boolean [] visited = boolean[M * N];
    Arrays.fill(visited, false);
    visited[ getIndx(start, M) ] = true;
    Stack<Point> stack = new Stack<Point>();
    stack.push(start);

    while(!stack.isEmpty()) {
        Point point = stack.pop();
        System.out.println(point.getX()+", "+ point.getY());
        if(isExit(point, M, N)) 
            break;
        int x = point.getX();
        int y = point.getY();

        Point neigh = new Point(x-1, y-1); 
        if(isValidMove(x-1, y-1, M, N) && !visited[ getIndx(neigh, M) ]){
            stack.push( neigh );
            visited[ getIndx(neigh, M) ] = true;
        }

        // For other 3 sides
        // .............
    }
}

【讨论】:

  • 不错的答案。顺便说一句,这个问题应该标记为“Java”吗?
  • @AmiTavory 谢谢,我在问题上添加了标签Java
猜你喜欢
  • 2022-01-07
  • 2018-02-21
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 2022-10-16
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
相关资源
最近更新 更多