Question (in Chinese): http://ac.jobdu.com/problem.php?pid=1391
Question Name: Print Matrix
Question Description: Give an integer matrix, print it in spiral form.
Input: the input might contain multiple test cases. Inside each test case, the first line includes two intergers N and M (0 <= N, M <= 1000), saying the number of rows and columns respectively. The following N lines contain the values of the matrix.
Output: print the matrix in spiral form. Every number is followed by a space.
1 2 3 4 5 6 7 8 | Input: 4 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 |
The solution is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | /*---------------------------------------------------------------- * Author: Sheng Yu codesays.com * Written: 08/10/2014 * Last updated: 08/10/2014 * * Filename: PrintMatrix.java * Compilation: javac PrintMatrix.java * Execution: java PrintMatrix * JDK Used: 1.7 *----------------------------------------------------------------*/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; /** * The utility to print a matrix in spiral form, in clockwise. * @author Sheng Yu codesays.com */ public class PrintMatrix { /** * By default, we show the content of matrix, * not return the representing string. * */ private boolean showDefault = true; /** * Print the matrix in spiral form. * @param matrix the matrix to print. */ public final void printMatrix(final int[][] matrix) { printMatrix(matrix, showDefault); return; } /** * Generate the content of matrix in spiral form. May print the result, or * return it. * @param matrix the matrix to print. * @param show should the function print the result or return it? * @return if show is false, return the representing string. * Otherwise, null. */ public final String printMatrix(final int[][] matrix, final boolean show) { // Check the input: not an array or empty matrix. if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { if (show) { return null; } else { return ""; } } StringBuilder sb = new StringBuilder(); int rows = matrix.length; int columns = matrix[0].length; int layerCount = Math.min(rows, columns) / 2; for (int layerIndex = 0; layerIndex < layerCount; ++layerIndex) { // Access the upper line (row). for (int cIndex = layerIndex; cIndex < columns - 1 - layerIndex; ++cIndex) { sb.append(matrix[layerIndex][cIndex]); sb.append(" "); } // Access the right line (column). for (int rIndex = layerIndex; rIndex < rows - 1 - layerIndex; ++rIndex) { sb.append(matrix[rIndex][columns - 1 - layerIndex]); sb.append(" "); } // Access the lower line (row). for (int cIndex = columns - 1 - layerIndex; cIndex > layerIndex; --cIndex) { sb.append(matrix[rows - 1 - layerIndex][cIndex]); sb.append(" "); } // Access the left line (column). for (int rIndex = rows - 1 - layerIndex; rIndex > layerIndex; --rIndex) { sb.append(matrix[rIndex][layerIndex]); sb.append(" "); } } if ((rows & 1) == 1 && layerCount == rows / 2) { // There is a row, waiting for accessing. for (int cIndex = layerCount; cIndex < columns - layerCount; ++cIndex) { sb.append(matrix[layerCount][cIndex]); sb.append(" "); } } else if ((columns & 1) == 1 && layerCount == columns / 2) { // There is a column, waiting for accessing. for (int rIndex = layerCount; rIndex < rows - layerCount; ++rIndex) { sb.append(matrix[rIndex][layerCount]); sb.append(" "); } } if (show) { System.out.println(sb); return null; } else { return sb.toString(); } } /** * Stub for test. * @param args the command line arguments. * @throws IOException when input gets error. */ public static void main(final String[] args) throws IOException { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); int rows = 0; int columns = 0; int[][] matrix = null; PrintMatrix printer = new PrintMatrix(); while (st.nextToken() != StreamTokenizer.TT_EOF) { // Read the dimension of the matrix. rows = (int) st.nval; st.nextToken(); columns = (int) st.nval; // Read the matrix. matrix = new int[rows][columns]; for (int rIndex = 0; rIndex < rows; ++rIndex) { for (int cIndex = 0; cIndex < columns; ++cIndex) { st.nextToken(); matrix[rIndex][cIndex] = (int) st.nval; } } // Print the matrix printer.printMatrix(matrix); } } } |