栈的括号匹配问题,迷宫求解问题 以及队列求杨辉三角,栈与队列判断是否为回文字符串

思路:依次遍历字符串中的每个字符,如果是左括号,就入栈,

                                                             如果是右括号

                                                                           如果栈为空,返回false

                                                                           如果栈不为空,则和栈顶元素匹配

                                                                                 如果不匹配,返回false

                                                                                 如果匹配,弹出栈顶元素

       最后判断栈是否为空,为空返回true,不为空,返回false

 public bool isMacth(string content)
    {
        Stack stack = new Stack();
        for (int i = 0; i < content.Length; i++)
        {
            switch (content[i])
            {
                case '(':
                    stack.Push(content[i]);
                    break;
                case '[':
                    stack.Push(content[i]);
                    break;
                case '{':
                    stack.Push(content[i]);
                    break;
                case ')':
                    if (stack.Count == 0)
                        return false;
                    if ((char)stack.Peek() == '(')
                        stack.Pop();
                    break;
                case ']':
                    if (stack.Count == 0)
                        return false;
                    if ((char)stack.Peek() == '[')
                        stack.Pop();
                    break;
                case '}':
                    if (stack.Count == 0)
                        return false;
                    if ((char)stack.Peek() == '{')
                        stack.Pop();
                    break;
            }
        }
        if (stack.Count == 0)
            return true;
        else
            return false;
    }

迷宫求解(从一个点到另一个点是否有路径可以到达)

思路:利用穷举法,从第一个点开始,按顺时针依次检查它的右上左下,四个方向的点,能不能通过,能通过的条件是不是障碍物或者没有通过过,如果可以通过,入栈,则更改要检查的点为能通过的点,如果四个方向都不能,则出栈,标记该点为不能通过或者已通过过

 Stack stack;
    //迷宫地图
    public int[,] map = new int[,] {  {1,0,1,1,1,1,1,1},
                                      {1,0,1,1,1,1,0,1},
                                      {1,0,0,0,0,0,0,1},
                                      {1,0,1,1,1,1,1,1},
                                      {1,0,0,0,0,0,0,0},
                                      {1,1,1,1,1,0,1,1},
                                      {1,1,1,1,0,0,0,1},
                                      {1,1,1,0,0,1,0,1},
                                      {1,1,0,0,1,1,0,1},
                                      {1,1,0,0,0,0,0,0},

    };
    //每一个地图块的属性
    public Data[,] maps = new Data[10, 8];
    void Start()
    {
        for (int i = 0; i < map.GetLength(0); i++)
        {
            for (int j = 0; j < map.GetLength(1); j++)
            {
                Data data = new Data() { x = i, y = j, index = 0, values = map[i, j], hasPass = false };
                maps[i, j] = data;
            }
        }
        Debug.Log(isHasPass(new Vector2(0, 1), new Vector2(9, 7)));
    }

   
    /// <summary>
    /// 检测是否有路径可以通过
    /// </summary>
    /// <param name="start">初始点</param>
    /// <param name="end">终点</param>
    /// <returns></returns>
    public bool isHasPass(Vector2 start, Vector2 end)
    {
        Data current = maps[(int)start.x, (int)start.y];
        current.x = (int)start.x;
        current.y = (int)start.y;
        stack = new Stack();
        do
        {
            if (!current.CannotPass && !current.hasPass)//如果可以通过,且没有通过过
            {
                if (current.x == (int)end.x && current.y == (int)end.y)//如果是重点,返回true
                    return true;
                current.hasPass = true;//标记已经检查过了
                stack.Push(current);//入栈,查找下一个,从右边开始
                current.index++;//
                if (current.y + 1 < 8)//索引不能越界,注意y表示的是列数,x表示的是行数,如果想右移一位,是水平移动,增加的是列数y,不是x
                {
                    current = maps[current.x, current.y + 1];//设置下一个检查点为右边的点
                }
            }
            else if (current.CannotPass || current.hasPass)//如果不可以通过或者已经检查过
            {
                if (stack.Count < 0)//说明起始点就是障碍物,不能通过
                    return false;
                switch (((Data)stack.Peek()).index)//检查栈顶的索引,查看目前的检查点是该点的上下左右那个方向
                {
                    case 1://上
                        ((Data)stack.Peek()).index++;//检查点往后移一位
                        if (current.x - 1 >= 0)
                        {
                            current = maps[((Data)stack.Peek()).x - 1, ((Data)stack.Peek()).y];//设置检查点为上位置
                        }
                        break;
                    case 2://左
                        ((Data)stack.Peek()).index++;
                        if (current.y - 1 >= 0)
                        {
                            current = maps[((Data)stack.Peek()).x, ((Data)stack.Peek()).y - 1];//检查点为左位置
                        }
                        break;
                    case 3://右
                        ((Data)stack.Peek()).index++;
                        if (current.x + 1 < 10)
                        {
                            current = maps[((Data)stack.Peek()).x + 1, ((Data)stack.Peek()).y];//检查点为下位置
                        }
                        break;
                    case 4://如果四个点检查完了,也就是四个点都不能通过,说明要往回退
                        maps[((Data)stack.Peek()).x, ((Data)stack.Peek()).y].hasPass = true;//先在地图上设置该点已经检查过了
                        current.hasPass = true;//设置该点的最后一个邻点也检查过了
                        stack.Pop();//弹出该点
                        if (stack.Count > 0)
                            current = (Data)stack.Peek();//回退一个位置,设置当前点为栈顶点
                        break;
                }
            }
        }
        while (stack.Count > 0);
        return false;
    }
}
[System.Serializable]
public class Data
{
    public int x, y;//地图的位置
    public int index = 0;//检查的索引
    public int values;//地图的值,是否是障碍物
    public bool hasPass;//有没有检查过

    bool cannotPass;//可不可以通过
    public bool CannotPass { get { return values == 1; } }
}

队列:也是一种操作受限的线性表,先进先出

杨辉三角:

思路:1.先把第一行的值入队

          2.得到当前队的长度len

         3. 对头元素出队,用temp元素返回

         4.重复2-3步len-1次,0入队

         5.重复2-5步n-1次

         6.当前队内的数据,就是杨辉三角第n行的数

  /// <summary>
    /// 求解杨辉三角的第几行
    /// </summary>
    /// <param name="n"></param>
    public void PascalTriangle(int n)
    {
        queue.Enqueue(0);
        queue.Enqueue(1);
        queue.Enqueue(0);
        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < queue.Count - 1; j++)
            {
                int temp = (int)queue.Dequeue();
                queue.Enqueue((temp + (int)queue.Peek()));
            }
            queue.Enqueue(0);
        }
        foreach (var item in queue.ToArray())
        {
            Debug.Log(item);
        }
    }

栈与队列的方式判断是不是回文字符串

回文字符串:正反读都是一样的,比如121,122232221

栈是先进后出,队是先进先出,首先把字符串分别单独入栈和队,然后比较出栈和出队的字符是不是相等,就可以了

发布了80 篇原创文章 · 获赞 7 · 访问量 2682

猜你喜欢

转载自blog.csdn.net/qq_37672438/article/details/103921894