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. VMwarevSphere Client 链接 vCenter Server中的主机,开启虚拟机提示:在主机当前连接状况下不允许执行该操作

    VMwarevSphere Client 链接 vCenter Server中的主机,开启虚拟机提示:在主机当前连接状况下不允许执行该操作很多原因都可以导致该问题出现,例如 vCenter Serve ...

  2. ueditor 去掉自动跟随内容的<p><br /></p>

    //编辑器不能为空内容 if (domUtils.isEmptyNode(me.body)) { me.body.innerHTML = ''; ueditor.all.js  删除BR 如果还不行, ...

  3. 第十五章·Kibana深入-Dev Tools及Lucene语法

    Dev Tools介绍 Dev Tools 页面包含开发工具,您可以使用这些Dev Tools与Kibana中的数据进行交互. 原先的交互式控制台Sense,使用户方便的通过浏览器直接与Elastic ...

  4. Django REST Framework(DRF)_第四篇

    DRF分页(总共三种) PageNumberPagination(指定第n页,每页显示n条数据) 说明 既然要用人家的那么我们就先来看下源码,这个分页类源码中举例通过参数指定第几页和每页显示的数据:h ...

  5. 二进制;16进制; Byte , Python的bytes类; Base64数据编码; Bae64模块;

    参考:中文维基 二进制 位操作(wiki) Byte字节 互联网数据处理:Base64数据编码 Python的模块Base64 16进制简介 python: bytes对象 字符集介绍:ascii 二 ...

  6. @Mapper和@Repository

    参考地址 https://www.cnblogs.com/jtfr/p/10962205.html 相同点 两者都是作用于dao上 不同点 @Repository需要在Spring中配置扫描地址,然后 ...

  7. 第六章 组件 56 组件-组件中的data

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  8. 报表开发神器!DevExpress Reporting v19.1全平台新功能解析

    行业领先的.NET界面控件DevExpress Reporting全新发布了v19.1版本,本文主要为大家介绍.NET Reporting v19.1中发布的所有平台的新功能,欢迎下载v19.1试用, ...

  9. ACID理解

    数据库事物的4个特性. A原子性:多次操作要么全部成功,要么全部失败.undo日志是在事务执行失败的时候撤销对数据库的操作,保证了事务的原子性(Atomicity) C一致性:一致性这个最不好理解.数 ...

  10. BBS-media配置

    media配置: 在上传头像的时候会用到media,首先需要在setting中加下面这一句话 MEDIA_ROOT=os.path.join(BASE_DIR,"blog",&qu ...