LC 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 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的更多相关文章
- 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 ...
- 【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 ...
- 【LeetCode】959. Regions Cut By Slashes 由斜杠划分区域(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 日期 题目地址:https://leetcod ...
- [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 ...
- Leetcode959. Regions Cut By Slashes由斜杠划分区域
在由 1 x 1 方格组成的 N x N 网格 grid 中,每个 1 x 1 方块由 /.\ 或空格构成.这些字符会将方块划分为一些共边的区域. (请注意,反斜杠字符是转义的,因此 \ 用 &quo ...
- weekly contest 115
958. Check Completeness of a Binary Tree Given a binary tree, determine if it is a complete binary t ...
- 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)
Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- R语言-来自拍拍贷的数据探索
案例分析:拍拍贷是中国的一家在线借贷平台,网站撮合了一些有闲钱的人和一些急用钱的人.用户若有贷款需求,可在网站上选择借款金额. 本项目拟通过该数据集的探索,结合自己的理解进行分析,最终目的的是初步预测 ...
随机推荐
- go语言json转map
package util import ( "encoding/json" "fmt" ) // json转map函数,通用 func JSONToMap(st ...
- php 随笔 截取字符串 跳出循环 去除空格 修改上传文件大小限制
substr(string,start,length) echo substr("Hello world",6); world 跳出循环 for($i=1; $i<5; $i ...
- Linux下安装opencv with-ffmpeg解决无法读取视频的问题
1. 编译安装ffmpeg 下载源码,执行 ./configure --disable-yasm --enbale-shared --prefix=/usr/local/ffmpeg 即可. 2. 下 ...
- solr7中文分词包
刚刚将solr4升级到了solr7.7,发现之前用的mmseg4j中文分词包用的时候会报错,插入新数据是创建索引会有异常 possible analysis error: startOffset mu ...
- Winfrom TextBox 添加水印文字 + 字体颜色
using System.Drawing; using System.Windows.Forms; namespace KK.WaterMark.Control { public partial cl ...
- c++第三次作业:类的友元
C++第三次作业:类的友元 1.友元的关系提供了不同类或对象的成员函数之间.类的成员函数与一般函数之间进行数据共享的机制.通俗的说友元关系就是一个类主动声明其他函数是他的朋友,可以使其获得特殊访问权利 ...
- C#工具:ASP.net 调用SQLserver帮助类
一.正常调用 1.创建DBHelper帮助类 2.复制以下代码到类中 using System; using System.Collections.Generic; using System.Data ...
- solr 倒排索引(转载)
原文地址:http://blog.csdn.net/chichengit/article/details/9235157 http://blog.csdn.net/njpjsoftdev/articl ...
- (八)zabbix获取到的数值自定义单位
1) 查找php文件 # find / -name "func.inc.php" /usr/share/zabbix/include/func.inc.php 2)修改文件 #vi ...
- 函数参数-undefined-默认值-可选参数
1.函数参数为undefined时,触发参数默认值 2.实参数量 < 形参数量,多余的形参值为 underfined 3.设置可选参数 1)JS中: 检测 undefined :function ...