矩阵遍历从一个点向外扩展?

时间:2017-06-01 23:23:10

标签: java matrix traversal

很抱歉,如果标题不清楚。我不确定如何在没有例子的情况下说出来。因此,目标是在单个点(可以是矩阵中的任何位置)开始时尽可能快地遍历所有方向上的矩阵。我的想法是使用一个二维阵列,从那个单点向外向外扩展几乎像爆炸一样。我在开发这样一个算法时遇到的问题是,当像素向外扩展并探索已经探索过的像素时,它变得非常低效。这是我想做的一个例子。

0,0,0,0,0
0,0,0,0,0
0,0,1,0,0
0,0,0,0,0
0,0,0,0,0

0,0,0,0,0
0,0,1,0,0
0,1,1,1,0
0,0,1,0,0
0,0,0,0,0

0,0,1,0,0
0,1,1,1,0
1,1,1,1,1
0,1,1,1,0
0,0,1,0,0

1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1

感谢您提供给我的任何帮助!

1 个答案:

答案 0 :(得分:0)

请考虑此示例(注释中的说明):

import java.util.ArrayList;

public class MatrixTraversal {

  public static void main(String[] main){
      // 5x5 matrix example 
      Byte[][] matrix = {{0,0,0,0,0},{0,0,0,0,0},
                         {0,0,0,0,0},{0,0,0,0,0}, 
                         {0,0,0,0,0}};

      // testing
      expand(matrix, new Point(0,0));
      print(matrix);

      expand(matrix, new Point(2,4));
      print(matrix);

      expand(matrix, new Point(4,1));
      print(matrix);

      expand(matrix, new Point(3,1));
      print(matrix);

      expand(matrix, new Point(2,2));
      print(matrix);

  }


  ////////////////////////////////////////////////////////


  /**
   * This method to expand a given point in the matrix
   * @param matrix
   * @param point
   */
  public static void expand(Byte[][]matrix, Point point){
      // first change the given point to 1
      matrix[point.x][point.y]=1;

      // then get the neighbors points of the given point 
      ArrayList<Point> neighbors = neighbors(matrix, point); 
      // loop through the neighbors ArrayList and convert 
      // the corresponding points to 1's
      for(Point p : neighbors){
          matrix[p.x][p.y]=1;
      }
  }


  ////////////////////////////////////////////////////////


  /**
   * This method to return the VALID neighbors points of 
   * a given point in the matrix
   * @param matrix
   * @param point
   */
  public static ArrayList<Point> neighbors(Byte[][]matrix, Point point){
      int column = point.y;  int row = point.x; 

      ArrayList<Point> neighbors = new ArrayList<Point>();
      if(column>0){
          if(matrix[row][column-1]!=1){
              neighbors.add(new Point(row, column-1));
          }
      }

      if(column<matrix[0].length-1){
          if(matrix[row][column+1]!=1){
              neighbors.add(new Point(row, column+1));
          }
      }

      if(row>0){
          if(matrix[row-1][column]!=1){
              neighbors.add(new Point(row-1, column));
          }
      }

      if(row<matrix.length-1){
          if(matrix[row+1][column]!=1){
              neighbors.add(new Point(row+1, column));
          }
      }
      return neighbors;
  }



  ////////////////////////////////////////////////////////


  /**
   * This method just to print the result
   * @param matrix
   */
  public static void print(Byte[][] matrix){
      for(int i=0; i< matrix.length; i++, System.out.println()){
          for(int j=0; j< matrix[0].length; j++){
              System.out.print(matrix[i][j] + " ");
          }
      }
      System.out.println("---------");
  }


  ////////////////////////////////////////////////////////


  //Inner Class to represent a Point(x,y)
  public static class Point{
      int x,y;

      public Point(int x, int y){
          this.x = x;
          this.y = y;
      } 
  } 
}

<强>测试

1 1 0 0 0 
1 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
---------
1 1 0 0 0 
1 0 0 0 1 
0 0 0 1 1 
0 0 0 0 1 
0 0 0 0 0 
---------
1 1 0 0 0 
1 0 0 0 1 
0 0 0 1 1 
0 1 0 0 1 
1 1 1 0 0 
---------
1 1 0 0 0 
1 0 0 0 1 
0 1 0 1 1 
1 1 1 0 1 
1 1 1 0 0 
---------
1 1 0 0 0 
1 0 1 0 1 
0 1 1 1 1 
1 1 1 0 1 
1 1 1 0 0