回旋矩阵

这是深圳某处的一个面试题,要求打印出如下格式:

int i=5;  
1   2   3   4  5  
16 17 18 19 6  
15 24 25 20 7  
14 23 22 21 8  
13 12 11 10 9  
  
int i=6  
1    2  3   4   5  6  
20 21 22 23 24  7  
19 32 33 34 25  8  
18 31 36 35 26  9  
17 30 29 28 27 10  
16 15 14 13 12 11  


解决方案1:

public class SnakePrint {  
  
    private int orient = 0, length = 0, x = 0, y = 0;  
    // orients为顺时针90°旋转方向,前进步长为1  
    private int[][] orients = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };  
    private int[][] arrays;  
  
    public SnakePrint(int length) {  
        this.length = length;  
        arrays = new int[length][length];  
    }  
  
    // 根据当前方向返回下一个前进方向  
    private int[] nextOrient(int[] curOrient) {  
        int nextX = x + curOrient[0], nextY = y + curOrient[1];  
  
        // 前进方向需要顺时针旋转90°了  
        if (nextX < 0 || nextX >= length || nextY < 0 || nextY >= length  
                || arrays[nextX][nextY] != 0) {  
            orient = ++orient % 4;  
            return orients[orient];  
        }  
  
        return curOrient; // 不需要掉头,返回原前进方向  
    }  
  
    public void print() {  
        int[] curOrient = orients[orient]; // 初始前进方向  
        for (int i = 1; i <= length * length; i++) { // 依次填充数组  
            arrays[x][y] = i;  
            curOrient = nextOrient(curOrient);  
            x += curOrient[0];  
            y += curOrient[1];  
        }  
  
        for (int i = 0; i < length; i++) {  
            for (int j = 0; j < length; j++) {  
                System.out.printf("%4d", arrays[i][j]); // 按固定4个字符宽度的格式输出  
            }  
            System.out.println();  
        }  
    }  
  
    public static void main(String[] args) {  
        SnakePrint snakePrint = new SnakePrint(6);  
        snakePrint.print();  
    }  
}  

解决方案2:

public class SnakePrint {  
  
    private int orient = 0, length = 0, x = 0, y = 0;  
    // orients为顺时针90°旋转方向,前进步长为1  
    private int[][] orients = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };  
    private int[][] arrays;  
  
    public SnakePrint(int length) {  
        this.length = length;  
        arrays = new int[length][length];  
    }  
  
    // 根据当前方向返回下一个前进方向  
    private int[] nextOrient(int[] curOrient) {  
        int nextX = x + curOrient[0], nextY = y + curOrient[1];  
  
        // 前进方向需要顺时针旋转90°了  
        if (nextX < 0 || nextX >= length || nextY < 0 || nextY >= length  
                || arrays[nextX][nextY] != 0) {  
            orient = ++orient % 4;  
            return orients[orient];  
        }  
  
        return curOrient; // 不需要掉头,返回原前进方向  
    }  
  
    public void print() {  
        int[] curOrient = orients[orient]; // 初始前进方向  
        for (int i = 1; i <= length * length; i++) { // 依次填充数组  
            arrays[x][y] = i;  
            curOrient = nextOrient(curOrient);  
            x += curOrient[0];  
            y += curOrient[1];  
        }  
  
        for (int i = 0; i < length; i++) {  
            for (int j = 0; j < length; j++) {  
                System.out.printf("%4d", arrays[i][j]); // 按固定4个字符宽度的格式输出  
            }  
            System.out.println();  
        }  
    }  
  
    public static void main(String[] args) {  
        SnakePrint snakePrint = new SnakePrint(6);  
        snakePrint.print();  
    }  
} 

以上代码来自:http://www.javaeye.com/topic/545378

 

 

    posted @ 2010-07-28 22:44  Mr.chenz  阅读(529)  评论(0编辑  收藏  举报