Facebook | Phone Screen | Walls and Gates

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2016-10-23Added By Anonymous
Source: Link

Company: Meta (Facebook)
Role: unknown
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

https://leetcode.com/problems/walls-and-gates/ (premium)

Given a maze with cells being: gates, walls or empty spaces.

Example:

Input:
_ W G _
_ _ _ W
_ W _ W
G W _ _

Output:
3 W G 1
2 2 1 W
1 W 2 W
G W 3 4

Fill the empty spaces with the number of steps to the closest gate. Allowed steps: UP, RIGHT, LEFT & DOWN

My solution

I think we could model this problem to a graph, from the input maze we could build a graph where each spot in the maze could at most have 4 edges (cause there are 4 legal moves only), if there's a wall we don't add an edge.
After building this graph with maze^2 vertices, we could run Floyd-Warshall algorithm to find all source/dist shortest path, then we build the result from this information.

I'm not sure if this is the optimal solution for this problem, I solved it recursively (brute-force) in the interview.

Steps:

adjMatrix = buildAjdsMatrixFromMaze(maze)
floyd-warshall(adjMatrix)
minCost = buildMinCost(adjMatrix)

Actual code

private boolean isSpace(int i) {
		return i == 0;
	}
	
private boolean isGate(int i) {
	return i == 1;
}

private boolean isWall(int i) {
	return i == 2;
}


class GatePosition {
	int x, y;
	public GatePosition(int x, int y) {
		this.x = x;
		this.y = y;
	}
}

public void buildAndProcessMaze() {
	int[][] maze = new int[][] {{0, 2, 1, 0},
								{0, 0, 0, 2},
								{0, 2, 0, 2},
								{1, 2, 0, 0}};
	int size = maze.length * maze.length;
			int max = (int) Math.pow(size, 2) + 1;
	int[][] matrix = new int[size][size];
	for(int i = 0; i < matrix.length; i++)
		matrix[i][i] = max;
	ArrayList<GatePosition> gates = new ArrayList<>();
	// build graph out of this maze
	// each vertex in the graph could have at max 4 edges cause we can move up, down, left, right
	// build graph including only spaces and gates
	for(int i = 0; i < maze.length; i++) {
		for(int j = 0; j < maze[0].length; j++) {
			// translate into the position in adj matrix
			int u = maze.length * i + j;
			int v;

			// initially fill the row with infinity, this to prepare for Floyd-Warshall algorithm
			Arrays.fill(matrix[u], max);
			// if wall, then fill its column infinity
			if(isWall(maze[i][j])) {
				for(int k = 0; k < matrix.length; k++)
					matrix[k][u] = max;
			}
			// spaces & gates
			// no self loops
			else {
				// save gates positions
				if(isGate(maze[i][j]))
					gates.add(new GatePosition(i, j));

				// check top spot
				if(i - 1 >= 0 && (isSpace(maze[i - 1][j]) || isGate(maze[i - 1][j]))) {
					v = maze.length * (i - 1) + j;
					matrix[u][v] = 1;
				}
				// check right spot
				if(j + 1 < maze[0].length && (isSpace(maze[i][j + 1]) || isGate(maze[i][j + 1]))) {
					v = maze.length * i + j + 1;
					matrix[u][v] = 1;
				}
				// check bottom spot
				if(i + 1 < maze.length && (isSpace(maze[i + 1][j]) || isGate(maze[i + 1][j]))) {
					v = maze.length * (i + 1) + j;
					matrix[u][v] = 1;
				}
				// check left spot
				if(j - 1 >= 0 && (isSpace(maze[i][j - 1]) || isGate(maze[i][j - 1]))) {
					v = maze.length * i + j - 1;
					matrix[u][v] = 1;
				}
			}
		}
	}

	// Floyd-Warshall algorithm
	for(int k = 0; k < matrix.length; k++) {
		for(int i = 0; i < matrix.length; i++) {
			for(int j = 0; j < matrix.length; j++) {
				int viaK = matrix[i][k] + matrix[k][j];
				matrix[i][j] = Math.min(viaK, matrix[i][j]);
			}
		}
	}

	// calculate min steps
	int[][] minSteps = new int[maze.length][maze[0].length]; 
	for(int i = 0; i < minSteps.length; i++)
		Arrays.fill(minSteps[i], max);
	for(GatePosition gate : gates) {
		// get gate column in adj matrix
		int g = gate.x * maze.length + gate.y;
		// for each spot in maze calculate min steps
		// if wall then it's infinity
		// if gate then it's 0
		// if space then it's the min of what we have now or distance to current gate
		for(int i = 0; i < maze.length; i++) {
			for(int j = 0; j < maze[0].length; j++) {
				if(isWall(maze[i][j]))
					minSteps[i][j] = max;
				if(isGate(maze[i][j]))
					minSteps[i][j] = 0;
				if(isSpace(maze[i][j])) {
					// get row in matrix
					int u = maze.length * i + j;
					minSteps[i][j] = Math.min(matrix[u][g], minSteps[i][j]);
				}
			}
		}
	}

	// print
	for(int i = 0; i < minSteps.length; i++) {
		for(int j = 0; j < minSteps[0].length; j++)
			System.out.print(minSteps[i][j] == max ? "m " : minSteps[i][j] + " ");
		System.out.println();
	}
}
Comments