Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Valid Path Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Valid Path

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

You have an n*m matrix with start position at (0, 0) and end position at (n, m).

There are c circles with centres inside the matrix, each with radius r. You are given the centre coordinates (x, y) of each of those circles.

Note: You can move from any point to any of its 8 adjacent neighbours.

Find if there is a way to travel from start to end without touching any of these circles

Approach

Make a matrix of size n*m and initialize every cell to 0. Now find the cells which are not reachable by traversing the matrix and comparing each cell with all the circles and mark those cells as -1. Perform a BFS on the rest of the cells to check whether a path is possible from start to end.

Analysis

  • Time Complexity: O(n * m * c)
  • Auxiliary Space Complexity: O(n * m)

Implementation

C++
/* This is the Circle class definition
class Circle {
public:
    int x, y; // Co-ordinates of center
    Circle(int x, int y) {
        this->x = x;
        this->y = y;
    }
};
*/
bool distance(int x1, int x2, int y1, int y2, int r) {
	int dx = abs(x2 - x1), dy = abs(y2 - y1);
	dx *= dx;
	dy *= dy;
	if (sqrt (dx + dy) <= r) {
		return true;
	}
	return false;
}
bool hasValidPath(int n, int m, int r, vector<Circle*> &circles) {
    vector<vector<int>> possiblePath (n + 1, vector<int> (m + 1, 0));
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= m; j++) {
			for(int k = 0; k < circles.size(); k++) {
				if (distance(i, circles[k]->x, j, circles[k]->y, r)) {
					possiblePath[i][j] = - 1;
				}
			}
		}
	}
	if (possiblePath[0][0] == - 1) {
		return false;
	}
	queue<pair<int,int>> reachableNodes;
	reachableNodes.push({0, 0});
	possiblePath[0][0] = 1;
	while(!reachableNodes.empty()) {
		pair<int, int> currNode = reachableNodes.front();
		int x = currNode.first;
		int y = currNode.second;
		reachableNodes.pop();
		for(int i = max(0, x - 1); i <= min(n, x + 1); i++) {
			for (int j = max(0, y - 1); j <= min(m, y + 1); j++) {
				if(possiblePath[i][j] == 0) {
					possiblePath[i][j] = 1;
					reachableNodes.push({i, j});
				}
			}
		}
	}
	if (possiblePath[n][m] == 1) {
		return true;
	}
	return false;
}
Java
/* This is the Circle class definition
class Circle {
	public int x, y; // Co-ordinates of center
    public Circle(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
*/

class Solution {
	class Pair {
		int first, second;
		Pair(int x, int y) {
			first = x;
			second = y;
		}
	}
	boolean distance(int x1, int x2, int y1, int y2, int r) {
		int dx = Math.abs(x2 - x1), dy = Math.abs(y2 - y1);
		dx *= dx;
		dy *= dy;
		if (Math.sqrt (dx + dy) <= r) {
			return true;
		}
		return false;
	}
	boolean hasValidPath(int n, int m, int r, Circle[] circles) {
        int[][] possiblePath = new int[n + 1][m + 1];
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= m; j++) {
				for(int k = 0; k < circles.length; k++) {
					if (distance(i, circles[k].x, j, circles[k].y, r)) {
						possiblePath[i][j] = - 1;
					}
				}
			}
		}
		if (possiblePath[0][0] == - 1) {
			return false;
		}
		Queue<Pair> reachableNodes = new LinkedList<Pair>();
		reachableNodes.add(new Pair(0, 0));
		possiblePath[0][0] = 1;
		while(!reachableNodes.isEmpty()) {
			Pair currNode = reachableNodes.poll();
			int x = currNode.first;
			int y = currNode.second;
			for(int i = Math.max(0, x - 1); i <= Math.min(n, x + 1); i++) {
				for (int j = Math.max(0, y - 1); j <= Math.min(m, y + 1); j++) {
					if(possiblePath[i][j] == 0) {
						possiblePath[i][j] = 1;
						reachableNodes.add(new Pair(i, j));
					}
				}
			}
			
		}
		if (possiblePath[n][m] == 1) {
			return true;
		}
		return false;
	}
}
Related Content
Adjacency List to Adjacency Matrix
Adjacency Matrix to Adjacency List
BFS of a Graph
Capture Surrounded Regions
Clone a Graph
DFS of an Acyclic Graph
DFS of a Cyclic Graph
Edges to Adjacency List
Flood Fill Image
Knight's Journey On A Chessboard
M-Coloring Problem
Number of Islands
Stepping Numbers
Word Search Board
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet