打印蛇形排列的数(这算法挺牛的)

本文介绍了一种蛇形填充算法的实现方式,该算法能够在一个二维数组中按蛇形路径填充数字。通过定义方向枚举和查找下一个方向的逻辑,实现了路径的自动转向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. class  snakePrint {  
  2.     static   int  length =  7 ;  
  3.     static   int  value =  1 ;  
  4.     static   int [][] snake =  new   int [length][length];  
  5.     static  Direction lastDirection = Direction.Right;  
  6.   
  7.     static   enum  Direction {  
  8.         Right, Down, Left, Up;  
  9.     }  
  10.   
  11.     public   static   void  initialArray() {  
  12.         int  row =  0 , line =  0 ;  
  13.         for  ( int  c =  0 ; c < length * length; c++) {  
  14.             snake[row][line] = value;  
  15.             lastDirection = findDirection(row, line);  
  16.             switch  (lastDirection) {  
  17.                 case  Right:  
  18.                     line++;  
  19.                     break ;  
  20.                 case  Down:  
  21.                     row++;  
  22.                     break ;  
  23.                 case  Left:  
  24.                     line--;  
  25.                     break ;  
  26.                 case  Up:  
  27.                     row--;  
  28.                     break ;  
  29.                 default :  
  30.                     System.out.println("error" );  
  31.             }  
  32.             value++;  
  33.         }  
  34.     }  
  35.   
  36.     static  Direction findDirection( int  row,  int  line) {  
  37.         Direction direction = lastDirection;  
  38.         switch  (direction) {  
  39.             case  Right: {  
  40.                 if  ((line == length -  1 ) || (snake[row][line +  1 ] !=  0 ))  
  41.                     direction = direction.Down;  
  42.                 break ;  
  43.             }  
  44.             case  Down: {  
  45.                 if  ((row == length -  1 ) || (snake[row +  1 ][line] !=  0 ))  
  46.                     direction = direction.Left;  
  47.                 break ;  
  48.             }  
  49.             case  Left: {  
  50.                 if  ((line ==  0 ) || (snake[row][line -  1 ] !=  0 ))  
  51.                     direction = direction.Up;  
  52.                 break ;  
  53.             }  
  54.             case  Up: {  
  55.                 if  (snake[row -  1 ][line] !=  0 )  
  56.                     direction = direction.Right;  
  57.                 break ;  
  58.             }  
  59.         }  
  60.         return  direction;  
  61.     }  
  62.   
  63.     public   static   void  main(String[] args) {  
  64.         initialArray();  
  65.   
  66.         // display.....   
  67.         for  ( int  i =  0 ; i < length; i++) {  
  68.             for  ( int  j =  0 ; j < length; j++) {  
  69.                 System.out.print(snake[i][j] + "  " );  
  70.             }  
  71.             System.out.println();  
  72.         }  
  73.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值