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

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

作者: thebigsilly | 来源:发表于2018-04-15 19:52 被阅读0次

本章包含如下内容
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;
    }
}

相关文章

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

    本章包含如下内容1. 进制转换2. 括号匹配校验3. 迷宫求解 进制转换 括号匹配校验 迷宫求解迷宫用二维数组来表...

  • 数据结构总结

    严蔚敏版总结 一.线性表 数组形式 链表形式 二.栈和队列 1.顺序栈 2.栈的应用 ①数制转换 ②括号匹配 ...

  • 栈的应用举例:迷宫求解

    参考严蔚敏老师的《数据结构》栈和队列这一章的相关内容完成的 栈的定义与基本操作的实现 迷宫求解的算法实现 用到的一...

  • 20. Valid Parentheses

    使用栈数据结构: 遇到左括号,需要压栈。 遇到右括号,判断栈顶是否和当前右括号匹配;若不匹配则返回false,否则...

  • 408计算机学科专业基础综合参考书目(转载)

    一、数据结构 1.教材:《数据结构》严蔚敏 清华大学出版社清华大学严蔚敏的这本数据结构的教材是国内数据结构教材的权...

  • 如何用数组来实现栈和队列

    Stack 栈是一种后入先出的数据结构,仅限定在栈顶进行插入和删除操作。栈结构的实际应用主要有数制转换、括号匹配、...

  • 数据结构复习

    第三章 栈和队列 一 栈 栈的类型 顺序栈 链式栈 双向栈 栈的应用 数制转换 行编辑程序 迷宫求解 表达式求值:...

  • 栈--利用栈实现进制转换

    利用栈实现进制转换 一、二进制转十进制 利用栈的数据结构特点,将二进制转换为十进制数。 二进制数是计算机数据的存储...

  • iOS架构

    mvc mvvm mvp 三四层 数据结构与算法严蔚敏,《数据结构》《大话数据结构与算法》 网络《HTTP...

  • 【数据结构】【C#】010-栈的应用:🌜🌛括号匹配

    C#数据结构:栈的应用:括号匹配问题 假设表达式中包含三种括号:圆括号、方括号和花括号,它们可互相嵌套,如 ( [...

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/cktjkftx.html