Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 391|回复: 0

[枚举学习]枚举学习一例:写螺旋矩阵

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-28 23:55:54 | 显示全部楼层 |阅读模式
    输入一个矩阵的行列数量,生成一个螺旋矩阵,比如输入3,5,则打印:
    1    2   3  4  5
    12 13 14 15 6
    11 10   9  8 7
    网上的代码,蚂蚁爬矩阵,学习了。
    1. import java.util.*;
    2. public class AntGame {  
    3.    
    4.      public static void main(String[] args) {  
    5.          int rowSize = 5;  
    6.          int colSize = 3;  
    7.          AntMap antMap = new AntMap(rowSize, colSize);  
    8.          Ant ant = new Ant(antMap);  
    9.          Position position = new Position(0,0);  
    10.          int count = 1;  
    11.          while (count <= colSize * rowSize) {  
    12.              ant.crawl(position, count++);  
    13.          }  
    14.          antMap.print();  
    15.      }  
    16. }  
    17.    
    18. class AntMap {  
    19.    
    20.      int[][] matrix;  
    21.      int rowSize;  
    22.      int colSize;  
    23.       
    24.      AntMap(int rowSize, int colsize) {  
    25.          this.rowSize = rowSize;  
    26.          this.colSize = colsize;  
    27.          matrix = new int[rowSize][colsize];  
    28.      }  
    29.    
    30.      void print() {  
    31.          for (int i = 0; i < rowSize; ++i) {  
    32.              for (int j = 0; j < colSize; ++j)   
    33.                  System.out.format("%3d", matrix[i][j]);  
    34.              System.out.println();  
    35.          }  
    36.      }  
    37.      public boolean isFilled(Position position) {  
    38.          return matrix[position.row][position.col] != 0;  
    39.      }  
    40. }  
    41.    
    42. enum Action {  //精华在这里了。
    43.       
    44.      FORWARD {  
    45.          public void act(Position position) {  
    46.              position.col++;  
    47.          }  
    48.    
    49.          public boolean shouldTurn(Position position, AntMap antMap) {  
    50.              return position.col == antMap.colSize-1  
    51.                  || antMap.isFilled(position.nextCol());  
    52.          }  
    53.      },   
    54.      DOWN {  
    55.          public void act(Position position) {  
    56.              position.row++;  
    57.          }  
    58.            
    59.          public boolean shouldTurn(Position position, AntMap antMap) {  
    60.              return position.row == antMap.rowSize-1  
    61.                  || antMap.isFilled(position.nextRow());  
    62.          }  
    63.      },  
    64.      BACKWARD {  
    65.          public void act(Position position) {  
    66.              position.col--;  
    67.          }  
    68.    
    69.          public boolean shouldTurn(Position position, AntMap antMap) {  
    70.              return position.col == 0   
    71.                  || antMap.isFilled(position.previousCol());  
    72.          }  
    73.      },   
    74.      UP {  
    75.          public void act(Position position) {  
    76.              position.row--;  
    77.          }  
    78.    
    79.          public boolean shouldTurn(Position position, AntMap antMap) {  
    80.              return position.row == 0   
    81.                  || antMap.isFilled(position.previousRow());  
    82.          }  
    83.      };  
    84.      public abstract void act(Position position);  
    85.      public abstract boolean shouldTurn(Position position, AntMap antMap);  
    86.       
    87.      private static List< Action> list = new ArrayList< Action>();  
    88.       
    89.      public static List< Action> actions() {  
    90.          if (!list.isEmpty()) return list;  
    91.          list.add(FORWARD);  
    92.          list.add(DOWN);  
    93.          list.add(BACKWARD);  
    94.          list.add(UP);  
    95.          return list;  
    96.      }  
    97. }  
    98.    
    99. class Position {  
    100.       
    101.      int row;   
    102.      int col;   
    103.       
    104.      public Position(int row, int col) {  
    105.          this.row = row;  
    106.          this.col = col;  
    107.      }  
    108.       
    109.      public Position previousCol() {  
    110.          return new Position(row, col-1);  
    111.      }  
    112.      public Position nextCol() {  
    113.          return new Position(row, col+1);  
    114.      }  
    115.      public Position previousRow() {  
    116.          return new Position(row-1, col);  
    117.      }  
    118.      public Position nextRow() {  
    119.          return new Position(row+1, col);  
    120.      }  
    121. }  
    122.    
    123. class Ant {  
    124.       
    125.      AntMap antMap = null;  
    126.      Action curentAction = Action.FORWARD;  
    127.       
    128.      Ant(AntMap antMap) {  
    129.          this.antMap = antMap;  
    130.      }  
    131.      void crawl(Position position, int count) {  
    132.          antMap.matrix[position.row][position.col] = count;  
    133.          if (curentAction.shouldTurn(position, antMap))  
    134.              turn();  
    135.          curentAction.act(position);  
    136.      }  
    137.    
    138.      private void turn() {  
    139.          List< Action> actions = Action.actions();  
    140.          curentAction = actions.get((actions.indexOf(curentAction)+1)%4);  
    141.      }  
    142.   }     
    复制代码
    运行结果:

    C:java>java AntGame
    1   2   3
    12 13  4
    11 14  5
    10 15  6
    9   8   7

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/28/235554031.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 04:33 , Processed in 0.292639 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表