Algorithms: Z-shaped (Zigzag) layout

  Problem: Given a two-dimensional matrix array of n rows and m columns. As shown, prints the ZIG-ZAG this matrix.

  From the point of view of symmetry, may be shaped as a line segment generated from a simple rule of the pattern reflected by repeatedly applying coasting.

  The main idea: Algorithm from (0, 0) traversed horizontally to the right starting position, upon reaching (0, 1) when the lower left traversal (using one control variable to the lower left upper right) in the opposite diagonal direction, the inner loop has been traversed to the touch to the edge of the row ++, to the upper right direction, col ++, traversal direction to the lower left edge when confronted in the reverse diagonal matrix, known upper half (including anti-diagonal traversed); been traversed half the matrix (which may be sub square matrix) after the position index of the current row and col extending direction where the index is determined, if the hit the edge, corresponding to the ranks of the index number plus one, did not encounter the edge of the inner loop of the press extending the idea, then the same The remaining part of the matrix thinking been traversed.

  Time complexity of the algorithm is O (n * m), wherein n, m is the number of rows and columns of the input matrix, the spatial complexity is O (1).

  . 1  Package algorithm;
   2  
  . 3  public  class ZArrangement {
   . 4      
  . 5        . 6   . 7 / **   . 8      * the Z-shaped arrangement of the print matrix
   . 9      *
 10      * @param ARR matrix
 . 11      * @param n-specified submatrix row
 12 is      * @param m are designated column of the matrix
 13 is * / 14 Private static void zigzagMatrix ( int [] [] ARR, int n-, int m) {
 15 int row = 0, COL = 0  
; 16 . 17 / * row growth variables and control ↙ ↗ Boolean variables * / 18 is Boolean row_inc = to false ; . 19 20 is // print half (including anti-diagonal) Z-zag pattern matrix 21 is int Mn = Math.min (m, n-); 22 is for ( int len =. 1; len <= Mn; ++ len) { 23 is for ( int I = 0; I <len; ++ I) { 24 of System.out.print (ARR [ Row] [COL] + "" ); 25 26 is IF (. 1 + I == len) { 27 BREAK ; 28 } 29 30 // If row_increment true, the increase in the row and column decrease, incrementing or decrementing the row and column 31 is IF (row_inc) { 32 // 33 is ++ Row; 34 is - COL; 35 } the else { 36 // 37 [ - Row; 38 is ++ COL; 39 } 40 } 41 is 42 is IF (len == Mn) { 43 is BREAK; 44 } 45 46 is // The last value of a row or column on incremental update 47 IF (row_inc) { 48 ++ Row; 49 row_inc = to false ; 50 } the else { 51 is ++ COL; 52 is row_inc = to true ; 53 is } 54 } 55 56 is 57 is // updated index variable row and col 58 IF (row == 0 ) { 59 IF(== m-COL. 1 ) 60 ++ Row; 61 is the else 62 is ++ COL; 63 is row_inc = to true ; 64 } the else { 65 IF (n-Row-==. 1 ) 66 ++ COL; 67 the else 68 ++ Row ; 69 row_inc = to false ; 70 } 71 is 72 // print the rest of the matrix Z-shaped pattern 73 is int MAX = Math.max(m, n) - 1; 74 for (int len, diag = MAX; diag > 0; --diag) { 75 if (diag > mn) 76 len = mn; 77 else 78 len = diag; 79 80 for (int i = 0; i < len; ++i) { 81 System.out.print(arr[row][col] + " "); 82 83 if (i + 1 == len) 84 break; 85 86 //The incremental value of the last update of a row or column 87 IF (row_inc) { 88 ++ ; Row 89 - ; COL 90 } the else { 91 is ++ ; COL 92 - ; Row 93 } 94 } 95 96 // Update col and row index variable 97 IF (== m-row col. 1 == 0 || ) { 98 IF (m ==-col. 1 ) 99 ++ row; 100 else 101 ++col; 102 row_inc = true; 103 } 104 105 else if (col == 0 || row == n-1) { 106 if (row == n-1) 107 ++col; 108 else 109 ++row; 110 row_inc = false; 111 } 112 } 113 } 114 115 public static void main(String[] args) { 116 int[][] matrix = { 117 {1, 2, 3}, 118 {4, 5, 6}, 119 {7, 8, 9} 120 }; 121 zigzagMatrix(matrix, 3, 3); 122 } 123 }

Guess you like

Origin www.cnblogs.com/magic-sea/p/12019945.html