Skip to content

Commit 703e9c9

Browse files
committed
feat: add solution to lc problem: No.3235
1 parent 39584f5 commit 703e9c9

File tree

4 files changed

+265
-0
lines changed

4 files changed

+265
-0
lines changed

solution/3200-3299/3235.Check if the Rectangle Corner Is Reachable/README.md

+90
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,96 @@ function canReachCorner(xCorner: number, yCorner: number, circles: number[][]):
416416
}
417417
```
418418

419+
#### Rust
420+
421+
```rust
422+
impl Solution {
423+
pub fn can_reach_corner(x_corner: i32, y_corner: i32, circles: Vec<Vec<i32>>) -> bool {
424+
let n = circles.len();
425+
let mut vis = vec![false; n];
426+
427+
let in_circle = |x: i64, y: i64, cx: i64, cy: i64, r: i64| -> bool {
428+
(x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r
429+
};
430+
431+
let cross_left_top = |cx: i64, cy: i64, r: i64| -> bool {
432+
let a = cx.abs() <= r && (cy >= 0 && cy <= y_corner as i64);
433+
let b = (cy - y_corner as i64).abs() <= r && (cx >= 0 && cx <= x_corner as i64);
434+
a || b
435+
};
436+
437+
let cross_right_bottom = |cx: i64, cy: i64, r: i64| -> bool {
438+
let a = (cx - x_corner as i64).abs() <= r && (cy >= 0 && cy <= y_corner as i64);
439+
let b = cy.abs() <= r && (cx >= 0 && cx <= x_corner as i64);
440+
a || b
441+
};
442+
fn dfs(
443+
circles: &Vec<Vec<i32>>,
444+
vis: &mut Vec<bool>,
445+
i: usize,
446+
x_corner: i32,
447+
y_corner: i32,
448+
cross_right_bottom: &dyn Fn(i64, i64, i64) -> bool,
449+
) -> bool {
450+
let c = &circles[i];
451+
let (x1, y1, r1) = (c[0] as i64, c[1] as i64, c[2] as i64);
452+
453+
if cross_right_bottom(x1, y1, r1) {
454+
return true;
455+
}
456+
457+
vis[i] = true;
458+
459+
for j in 0..circles.len() {
460+
if vis[j] {
461+
continue;
462+
}
463+
464+
let c2 = &circles[j];
465+
let (x2, y2, r2) = (c2[0] as i64, c2[1] as i64, c2[2] as i64);
466+
467+
if (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) > (r1 + r2) * (r1 + r2) {
468+
continue;
469+
}
470+
471+
if x1 * r2 + x2 * r1 < (r1 + r2) * x_corner as i64
472+
&& y1 * r2 + y2 * r1 < (r1 + r2) * y_corner as i64
473+
&& dfs(circles, vis, j, x_corner, y_corner, cross_right_bottom)
474+
{
475+
return true;
476+
}
477+
}
478+
false
479+
}
480+
481+
for i in 0..n {
482+
let c = &circles[i];
483+
let (x, y, r) = (c[0] as i64, c[1] as i64, c[2] as i64);
484+
485+
if in_circle(0, 0, x, y, r) || in_circle(x_corner as i64, y_corner as i64, x, y, r) {
486+
return false;
487+
}
488+
489+
if !vis[i]
490+
&& cross_left_top(x, y, r)
491+
&& dfs(
492+
&circles,
493+
&mut vis,
494+
i,
495+
x_corner,
496+
y_corner,
497+
&cross_right_bottom,
498+
)
499+
{
500+
return false;
501+
}
502+
}
503+
504+
true
505+
}
506+
}
507+
```
508+
419509
<!-- tabs:end -->
420510

421511
<!-- solution:end -->

solution/3200-3299/3235.Check if the Rectangle Corner Is Reachable/README_EN.md

+90
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,96 @@ function canReachCorner(xCorner: number, yCorner: number, circles: number[][]):
414414
}
415415
```
416416

417+
#### Rust
418+
419+
```rust
420+
impl Solution {
421+
pub fn can_reach_corner(x_corner: i32, y_corner: i32, circles: Vec<Vec<i32>>) -> bool {
422+
let n = circles.len();
423+
let mut vis = vec![false; n];
424+
425+
let in_circle = |x: i64, y: i64, cx: i64, cy: i64, r: i64| -> bool {
426+
(x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r
427+
};
428+
429+
let cross_left_top = |cx: i64, cy: i64, r: i64| -> bool {
430+
let a = cx.abs() <= r && (cy >= 0 && cy <= y_corner as i64);
431+
let b = (cy - y_corner as i64).abs() <= r && (cx >= 0 && cx <= x_corner as i64);
432+
a || b
433+
};
434+
435+
let cross_right_bottom = |cx: i64, cy: i64, r: i64| -> bool {
436+
let a = (cx - x_corner as i64).abs() <= r && (cy >= 0 && cy <= y_corner as i64);
437+
let b = cy.abs() <= r && (cx >= 0 && cx <= x_corner as i64);
438+
a || b
439+
};
440+
fn dfs(
441+
circles: &Vec<Vec<i32>>,
442+
vis: &mut Vec<bool>,
443+
i: usize,
444+
x_corner: i32,
445+
y_corner: i32,
446+
cross_right_bottom: &dyn Fn(i64, i64, i64) -> bool,
447+
) -> bool {
448+
let c = &circles[i];
449+
let (x1, y1, r1) = (c[0] as i64, c[1] as i64, c[2] as i64);
450+
451+
if cross_right_bottom(x1, y1, r1) {
452+
return true;
453+
}
454+
455+
vis[i] = true;
456+
457+
for j in 0..circles.len() {
458+
if vis[j] {
459+
continue;
460+
}
461+
462+
let c2 = &circles[j];
463+
let (x2, y2, r2) = (c2[0] as i64, c2[1] as i64, c2[2] as i64);
464+
465+
if (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) > (r1 + r2) * (r1 + r2) {
466+
continue;
467+
}
468+
469+
if x1 * r2 + x2 * r1 < (r1 + r2) * x_corner as i64
470+
&& y1 * r2 + y2 * r1 < (r1 + r2) * y_corner as i64
471+
&& dfs(circles, vis, j, x_corner, y_corner, cross_right_bottom)
472+
{
473+
return true;
474+
}
475+
}
476+
false
477+
}
478+
479+
for i in 0..n {
480+
let c = &circles[i];
481+
let (x, y, r) = (c[0] as i64, c[1] as i64, c[2] as i64);
482+
483+
if in_circle(0, 0, x, y, r) || in_circle(x_corner as i64, y_corner as i64, x, y, r) {
484+
return false;
485+
}
486+
487+
if !vis[i]
488+
&& cross_left_top(x, y, r)
489+
&& dfs(
490+
&circles,
491+
&mut vis,
492+
i,
493+
x_corner,
494+
y_corner,
495+
&cross_right_bottom,
496+
)
497+
{
498+
return false;
499+
}
500+
}
501+
502+
true
503+
}
504+
}
505+
```
506+
417507
<!-- tabs:end -->
418508

419509
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
impl Solution {
2+
pub fn can_reach_corner(x_corner: i32, y_corner: i32, circles: Vec<Vec<i32>>) -> bool {
3+
let n = circles.len();
4+
let mut vis = vec![false; n];
5+
6+
let in_circle = |x: i64, y: i64, cx: i64, cy: i64, r: i64| -> bool {
7+
(x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r
8+
};
9+
10+
let cross_left_top = |cx: i64, cy: i64, r: i64| -> bool {
11+
let a = cx.abs() <= r && (cy >= 0 && cy <= y_corner as i64);
12+
let b = (cy - y_corner as i64).abs() <= r && (cx >= 0 && cx <= x_corner as i64);
13+
a || b
14+
};
15+
16+
let cross_right_bottom = |cx: i64, cy: i64, r: i64| -> bool {
17+
let a = (cx - x_corner as i64).abs() <= r && (cy >= 0 && cy <= y_corner as i64);
18+
let b = cy.abs() <= r && (cx >= 0 && cx <= x_corner as i64);
19+
a || b
20+
};
21+
fn dfs(
22+
circles: &Vec<Vec<i32>>,
23+
vis: &mut Vec<bool>,
24+
i: usize,
25+
x_corner: i32,
26+
y_corner: i32,
27+
cross_right_bottom: &dyn Fn(i64, i64, i64) -> bool,
28+
) -> bool {
29+
let c = &circles[i];
30+
let (x1, y1, r1) = (c[0] as i64, c[1] as i64, c[2] as i64);
31+
32+
if cross_right_bottom(x1, y1, r1) {
33+
return true;
34+
}
35+
36+
vis[i] = true;
37+
38+
for j in 0..circles.len() {
39+
if vis[j] {
40+
continue;
41+
}
42+
43+
let c2 = &circles[j];
44+
let (x2, y2, r2) = (c2[0] as i64, c2[1] as i64, c2[2] as i64);
45+
46+
if (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) > (r1 + r2) * (r1 + r2) {
47+
continue;
48+
}
49+
50+
if x1 * r2 + x2 * r1 < (r1 + r2) * x_corner as i64
51+
&& y1 * r2 + y2 * r1 < (r1 + r2) * y_corner as i64
52+
&& dfs(circles, vis, j, x_corner, y_corner, cross_right_bottom)
53+
{
54+
return true;
55+
}
56+
}
57+
false
58+
}
59+
60+
for i in 0..n {
61+
let c = &circles[i];
62+
let (x, y, r) = (c[0] as i64, c[1] as i64, c[2] as i64);
63+
64+
if in_circle(0, 0, x, y, r) || in_circle(x_corner as i64, y_corner as i64, x, y, r) {
65+
return false;
66+
}
67+
68+
if !vis[i]
69+
&& cross_left_top(x, y, r)
70+
&& dfs(
71+
&circles,
72+
&mut vis,
73+
i,
74+
x_corner,
75+
y_corner,
76+
&cross_right_bottom,
77+
)
78+
{
79+
return false;
80+
}
81+
}
82+
83+
true
84+
}
85+
}
Loading

0 commit comments

Comments
 (0)