-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1036.cs
60 lines (50 loc) · 1.7 KB
/
Solution1036.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1036
{
/// <summary>
/// 1036. Escape a Large Maze - Hard
/// <a href="https://leetcode.com/problems/escape-a-large-maze</a>
/// </summary>
public bool IsEscapePossible(int[][] blocked, int[] source, int[] target)
{
int N = 1_000_000;
var blockedSet = new HashSet<(int, int)>();
foreach (var block in blocked)
{
blockedSet.Add((block[0], block[1]));
}
return IsEscapePossible(blockedSet, source, target, N) && IsEscapePossible(blockedSet, target, source, N);
}
private bool IsEscapePossible(HashSet<(int, int)> blocked, int[] source, int[] target, int N)
{
var visited = new HashSet<(int, int)>();
var queue = new Queue<(int, int)>();
queue.Enqueue((source[0], source[1]));
int[][] directions = [[0, 1], [0, -1], [1, 0], [-1, 0]];
while (queue.Count > 0)
{
var current = queue.Dequeue();
visited.Add(current);
if (current.Item1 == target[0] && current.Item2 == target[1])
{
return true;
}
foreach (var dir in directions)
{
int r = current.Item1 + dir[0], c = current.Item2 + dir[1];
if (r >= 0 && r < N && c >= 0 && c < N && !visited.Contains((r, c)) && !blocked.Contains((r, c)))
{
queue.Enqueue((r, c));
visited.Add((r, c));
}
}
if (visited.Count == 20000)
{
return true;
}
}
return false;
}
}