解决输出顺时针螺旋数组问题【寻路算法】

现有题如下:


 

接题思路:

  1. 先简化,我们输入的值为3.
  2. 首先生成一个二维数组且初始化为0,如下图:
[0,0] = 0 [0,0] = 0 [0,0] = 0
[0,0] = 0 [0,0] = 0 [0,0] = 0
[0,0] = 0 [0,0] = 0 [0,0] = 0

  此时我们建立一套规则,我们有一个变量记录方向,随着条件的变化,他的变化规则是→↓←↑(右下左上顺时针)。

  • 当向右的时候数组[x,y], 判断数组[x,y]==0是否为真,当为真且y<3-1的时候x不变,y+1。当y>=3的时候,方向改为向下,并且把对应的步幅赋值给数组,此时数组如下:
[0,0]=1 [0,1]=2 [0,2]=3
[0,0] = 0 [0,0] = 0 [0,0] = 0
[0,0] = 0 [0,0] = 0 [0,0] = 0
  • 当向下的时候,判断数组[x,y]==0是否为真,当为真且 x<3-1, 数组y不动,x+1,当x>=3的时候,方向改向左,此时数组如下:
    [0,0]=1 [0,1]=2 [0,2]=3

    [0,0] = 0
    [0,0] = 0 [1,2]=4
    [0,0] = 0 [0,0] = 0

    [2,2]=5

  • 当向左的时候,判断数组[x,y]==0是否为真,当为真且y>1,数组x不动,y-1,当y<0的时候,方向改向上,此时数组如下:
    [0,0]=1 [0,1]=2 [0,2]=3
    [0,0] = 0 [0,0] = 0 [1,2]=4
    [2,0]=7 [2,1]=6 [2,2]=5
  • 当向上的时候,判断数组[x,y]==0是否为真,当为真且x>1,数组y不动,x-1,当x<0的时候,方向改向右,向右时用第一条规则,此时数组如下:
    [0,0]=1 [0,1]=2 [0,2]=3
    [1,0]=8 [1,1]=9 [1,2]=4
    [2,0]=7 [2,1]=6 [2,2]=5

代码方面。把3发散为N,现在需要一个入参count,一个二维数组arr,一个记录步数的变量steps,一个表达方向的枚举。具体如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SearchPath
{
    class Searcher
    {
        private int count;
        private int[,] arr;
        private enum Direction
        {
            right,
            left,
            up,
            down
        }

        public Searcher(int count)
        {
            this.count = count;
            arr = new int[count, count];
        }

        public int[,] SearchWay()
        {
            int x = 0;
            int y = 0;
            int steps = 1;
            Direction dir = Direction.right;
            arr[0, 0] = steps;
            while (true)
            {
                switch (dir)
                {
                    case (Direction.right):
                        {
                            if (y + 1 < count && arr[x, y + 1] == 0)
                            {
                                y++; steps++;
                                arr[x, y] = steps;
                            }
                            else
                            {
                                dir = Direction.down;
                            }
                        }
                        break;
                    case (Direction.down):
                        {
                            if (x + 1 < count && arr[x + 1, y] == 0)
                            {
                                x++; steps++;
                                arr[x, y] = steps;
                            }
                            else
                            {
                                dir = Direction.left;
                            }
                        }
                        break;
                    case (Direction.left):
                        {
                            if (y - 1 >= 0 && arr[x, y - 1] == 0)
                            {
                                y--; steps++;
                                arr[x, y] = steps;
                            }
                            else
                            {
                                dir = Direction.up;
                            }
                        }
                        break;
                    case (Direction.up):
                        {
                            if (x - 1 < count && arr[x - 1, y] == 0)
                            {
                                x--; steps++;
                                arr[x, y] = steps;
                            }
                            else
                            {
                                dir = Direction.right;
                            }
                        }
                        break;
                }
                
                if (steps == count * count)
                {
                    return arr;
                }
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/FanPigPig/p/10604819.html
今日推荐