蛇形矩阵     面试算法(1)

   前段时间微信订阅号收到一个关于算法的面试题,是让实现一个N阶的蛇形矩阵。

题中所给图为如下的正方形。


wKiom1NJoBmDv6hmAAAjFGl-Xyw687.jpg

   分析:观察可知图中数据排列规律。

         1. 数据从最右边一列开始,由上至下至最后一行;

         2. 由最下一行自右向左,至最左端;

         3. 与 1 顺序相反,由下至上至第一行;

         4. 与 2 顺序相反,由左至右至倒数第二列;

         5. 依次执行1,2,3,4 至 矩阵填满,或当前数据至达到N*N。

       注:图中数据只可被赋值依次,且沿顺时针方向变化。


/**
 * project  Algorithm
 * package  finish
 * fileInfo SnakeMartix.java  2014-3-29 下午1:21:48
 * author   WANGXIN
 * aim      TODO
 */
package finish;
import java.util.Scanner;
/**
 * @author WANGXIN by 2014-3-29下午1:21:48
 * @version v1.0
 */
public class SnakeMartix {
    public static void main(String[] args) {
        int M;
        int N;
        System.out
                .println("Please input the Snake-Matrix`s rows and columns: ");
        System.out.print("rows = ");
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        M = scanner.nextInt();
        System.out.print("columns = ");
        N = scanner.nextInt();
        System.out.println("Snake-Matrix(" + M + " , " + N + ")");
        printSnake(M, N);
    }
    /**
     * display the Snake-Matrix
     *
     * @param M
     *            the row of the Matrix
     * @param N
     *            the columns of the Matrix
     */
    public static void printSnake(int M, int N) {
        int num = 0;
        int i = 0;
        int j = 0;
        int x = 0;
        int y = 0;
        int[][] arr = new int[M][N];
        char[][] str = new char[M][N];
        int min = M > N ? N : M;
        min = (int)Math.round(min / 2.0 + 0.5);
        while (true) {
            for (i = x; i <= M - 1 - x; i++) {
                if (num == M * N)
                    break ;
                arr[i][N - 1 - y] = ++num;
                str[i][N - 1 - y] = '↓';
            }
            y++;// decrease y line(s)
            for (j = N - 1 - y; j >= y - 1; j--) {
                if (num == M * N)
                    break ;
                arr[M - y][j] = ++num;
                str[M - y][j] = '←';
            }
            x++;// decrease x line(s)
            for (i = M - 1 - x; i >= x - 1; i--) {
                if (num == M * N)
                    break ;
                arr[i][y - 1] = ++num;
                str[i][y - 1] = '↑';
            }
            for (j = y; j < N - y; j++) {
                if (num == M * N)
                    break ;
                arr[y - 1][j] = ++num;
                str[y - 1][j] = '→';
            }
            if (y == min || x == min) {
//              System.out.println("x = " + x  + " y = "+ y);//label is not the most efficient
                break;
            }
        }
        System.out.println();
        for (int k = 0; k < M; k++) {
            for (int k2 = 0; k2 < N; k2++) {
                System.out.print(arr[k][k2] + "\t");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println("-----------direction show as follow------------");
        for (int k = 0; k < M; k++) {
            for (int k2 = 0; k2 < N; k2++) {
                System.out.print(str[k][k2] + "\t");
            }
            System.out.println();
        }
    }
}


你可能感兴趣的:(java,算法,蛇形矩阵)