-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_1036.kt
42 lines (38 loc) · 1.45 KB
/
LC_1036.kt
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
class Solution {
private val MAX_VISIT = 20000
fun isEscapePossible(blocked: Array<IntArray>, source: IntArray, target: IntArray): Boolean {
val blockedSet: MutableSet<String> = HashSet()
for (point in blocked) {
blockedSet.add(getKey(point))
}
return canVisit(blockedSet, source, getKey(target)) && canVisit(blockedSet, target, getKey(source))
}
private fun getKey(point: IntArray): String {
return point[0].toString() + "," + point[1]
}
private fun canVisit(blocked: Set<String>, source: IntArray, targetKey: String): Boolean {
val visited: MutableSet<String> = HashSet()
dfs(blocked, source, targetKey, visited)
return visited.size >= MAX_VISIT || visited.contains(targetKey)
}
private fun dfs(blocked: Set<String>, curr: IntArray, targetKey: String, visited: MutableSet<String>) {
val (i,j) = curr
if (i < 0 || j < 0 || i >= 1e6 || j >= 1e6) {
return
}
val key = getKey(curr)
if (blocked.contains(key)) {
return
}
if (visited.size >= MAX_VISIT || visited.contains(targetKey)) {
return
}
if (visited.contains(key)) {
return
}
visited.add(key)
for (next in arrayOf(intArrayOf(i - 1, j), intArrayOf(i + 1, j), intArrayOf(i, j - 1), intArrayOf(i, j + 1))) {
dfs(blocked, next, targetKey, visited)
}
}
}