绘制数字缠绕矩阵

来源:互联网 发布:如何面试淘宝美工 编辑:程序博客网 时间:2024/04/24 18:48
        什么是数字缠绕矩阵呢? 呵呵,这个名字是我起的,好玩而已,举个例子大家就明白了:

01 02 03 04 05
16 17 18 19 06
15 24 25 20 07
14 23 22 21 08
13 12 11 10 09

01 20 19 18 17 16
02 21 32 31 30 15
03 22 33 36 29 14
04 23 34 35 28 13
05 24 25 26 27 12
06 07 08 09 10 11

     像这样的矩阵,由阿拉伯数组从外向内缠绕递增生成,就叫做数字缠绕矩阵了。当然,缠绕的方向分为顺时针和逆时针,第一个例子是顺时针,第二个例子是逆时针。
      这只是一个比较有趣的小程序,闲来无事写一个玩玩,代码如下:
public class Main {

    
//测试函数
    public static void main(String[] args) {
        NumMatrix nm 
= new NumMatrix();
        nm.run(
5, NumMatrix.DEASIL);
        System.out.println();
        nm.run(
6, NumMatrix.ANTICLOCKWISE);

    }



}


class NumMatrix {

    
public static final int DEASIL = 1;    //顺时针
    public static final int ANTICLOCKWISE = 2;    //逆时针

    
/**
     * 绘制出一个数字串缠绕矩阵
     * 
@param n    矩阵的尺寸
     * 
@param circle    数字串的旋转方向:顺时针或逆时针
     
*/

    
public void run(int n, int circle) {
        
int[][] a = new int[n][n];
        setNum(a);
        setCircle(circle);
        
while(true{
            
if(move(n, a)) {
                setNum(a);
                turnCount 
= 0;
            }
else {
                turn();
                
if(++turnCount == 2)
                    
break;
            }

        }

        
        
for(int i=0; i<n; i++{
            
for(int j=0; j<n; j++{
                System.out.print(String.format(
"%1$02d ", a[i][j]));
            }

            System.out.println();
        }

        
        reset();

    }

    
    
//重置各项参数,为下一次绘制做准备
    private void reset() {
        x 
= 0;
        y 
= 0;
        num 
= 1;
        arrow 
= RIGHT;
        turnCount 
= 0;
    }

    
    
//移动一格,若成功移动返回true,若撞墙则返回false
    private boolean move(int n, int[][] a) {
        
switch(arrow) {
        
case RIGHT:
            
if(y+1==|| a[x][y+1]>0)
                
return false;
            y
++;
            
break;
        
case DOWN:
            
if(x+1==|| a[x+1][y]>0)
                
return false;
            x
++;
            
break;
        
case LEFT:
            
if(y==0 || a[x][y-1]>0)
                
return false;
            y
--;
            
break;
        
case UP:
            
if(x==0 || a[x-1][y]>0)
                
return false;
            x
--;
            
break;
        }

        
return true;
    }

    
    
//将数字填入当前空白格子
    private void setNum(int[][] a) {
        a[x][y] 
= num++;
    }

    
    
//根据旋转方向为顺时针和逆时针,进行拐弯
    private void turn() {
        
switch(circle) {
        
case DEASIL:
            arrow 
= (arrow+1% 4;
            
break;
        
case ANTICLOCKWISE:
            arrow 
= (arrow+3% 4;
        }

        
    }

    
    
//设置旋转方向
    private void setCircle(int circle) {
        
this.circle = circle;
        
switch(circle) {
        
case DEASIL:
            arrow 
= RIGHT;
            
break;
        
case ANTICLOCKWISE:
            arrow 
= DOWN;
            
break;
        }

    }

    
    
private int turnCount = 0;
    
private int x = 0;
    
private int y = 0;
    
    
private int num = 1;
    
    
private final static int RIGHT = 0;
    
private final static int DOWN = 1;
    
private final static int LEFT = 2;
    
private final static int UP = 3;
    
    
private int circle;
    
private int arrow;
}

原创粉丝点击