回型打印

  1. /*1    2  3  4  5 
  2. 14  15  16  17  6 
  3. 13  20  19  18  7 
  4. 12  11  10  9  8 
  5. 打印如上矩阵*/
  6. public class MainApp {
  7.     /**
  8.      * @param args
  9.      */
  10.     public static void main(String[] args) {
  11.         // TODO Auto-generated method stub
  12.         Cube text = new Cube(5,5);                     //这里的参数分别表示要打印的行数和列数
  13.         System.out.print(text);
  14.     }
  15. }

  1. import java.util.Arrays;
  2. /*这是实现类,构造方法的参数为一个row和一个coloum,形成一个矩阵*/
  3. public class Cube {
  4.     
  5.     int[] cube;                              //用于存放数据
  6.     int row;                                 //行数
  7.     int coloum;                              //列数
  8.     
  9.     public Cube(int row,int coloum){         //构造函数
  10.         /*初始化*/
  11.         this.cube = new int[row*coloum];
  12.         Arrays.fill(this.cube,0);
  13.         this.row  = row;
  14.         this.coloum = coloum;
  15.         this.initialarry();
  16.     }
  17.     
  18.     //形成队列  
  19.     private void initialarry() {
  20.         int step = 1;              //下一步要走的值
  21.         int id =0;                 //id号
  22.         int next;                  //存forward函数;
  23.         for(int i=1;i<=cube.length;i++){
  24.             cube[id] = i;
  25.             next = this.forward(id+1);
  26.             if(next!=0){           //如果不等于0
  27.                 step=next;
  28.             }
  29.             id+=step;              //确定下一步要要往哪里走
  30.         }
  31.     }
  32.     @Override
  33.     public String toString() {
  34.        String output="";                    //输出
  35.        for(int i = 0;i<this.cube.length;i++){
  36.            /*这是为了打印好看*/
  37.            output+=cube[i]<10?"  "+String.valueOf(cube[i]):" "+String.valueOf(cube[i]);
  38.            if((i+1)%this.coloum==0){
  39.                output+="/n";
  40.            }
  41.        }
  42.        return output;
  43.     }
  44.     private int forward(int id){              //用于判断一个格子附近的4个格子是否为空
  45.         int       count=0;                         //计数器
  46.         boolean[] forward = new boolean[4];        //表示方向    
  47.         Arrays.fill(forward,false);                //初始化
  48.         
  49.         /*判断上面的格子*/
  50.         if((id-this.coloum)>0                      //不是最上面一行
  51.             &&cube[id-this.coloum-1]==0){            //还没有赋值
  52.             count++;
  53.             forward[0] = true;                     //0表示向上
  54.         }
  55.         
  56.         /*判断左面的格子*/
  57.         if((id-1)>0                                //序列号正常
  58.            &&((id-1)%this.coloum)!=0               //不是最左面一列
  59.            &&cube[id-2]==0){
  60.             count++;
  61.             forward[1] = true;                     //1表示向左
  62.         }
  63.         
  64.         /*判断右的格子*/
  65.         if((id+1)<=this.cube.length                 //序列号正常
  66.            &&(id%this.coloum)!=0                    //不是最右面一列
  67.            &&cube[id]==0){
  68.             count++;
  69.             forward[2] = true;                     //2表示向右
  70.         }
  71.         
  72.         /*判断下面的格子*/
  73.         if((id+this.coloum)<=this.cube.length       //序列号正常
  74.            &&cube[id+this.coloum-1]==0){
  75.             count++;
  76.             forward[3] = true;                     //1表示向左
  77.         }
  78.         
  79.         /*决定返回值*/
  80.         if(count!=1){                              //根据观察,如果朋友不为一个的话,是不会转向的。
  81.             return 0;   
  82.          /*如果为一时,那里那里为空,网哪里走*/
  83.         }else if(forward[0]){                      //上面为空,往上走   
  84.             return -this.coloum;
  85.         }else if(forward[1]){                      //左面为空,往左走
  86.             return -1;
  87.         }else if (forward[2]){                      //右面为空,往右走
  88.             return 1
  89.         }else if(forward[3]){                       //下面为空,往下走
  90.             return this.coloum;
  91.         }
  92.         return 0;
  93.     }
  94.     
  95. }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值