2
static final int limit = 1000000; // boundary check4
public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) {5
Set<String> blocks = new HashSet<>(); // HashSet to reduce the access time from O(N)-> O(1)7
for (int block[] : blocked)10
+ block[1]); // putting the blocked node into the HashSet to access it at O(1)12
return bfsRange(source, target, blocks) && bfsRange(target, source, blocks); // sector division14
/*checking for both the case that the source is blocked or the target is blocked15
*if one of them is blocked we return false16
*since it is not practical to traverse each node, it will provide us with TLE21
* shortest arc(displacement) of 1/4 sector of circel is a* 2^1/222
* Area of the triangular sector is 1/2 * ( r{base} * r{height} )23
* Number of minimum shell to cover to move ahed of sector or boundary if possible = 0 + 1 + 2 + 3 + ... + 199 = 19900 (A.P)26
public boolean bfsRange(30
blocks) { // we simply do bsf to check the circular quadrant 1/4th boundary of the sector33
new HashSet<>(); // visited hash set is so that we dont visit the visited cell again and the34
// access time is O(1)35
Queue<int[]> q = new LinkedList<>(); // as we use in BT37
q.offer(source); // adding the starting BFS node to the Queue40
source[0] + "-" + source[1]); // marking it as visited so that we dont traverse it again42
int count = 0; // number of node traverse total outside + inside43
while (!q.isEmpty()) { // r m* w a*44
int temp[] = q.poll(); // poling the node45
count += 1; // counting the number of node traverse47
int trav[][] = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}}; // Traversing in 4-Direction49
for (int direction[] : trav) {50
int i = temp[0] + direction[0];51
int j = temp[1] + direction[1];53
String key = (i + "-" + j);59
|| visited.contains(key)60
|| blocks.contains(key))61
continue; // base case 1)checking the index 2)We dont visit the blocked node 3) we dont62
// visit the visited node67
1]) // when we find the target within the boundary(same sector or the quadrand)68
// we just return true //best case saves a lot of time71
visited.add(key); // marking the node as visited and adding it to the Queue73
q.offer(new int[] {i, j}); // Expaning the search for path and adding the node75
if (count > 19900) // number of cell, crossing the boundary limit76
return true; // path exists from this node79
return false; // no path, to reach the node//boundary blocked us81
} // Please do Upvote, it helps a lot