Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions solution/1500-1599/1559.Detect Cycles in 2D Grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,67 @@ public:
};
```

### **Rust**

```rust
impl Solution {
#[allow(dead_code)]
pub fn contains_cycle(grid: Vec<Vec<char>>) -> bool {
let n = grid.len();
let m = grid[0].len();
let mut d_set: Vec<usize> = vec![0; n * m];

// Initialize the disjoint set
for i in 0..n * m {
d_set[i] = i;
}

// Traverse the grid
for i in 0..n {
for j in 0..m {
if i + 1 < n && grid[i + 1][j] == grid[i][j] {
// Check the below cell
let p_curr = Self::find(i * m + j, &mut d_set);
let p_below = Self::find((i + 1) * m + j, &mut d_set);
if p_curr == p_below {
return true;
}
// Otherwise, union the two cells
Self::union(p_curr, p_below, &mut d_set);
}
// Same to the right cell
if j + 1 < m && grid[i][j + 1] == grid[i][j] {
let p_curr = Self::find(i * m + j, &mut d_set);
let p_right = Self::find(i * m + (j + 1), &mut d_set);
if p_curr == p_right {
return true;
}
// Otherwise, union the two cells
Self::union(p_curr, p_right, &mut d_set);
}
}
}

false
}

#[allow(dead_code)]
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
if d_set[x] != x {
d_set[x] = Self::find(d_set[x], d_set);
}
d_set[x]
}

#[allow(dead_code)]
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
let p_x = Self::find(x, d_set);
let p_y = Self::find(y, d_set);
d_set[p_x] = p_y;
}
}
```

### **Go**

```go
Expand Down
61 changes: 61 additions & 0 deletions solution/1500-1599/1559.Detect Cycles in 2D Grid/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,67 @@ public:
};
```

### **Rust**

```rust
impl Solution {
#[allow(dead_code)]
pub fn contains_cycle(grid: Vec<Vec<char>>) -> bool {
let n = grid.len();
let m = grid[0].len();
let mut d_set: Vec<usize> = vec![0; n * m];

// Initialize the disjoint set
for i in 0..n * m {
d_set[i] = i;
}

// Traverse the grid
for i in 0..n {
for j in 0..m {
if i + 1 < n && grid[i + 1][j] == grid[i][j] {
// Check the below cell
let p_curr = Self::find(i * m + j, &mut d_set);
let p_below = Self::find((i + 1) * m + j, &mut d_set);
if p_curr == p_below {
return true;
}
// Otherwise, union the two cells
Self::union(p_curr, p_below, &mut d_set);
}
// Same to the right cell
if j + 1 < m && grid[i][j + 1] == grid[i][j] {
let p_curr = Self::find(i * m + j, &mut d_set);
let p_right = Self::find(i * m + (j + 1), &mut d_set);
if p_curr == p_right {
return true;
}
// Otherwise, union the two cells
Self::union(p_curr, p_right, &mut d_set);
}
}
}

false
}

#[allow(dead_code)]
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
if d_set[x] != x {
d_set[x] = Self::find(d_set[x], d_set);
}
d_set[x]
}

#[allow(dead_code)]
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
let p_x = Self::find(x, d_set);
let p_y = Self::find(y, d_set);
d_set[p_x] = p_y;
}
}
```

### **Go**

```go
Expand Down
56 changes: 56 additions & 0 deletions solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
impl Solution {
#[allow(dead_code)]
pub fn contains_cycle(grid: Vec<Vec<char>>) -> bool {
let n = grid.len();
let m = grid[0].len();
let mut d_set: Vec<usize> = vec![0; n * m];

// Initialize the disjoint set
for i in 0..n * m {
d_set[i] = i;
}

// Traverse the grid
for i in 0..n {
for j in 0..m {
if i + 1 < n && grid[i + 1][j] == grid[i][j] {
// Check the below cell
let p_curr = Self::find(i * m + j, &mut d_set);
let p_below = Self::find((i + 1) * m + j, &mut d_set);
if p_curr == p_below {
return true;
}
// Otherwise, union the two cells
Self::union(p_curr, p_below, &mut d_set);
}
// Same to the right cell
if j + 1 < m && grid[i][j + 1] == grid[i][j] {
let p_curr = Self::find(i * m + j, &mut d_set);
let p_right = Self::find(i * m + (j + 1), &mut d_set);
if p_curr == p_right {
return true;
}
// Otherwise, union the two cells
Self::union(p_curr, p_right, &mut d_set);
}
}
}

false
}

#[allow(dead_code)]
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
if d_set[x] != x {
d_set[x] = Self::find(d_set[x], d_set);
}
d_set[x]
}

#[allow(dead_code)]
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
let p_x = Self::find(x, d_set);
let p_y = Self::find(y, d_set);
d_set[p_x] = p_y;
}
}