vinkeychen

回旋矩阵

这是深圳某处的一个面试题,要求打印出如下格式:
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 = { { 01 }, { 10 }, { 0-1 }, { -10 } };  
    
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:
转化为二维数组初始化问题
class snakePrint {  
    
static int length = 7;  
    
static int value = 1;  
    
static int[][] snake = new int[length][length];  
    
static Direction lastDirection = Direction.Right;  
  
    
static enum Direction {  
        Right, Down, Left, Up;  
    }  
  
    
public static void initialArray() {  
        
int row = 0, line = 0;  
        
for (int c = 0; c < length * length; c++) {  
            snake[row][line] 
= value;  
            lastDirection 
= findDirection(row, line);  
            
switch (lastDirection) {  
                
case Right:  
                    line
++;  
                    
break;  
                
case Down:  
                    row
++;  
                    
break;  
                
case Left:  
                    line
--;  
                    
break;  
                
case Up:  
                    row
--;  
                    
break;  
                
default:  
                    System.out.println(
"error");  
            }  
            value
++;  
        }  
    }  
  
    
static Direction findDirection(int row, int line) {  
        Direction direction 
= lastDirection;  
        
switch (direction) {  
            
case Right: {  
                
if ((line == length - 1|| (snake[row][line + 1!= 0))  
                    direction 
= direction.Down;  
                
break;  
            }  
            
case Down: {  
                
if ((row == length - 1|| (snake[row + 1][line] != 0))  
                    direction 
= direction.Left;  
                
break;  
            }  
            
case Left: {  
                
if ((line == 0|| (snake[row][line - 1!= 0))  
                    direction 
= direction.Up;  
                
break;  
            }  
            
case Up: {  
                
if (snake[row - 1][line] != 0)  
                    direction 
= direction.Right;  
                
break;  
            }  
        }  
        
return direction;  
    }  
  
    
public static void main(String[] args) {  
        initialArray();  
  
        
// display..  
        for (int i = 0; i < length; i++) {  
            
for (int j = 0; j < length; j++) {  
                System.out.print(snake[i][j] 
+ "  ");  
            }  
            System.out.println();  
        }  
    }  
}  


posted on 2010-07-25 13:55 Mr.chenz 阅读(57) 评论(0)  编辑  收藏 所属分类: java基础练习


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

统计

留言簿

文章分类

文章档案

搜索

最新评论