In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /\, or blank space.  These characters divide the square into contiguous regions.

(Note that backslash characters are escaped, so a \ is represented as "\\".)

Return the number of regions.

我的DFS解法。

Runtime: 32 ms, faster than 12.39% of C++ online submissions for Regions Cut By Slashes.

#include <string>
#include <iostream>
#include <vector>
using namespace std;
int dirs[][] = {{,},{,-},{,},{-,},{,},{,-},{-,},{-,-}};
class Solution {
public:
int regionsBySlashes(vector<string>& grid) {
size_t n = grid.size();
vector<vector<int>> mtx(*n, vector<int>(*n, ));
for(int i=; i<n; i++){
for(int j=; j<n; j++){
if(grid[i][j] == '/'){
mtx[*i][*j+] = ;
mtx[*i+][*j+] = ;
mtx[*i+][*j+] = ;
mtx[*i+][*j] = ;
}else if(grid[i][j] == '\\'){
mtx[*i][*j] = ;
mtx[*i+][*j+] = ;
mtx[*i+][*j+] = ;
mtx[*i+][*j+] = ;
}
}
}
int ret = ;
for(int i=; i<*n; i++){
for(int j=; j<*n; j++){
if(mtx[i][j] == ) ret++;
dfs(mtx, i, j);
}
}
return ret;
}
void dfs(vector<vector<int>>& mtx, int x, int y){
size_t n = mtx.size();
if(x < || x >= n || y < || y >= n || mtx[x][y] != ) return ;
mtx[x][y] = -;
for(int i=; i<; i++){
int newx = x+dirs[i][];
int newy = y+dirs[i][];
if(newx >= && newy >= && newx < n && newy < n && i >= ){
if(mtx[newx][y] == && mtx[x][newy] == ) continue;
}
dfs(mtx, newx, newy);
}
}
};

网上的unionfound解法。

class Solution {
public:
int count, n;
vector<int> f;
int regionsBySlashes(vector<string>& grid) {
n = grid.size();
count = n * n * ;
for (int i = ; i < n * n * ; ++i)
f.push_back(i);
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
if (i > ) uni(g(i - , j, ), g(i, j, ));
if (j > ) uni(g(i , j - , ), g(i , j, ));
if (grid[i][j] != '/') {
uni(g(i , j, ), g(i , j, ));
uni(g(i , j, ), g(i , j, ));
}
if (grid[i][j] != '\\') {
uni(g(i , j, ), g(i , j, ));
uni(g(i , j, ), g(i , j, ));
}
}
}
return count;
} int find(int x) {
if (x != f[x]) {
f[x] = find(f[x]);
}
return f[x];
}
void uni(int x, int y) {
x = find(x); y = find(y);
if (x != y) {
f[x] = y;
count--;
}
}
int g(int i, int j, int k) {
return (i * n + j) * + k;
}
};

LC 959. Regions Cut By Slashes的更多相关文章

  1. LeetCode 959. Regions Cut By Slashes

    原题链接在这里:https://leetcode.com/problems/regions-cut-by-slashes/ 题目: In a N x N grid composed of 1 x 1 ...

  2. 【leetcode】959. Regions Cut By Slashes

    题目如下: In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank spac ...

  3. 【LeetCode】959. Regions Cut By Slashes 由斜杠划分区域(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 日期 题目地址:https://leetcod ...

  4. [Swift]LeetCode959. 由斜杠划分区域 | Regions Cut By Slashes

    In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space.  Th ...

  5. Leetcode959. Regions Cut By Slashes由斜杠划分区域

    在由 1 x 1 方格组成的 N x N 网格 grid 中,每个 1 x 1 方块由 /.\ 或空格构成.这些字符会将方块划分为一些共边的区域. (请注意,反斜杠字符是转义的,因此 \ 用 &quo ...

  6. weekly contest 115

    958. Check Completeness of a Binary Tree Given a binary tree, determine if it is a complete binary t ...

  7. 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)

    Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. R语言-来自拍拍贷的数据探索

    案例分析:拍拍贷是中国的一家在线借贷平台,网站撮合了一些有闲钱的人和一些急用钱的人.用户若有贷款需求,可在网站上选择借款金额. 本项目拟通过该数据集的探索,结合自己的理解进行分析,最终目的的是初步预测 ...

随机推荐

  1. Win10系统的开机启动项如何去关闭?

    我们在使用电脑时会安装许多的应用程序,而有些应用程序会默认在电脑开机时自行启动,不仅影响开机速度,还会在开机后占用电脑内存资源. 那么在Win10系统中,我们该如何查看有哪些开机启动项呢?我们又该怎么 ...

  2. 第五章·Logstash深入-日志收集

    1.Logstash收集单个日志到文件中 file模块收集日志 不难理解,我们的日志通常都是在日志文件中存储的,所以,当我们在使用INPUT插件时,收集日志,需要使用file模块,从文件中读取日志的内 ...

  3. Linux之apt-get软件管理

    apt-get 用Linux apt-get命令的第一步就是引入必需的软件库,Debian的软件库也就是所有Debian软件包的集合,它们存在互联网上的一些公共站点上.把它们的地址加入,apt-get ...

  4. Windows下Redis如何永久更改密码

    公司使用的是Spring-session-redis 需要给Redis配置一个密码 本来我配置密码的方法是 先打开Redis服务 在采用 命令 CONFIG SET requirepass " ...

  5. mybatis整合Spring编码

    mybatis整合Spring的核心代码 spring-dao.xml <?xml version="1.0" encoding="UTF-8"?> ...

  6. windows下基于IIS配置ssl证书遇到的坑

    前几天配置windows下基于IIS配置ssl证书 完全按照步骤执行 绑定https网址后,一直显示:无法访问此网站 检查了443端口,还有防火墙限制,没发现什么 足足困扰了我好几天 后来突然想到前不 ...

  7. systemctl详解

    [root@hadoop01 hadoop]# systemctl --help systemctl [OPTIONS...] {COMMAND} ... Query or send control ...

  8. jenkins汉化

    插件: Localization: Chinese (Simplified) locale plugin(或者是这个版本不一样,名字不一样) 可以直接安装这个插件,然后走最后一步设置即可. 由于安装失 ...

  9. EntityManager的merge()方法

    EntityManager的merge()方法相当于hibernate中session的saveOrUpdate()方法: 用于实体的插入和更新操作:

  10. URAL 2036 Intersect Until You're Sick of It 形成点的个数 next_permutation()函数

    A - Intersect Until You're Sick of It Time Limit:500MS     Memory Limit:65536KB     64bit IO Format: ...