数据结构-严蔚敏:栈(进制转换,括号匹配校验,迷宫求解)

本章包含如下内容
1. 进制转换
2. 括号匹配校验
3. 迷宫求解

  1. 进制转换

/**
     * 进制转换
     * @param target 待转换元素
     * @param format 转换进制
     */
    private String conversion(int target, int format) {
        Stack<Integer> stack = new StackImpl<>();
        while (target != 0) {
            stack.push(target % format);
            target = target / format;
        }

        StringBuilder stringBuilder = new StringBuilder();
        while (!stack.isEmpty()) {
            stringBuilder.append(stack.pop());
        }

        return stringBuilder.toString();
    }
  1. 括号匹配校验

 private boolean match(String target) throws Exception {
        Stack<Character> stack = new StackImpl<>();
        for (int i = 0; i < target.length(); i++) {
            char c = target.charAt(i);
            switch (c) {
                case '(':
                    stack.push(c);
                    break;
                case '{':
                    stack.push(c);
                    break;
                case '[':
                    stack.push(c);
                    break;
                case ')':
                    if (stack.pop() != '(') {
                        return false;
                    }
                    break;
                case '}':
                    if (stack.pop() != '{') {
                        return false;
                    }
                    break;
                case ']':
                    if (stack.pop() != '[') {
                        return false;
                    }
                    break;
                default:
                    throw new Exception("只能支持'(' ')' '{' '}' '[' ']' ");
            }
        }
        return true;
    }
  1. 迷宫求解
    迷宫用二维数组来表示,总体思想就是优先往上走,上走不通往左走,左走不通往下走。已经走过上的只能往左走,已经走过左的只能往下走,已经走过下的只能出栈
public class Maze {
    class Location {
        private final int x;
        private final int y;
        //限制方向
        //0:可以往任何方向走
        //1:不能往上走
        //2:不能往左,上走
        //3:都不能走,等待弹出
        private int label;

        public int getLabel() {
            return label;
        }

        public void setLabel(int label) {
            this.label = label;
        }

        Location(int x, int y, int label) {
            this.x = x;
            this.y = y;
            this.label = label;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Location location = (Location) o;
            return x == location.x &&
                    y == location.y;
        }

        @Override
        public int hashCode() {

            return Objects.hash(x, y);
        }
    }

    @Test
    public void maze() {
        int[][] maze = {
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 1, 1, 0, 1, 1, 1, 0, 1, 0},
                {0, 1, 1, 0, 1, 1, 1, 0, 1, 0},
                {0, 1, 1, 1, 1, 0, 0, 1, 1, 0},
                {0, 1, 0, 0, 0, 1, 1, 1, 1, 0},
                {0, 1, 1, 1, 0, 1, 1, 1, 1, 0},
                {0, 1, 0, 1, 1, 1, 0, 1, 1, 0},
                {0, 1, 0, 0, 0, 1, 0, 0, 1, 0},
                {0, 0, 1, 1, 1, 1, 1, 1, 1, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        };
        System.out.println(maze[1][3]);
        System.out.println(maze(maze, new Location(1, 1, 0), new Location(8, 8, 0)));
    }
    
    boolean maze(int[][] maze, Location beginLocation, Location endLocation) {
        Stack<Location> stack = new StackImpl<>();
        stack.push(beginLocation);
        while (!stack.isEmpty()) {
            Location current = stack.getTop();
            if (current.equals(endLocation)) {
                return true;
            }
            //不能走则弹出
            if (maze[current.y][current.x] == 0) {
                stack.pop();
                continue;
            }
            //从上走,已经走过上的不能再走
            if (current.label < 1) {

                if (current.y - 1 >= 0 ) {
                    //限制不能往上走
                    current.setLabel(1);
                    //可以往上,左
                    stack.push(new Location(current.x, current.y - 1, 0));
                    continue;
                }
            }

            //向左走,已经走过左的不能再走
            if (current.label < 2) {

                if (current.x + 1 < maze[0].length ) {
                    //限制不能往左走
                    current.setLabel(2);
                    //可以上,左,下
                    stack.push(new Location(current.x + 1, current.y, 0));
                    continue;
                }
            }

            //向下走
            if (current.label < 3) {
                if (current.y + 1 < maze.length) {
                    //这个位置已经往下走过了
                    current.setLabel(3);
                    //不能往上走,可以左,下
                    stack.push(new Location(current.x, current.y + 1, 1));
                    continue;
                }
            }
            //都不能走
            stack.pop();
        }
        return false;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 初衷:看了很多视频、文章,最后却通通忘记了,别人的知识依旧是别人的,自己却什么都没获得。此系列文章旨在加深自己的印...
    DCbryant阅读 9,502评论 0 20
  • 通常程序开发中内存管理是非常重要的,而内存主要分为占内存和堆内存。那么栈和堆内存有什么区别呢?希望在这篇文章里能带...
    xiaoyouPrince阅读 3,841评论 0 3
  • 做题,实际写出例子,然后分析可能遇到的情况,慢慢的,思路就会出来了。 线性表 33. Search in Rota...
    小碧小琳阅读 5,503评论 0 2
  • 傻笑,其实是一种高明的处世方法。 这种方法需要人具备水一样的性格,上善若水、圆融,任何容器都能容,随着容器的变化而...
    秦东魁阅读 3,352评论 0 0
  • 就是今天 大姨妈来了 没有那么疼 但是也不舒服 感谢宝玉的抱枕 还是一如既往的打盹 不过晚上的时候用心做了昨天赶进...
    Emily_e135阅读 780评论 0 0