• leecode959. 由斜杠划分区域


    在由 1 x 1 方格组成的 n x n 网格 grid 中,每个 1 x 1 方块由 '/'、'\' 或空格构成。这些字符会将方块划分为一些共边的区域。

    给定网格 grid 表示为一个字符串数组,返回 区域的数量 。

    请注意,反斜杠字符是转义的,因此 '\' 用 '\\' 表示。

    示例 1:

    输入:grid = [" /","/ "]
    输出:2
    示例 2:

    输入:grid = [" /","  "]
    输出:1
    示例 3:

    输入:grid = ["/\\","\\/"]
    输出:5
    解释:回想一下,因为 \ 字符是转义的,所以 "/\\" 表示 /\,而 "\\/" 表示 \/。

    1. /* ________
    2. * |\ 1 / |
    3. * | \/ |
    4. * | 0/\ 2|
    5. * | / 3 \|
    6. * --------
    7. * 按如上划分每个 1x1 的方格为四个部分,并分配序号

    class Solution {
        public int regionsBySlashes(String[] grid) {
            int len = grid.length;
            int size = len * len * 4;
            UnionfindLinkNode u = new UnionfindLinkNode(size);
    
            int start = 0;
            for (int i = 0; i < grid.length; i++) {
                String line = grid[i];
                for (int j = 0; j < line.length(); j++) {
                    int left = start++;
                    int up = start++;
                    int right = start++;
                    int down = start++;
                    //不是最左边
                    if (j != 0) {
                        //合入左边
                        u.union(left, left - 2);
                    }
                    //不是第一层
                    if (i != 0) {
                        //合入上边
                        u.union(up, up - ((len - 1) * 4 + 2));
                    }
    
                    char cur = line.charAt(j);
    
                    if (cur == '/') {
                        u.union(left, up);
                        u.union(right, down);
                    } else if (cur == '\\') {
                        u.union(right, up);
                        u.union(left, down);
                    } else {
                        u.union(left, up);
                        u.union(up, right);
                        u.union(right, down);
                    }
    
                }
            }
    
            return u.size();
        }
    }
    
    //链表实现的并查集
    class UnionfindLinkNode {
    
        private Node[] nodes;
    
        private int size;
    
        private int num;
    
        public UnionfindLinkNode(int num) {
            nodes = new Node[num];
            for (int i = 0; i < num; i++) {
                nodes[i] = new Node();
            }
            this.size = num;
            this.num = num;
        }
    
        public void union(int x, int y) {
            if (x < 0 || y < 0 || x > this.num - 1 || y > this.num - 1) {
                return;
            }
            Node nodeX = nodes[x];
            Node nodeY = nodes[y];
            //代表节点
            Node firstX = nodeX.first;
            Node firstY = nodeY.first;
            if (firstX == firstY) {
                return;
            }
            //尾节点
            Node tailX = firstX.pre;
            Node tailY = firstY.pre;
    
            tailX.next = firstY;
            firstY.pre = tailX;
    
            tailY.next = firstX;
            firstX.pre = tailY;
    
            //更新y链表的代表节点
            Node start = firstY;
            while (start != tailY) {
                start.first = firstX;
                start = start.next;
            }
            tailY.first = firstX;
            size--;
            System.out.println(x + ":" + y + ":" + size);
        }
    
        public boolean isSameSet(int x, int y) {
            return nodes[x].first == nodes[y].first;
        }
    
    
        public int size() {
            return size;
        }
    
    }
    
    
    class Node {
        //上一个节点
        Node pre = this;
        //下一个节点
        Node next = this;
        //代表节点
        Node first = this;
    
    }
    
    
  • 相关阅读:
    Android studio控制台 输出乱码解决方法
    【matplotlib】绘制散点图,为不同区域的点添加不同颜色+添加纵向\横向分割线(含Python代码实例)
    取得高等学校教师资格证应当具备什么学历要求
    Hystrix Readed time out,看我这一篇就让你彻底解决!
    Java JSON字符串转换成JSON对象
    iOS APP上架App Store其中一个步骤就是要把ipa文件上传到App Store
    springMvc5-IDEA修改背景颜色大全
    贰[2],OpenCV函数解析
    LeetCode算法递归类—剑指 Offer 26. 树的子结构
    nginx版本号隐藏(405 not allowed解决办法)
  • 原文地址:https://blog.csdn.net/liuhongtaowxp1/article/details/125891746