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语言-来自拍拍贷的数据探索
案例分析:拍拍贷是中国的一家在线借贷平台,网站撮合了一些有闲钱的人和一些急用钱的人.用户若有贷款需求,可在网站上选择借款金额. 本项目拟通过该数据集的探索,结合自己的理解进行分析,最终目的的是初步预测 ...
随机推荐
- zabbix监控项截图整理
general监控项
- 《python解释器源码剖析》第7章--python中的set对象
7.0 序 集合和字典一样,都是性能非常高效的数据结构,性能高效的原因就在于底层使用了哈希表.因此集合和字典的原理本质上是一样的,都是把值映射成索引,通过索引去查找. 7.1 PySetObject ...
- JDBC的两种sql命令发送器比较【Statement:PreparedStatement】
PreparedStatement 接口继承 Statement接口如果需要多次执行一个SQL语句,可以使用PreparedStatement对象.在创建PreparedStatement对象时,通过 ...
- js常用阻止冒泡事件
原文链接:http://caibaojian.com/javascript-stoppropagation-preventdefault.html 防止冒泡 w3c的方法是e.stopPropagat ...
- 3.NumPy - 数组属性
1.ndarray.shape 这一数组属性返回一个包含数组维度的元组,它也可以用于调整数组大小 # -*- coding: utf-8 -*- import numpy as np a = np.a ...
- VIM技巧----改变
1.大小写转换 ~ 将光标下的字母改变大小写 vaw~ 选中单词(vaw:a会选择一个对象(an object)包括空格在内)后进行大小写转换 viw~ 选中单词(viw:i会选择一个对象的内部(an ...
- Cmd有关IP的部分命令
ping命令判断系统数据包在传送的时候至少会经过一个以上的路由器,当数据包经过一个路由器的时候,TTL就会自动减1,如果减到0了还是没有传送到目的主机,那么这个数据包就会自动丢失,这时路由器会发送一个 ...
- Tomcat基础知识
介绍Tomcat之前先介绍下Java相关的知识. 各常见组件: 1.服务器(server):Tomcat的一个实例,通常一个JVM只能包含一个Tomcat实例:因此,一台物理服务器上可以在启动多个JV ...
- pycharm运行程序,总是出现IPthony界面(IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help. PyDev console: using IPython 6.2.1)
解决方式如下: 取消即可.
- veeValidate实战
说在前面 vee-validate 版本2.0.4的学习github地址我的项目地址第一次认真的在git上写一个demo教程,喜欢的可以star一下~^o^~ (^-^) (^o^) 后续会有一个完整 ...