zoukankan      html  css  js  c++  java
  • 一个打印螺旋数的程序

    看到博客园上有人发这个程序的练习,便自己也写了一个。

    代码
     1 class ScremNumbers
     2 {
     3     enum Direction { Right, Down, Left, Up }
     4 
     5     [System.Diagnostics.DebuggerDisplay("({X},{Y})")]
     6     struct Point
     7     {
     8         public int X;
     9         public int Y;
    10     }
    11 
    12     public int Count { getprivate set; }
    13 
    14     int[,] numbers;
    15     public ScremNumbers(int count)
    16     {
    17         this.Count = count;
    18         numbers = new int[count, count];
    19 
    20         Process();
    21     }
    22 
    23     void Process()
    24     {
    25         var point = new Point();
    26         var direction = Direction.Right;
    27 
    28         for (int i = 0; i < Count * Count; i++)
    29         {
    30             var nextPoint = NextPoint(point, direction);
    31             if (this[nextPoint] != 0)
    32             {
    33                 direction = NextDirection(direction);
    34                 nextPoint = NextPoint(point, direction);
    35             }
    36 
    37             this[point] = i + 1;
    38             point = nextPoint;
    39         }
    40     }
    41 
    42     public void Print()
    43     {
    44         var maxLength = (Count * Count).ToString().Length + 1;
    45 
    46         for (int i = 0; i < Count; i++)
    47         {
    48             for (int j = 0; j < Count; j++)
    49             {
    50                 Console.Write(numbers[j, i].ToString().PadRight(4));
    51             }
    52             Console.WriteLine();
    53         }
    54     }
    55 
    56     Point NextPoint(Point point, Direction direction)
    57     {
    58         var nextPoint = point;
    59         switch (direction)
    60         {
    61             case Direction.Right:
    62                 nextPoint.X++;
    63                 break;
    64             case Direction.Down:
    65                 nextPoint.Y++;
    66                 break;
    67             case Direction.Left:
    68                 nextPoint.X--;
    69                 break;
    70             case Direction.Up:
    71                 nextPoint.Y--;
    72                 break;
    73             default:
    74                 throw new InvalidOperationException();
    75         }
    76 
    77         return nextPoint;
    78     }
    79 
    80     Direction NextDirection(Direction direction)
    81     {
    82         return (Direction)(((int)direction + 1% 4);
    83     }
    84 
    85     private int this[Point index]
    86     {
    87         get
    88         {
    89             if (index.X < 0 || index.X >= Count || index.Y < 0 || index.Y >= Count)
    90                 return -1;
    91 
    92             return numbers[index.X, index.Y];
    93         }
    94 
    95         set { numbers[index.X, index.Y] = value; }
    96     }
    97 }

    程序用的算法比较简单,是一种最直接的算法:不停的往前走,碰壁便转弯。总体感觉整体思路还比较清晰,这里记录一下。

  • 相关阅读:
    【Educational Codeforces Round 33 B】Beautiful Divisors
    【Educational Codeforces Round 33 A】Chess For Three
    【习题 6-7 UVA
    【习题 6-6 UVA
    【习题 6-5 UVA-1600】Patrol Robot
    【习题 6-4 UVA-439】Knight Moves
    【习题 6-3 UVA
    【POJ1144】Network(割点)(模板)
    【CF711D】Directed Roads(环,强连通分量)
    【CF711C】Coloring Trees(DP)
  • 原文地址:https://www.cnblogs.com/TianFang/p/1918330.html
Copyright © 2011-2022 走看看