TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
输入一个矩阵的行列数量,生成一个螺旋矩阵,比如输入3,5,则打印:
1 2 3 4 5
12 13 14 15 6
11 10 9 8 7
网上的代码,蚂蚁爬矩阵,学习了。
- import java.util.*;
- public class AntGame {
-
- public static void main(String[] args) {
- int rowSize = 5;
- int colSize = 3;
- AntMap antMap = new AntMap(rowSize, colSize);
- Ant ant = new Ant(antMap);
- Position position = new Position(0,0);
- int count = 1;
- while (count <= colSize * rowSize) {
- ant.crawl(position, count++);
- }
- antMap.print();
- }
- }
-
- class AntMap {
-
- int[][] matrix;
- int rowSize;
- int colSize;
-
- AntMap(int rowSize, int colsize) {
- this.rowSize = rowSize;
- this.colSize = colsize;
- matrix = new int[rowSize][colsize];
- }
-
- void print() {
- for (int i = 0; i < rowSize; ++i) {
- for (int j = 0; j < colSize; ++j)
- System.out.format("%3d", matrix[i][j]);
- System.out.println();
- }
- }
- public boolean isFilled(Position position) {
- return matrix[position.row][position.col] != 0;
- }
- }
-
- enum Action { //精华在这里了。
-
- FORWARD {
- public void act(Position position) {
- position.col++;
- }
-
- public boolean shouldTurn(Position position, AntMap antMap) {
- return position.col == antMap.colSize-1
- || antMap.isFilled(position.nextCol());
- }
- },
- DOWN {
- public void act(Position position) {
- position.row++;
- }
-
- public boolean shouldTurn(Position position, AntMap antMap) {
- return position.row == antMap.rowSize-1
- || antMap.isFilled(position.nextRow());
- }
- },
- BACKWARD {
- public void act(Position position) {
- position.col--;
- }
-
- public boolean shouldTurn(Position position, AntMap antMap) {
- return position.col == 0
- || antMap.isFilled(position.previousCol());
- }
- },
- UP {
- public void act(Position position) {
- position.row--;
- }
-
- public boolean shouldTurn(Position position, AntMap antMap) {
- return position.row == 0
- || antMap.isFilled(position.previousRow());
- }
- };
- public abstract void act(Position position);
- public abstract boolean shouldTurn(Position position, AntMap antMap);
-
- private static List< Action> list = new ArrayList< Action>();
-
- public static List< Action> actions() {
- if (!list.isEmpty()) return list;
- list.add(FORWARD);
- list.add(DOWN);
- list.add(BACKWARD);
- list.add(UP);
- return list;
- }
- }
-
- class Position {
-
- int row;
- int col;
-
- public Position(int row, int col) {
- this.row = row;
- this.col = col;
- }
-
- public Position previousCol() {
- return new Position(row, col-1);
- }
- public Position nextCol() {
- return new Position(row, col+1);
- }
- public Position previousRow() {
- return new Position(row-1, col);
- }
- public Position nextRow() {
- return new Position(row+1, col);
- }
- }
-
- class Ant {
-
- AntMap antMap = null;
- Action curentAction = Action.FORWARD;
-
- Ant(AntMap antMap) {
- this.antMap = antMap;
- }
- void crawl(Position position, int count) {
- antMap.matrix[position.row][position.col] = count;
- if (curentAction.shouldTurn(position, antMap))
- turn();
- curentAction.act(position);
- }
-
- private void turn() {
- List< Action> actions = Action.actions();
- curentAction = actions.get((actions.indexOf(curentAction)+1)%4);
- }
- }
复制代码 运行结果:
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 |
|