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. 正确理解这四个重要且容易混乱的知识点:异步,同步,阻塞,非阻塞,5种IO模型

    本文讨论的背景是Linux环境下的network IO,同步IO和异步IO,阻塞IO和非阻塞IO分别是什么 概念说明 在进行解释之前,首先要说明几个概念: - 用户空间和内核空间 - 进程切换 - 进 ...

  2. PAT Basic 1094 谷歌的招聘 (20 分)

    20 5 23654987725541023819 输出样例 1: 49877 输入样例 2: 10 3 2468024680 输出样例 2: 404 #include <iostream> ...

  3. 1.opencv_画图

    #导入工具包 import numpy as np import cv2 import matplotlib.pyplot as plt # 定义显示图片 def show(image): plt.i ...

  4. poj3691 DNA repair[DP+AC自动机]

    $给定 n 个模式串,和一个长度为 m 的原串 s,求至少修改原串中的几个字符可以使得原串中不包含任一个模式串.模式串总长度 ≤ 1000,m ≤ 1000.$ 先建出模式串的AC自动机,然后考虑怎么 ...

  5. 内置对象-Math

    1.随机数 Math.random() 1)获得0-1之间的随机数 2)0到100:Math.round(Math.random()*100) 2.max:求最大值 ,,,) console.log( ...

  6. 报错:required string parameter XXX is not present

    报错:required string parameter XXX is not present 不同工具发起的get/delete请求,大多数不支持@RequestParam,只支持@PathVari ...

  7. phpstudy 80端口被占用的解决方法

    1.执行httpd.exe  D:\phpStudy\PHPTutorial\Apache\bin>httpd.exe   返回 could not bind to address 0.0.0. ...

  8. docker stack /swarm 替代 docker-compose 进行部署

    之前一直用docker-compose开发了几个单例的service, 今天开始压力测试, 结果发现postgres的CPU负载很重, 就想设置cpus 结果发现docker-compose V3之后 ...

  9. cogs服务点设置(不凶,超乖) x

    cogs3. 服务点设置 ★   输入文件:djsa.in   输出文件:djsa.out   简单对比时间限制:1 s   内存限制:128 MB 问题描述为了进一步普及九年义务教育,政府要在某乡镇 ...

  10. 响应式web布局

    通过不同的媒体类型和条件定义样式表规则,媒体查询让CSS可以更精确作用于不同的媒体类型和同一媒体的不同条件.媒体查询的大部分媒体特性都接受min和max用于表达”大于或等于”和”小与或等于”.如:wi ...