Gallery Ceiling Sensors
An art gallery is fitting motion sensors to a ceiling of n × n panels. Each sensor beams along its row, its column and both diagonals to the walls, and two sensors on one beam set each other off. Some panels are glass skylights: nothing mounts on glass, but beams pass through it. The gallery needs exactly n sensors and wants to know how many layouts are possible.
Your task
Write countLayouts(ceiling). ceiling is a list of n strings of length n; ceiling[r][c] is '.' for a normal panel and '#' for a skylight. Return the number of ways to place n sensors on normal panels so that no two share a row, a column or a diagonal.
Two layouts are different if some panel has a sensor in one and not in the other. Skylights do not block beams. If no layout exists, return 0.
countLayouts(ceiling: string[]) → int
Examples
ceiling = ["....", "....", "....", "...."]
2
Writing each row's sensor column, the two layouts are 1, 3, 0, 2 and its mirror image 2, 0, 3, 1. Every other try shares a column or diagonal.
ceiling = [".#..", "....", "....", "...."]
1
The skylight at row 0, column 1 rules out 1, 3, 0, 2, which needs a sensor there. The mirror 2, 0, 3, 1 survives.
ceiling = ["#"]
0
The only panel is a skylight, so no sensor can be placed and the answer is 0.
Constraints
- 1 ≤ n ≤ 10
- Every row of ceiling has length n.
- Each character is '.' or '#'.
Start with brute force
Choosing any n of the n² panels means about 17 trillion choices on a 10 × 10 ceiling. Even one sensor per row and column (every column ordering) is 10! = 3,628,800 full layouts, each checked at the end, though most clash within the first two or three rows.
Hints
0 of 4 shown- Hint 1
On a 4 × 4 ceiling with 4 sensors, how many sensors can one row hold? So how many does each row get?
- Hint 2
Exactly one sensor per row, so pick a column for row 0, then row 1, and so on. A column is allowed only if the panel is not glass and no placed sensor watches it.
- Hint 3
A sensor at (r, c) watches column c, the diagonal where row − col = r − c, and the one where row + col = r + c. Keep three boolean lists (columns, row − col, row + col) so each check is instant. Shift row − col by n − 1 so it is never negative.
- Hint 4
Write
count(row): ifrow == n, return 1. Otherwise sumcount(row + 1)over every allowed column, marking the column and both diagonals before the call and clearing them after. A row with no allowed panel returns 0 and the search backs up.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Each row holds exactly one sensor, so you only choose a column per row. A panel's diagonals are identified by row − col and row + col, so "is this panel watched?" is three lookups.
Trace the open 4 × 4 ceiling, writing the chosen columns so far:
- Put row 0's sensor in column 0. Row 1 can then use column 2 or 3. With
0, 2, all of row 2 is watched: dead end. With0, 3, row 2 takes column 1, then all of row 3 is watched: dead end. So column 0 gives 0 layouts. - Put row 0's sensor in column 1. Row 1 can then only use column 3, row 2 only column 0, and row 3 only column 2, so this gives 1 layout.
- Column 2 mirrors column 1 and gives 1 layout, and column 3 mirrors column 0 and gives 0.
The total is 2. A common bug is checking only one diagonal direction, which on the open 4 × 4 ceiling would also count the layout 3, 2, 1, 0, where all sensors sit on one diagonal. Another is forgetting to clear the marks after the recursive call, so later branches see sensors that are no longer there.
function countLayouts(ceiling) {
const n = ceiling.length;
const colUsed = new Array(n).fill(false);
const downUsed = new Array(2 * n - 1).fill(false); // same row - col
const upUsed = new Array(2 * n - 1).fill(false); // same row + col
function count(row) {
if (row === n) return 1; // one sensor in every row
let total = 0;
for (let col = 0; col < n; col++) {
const d = row - col + n - 1, u = row + col;
if (ceiling[row][col] === '#' || colUsed[col] || downUsed[d] || upUsed[u]) continue;
colUsed[col] = downUsed[d] = upUsed[u] = true;
total += count(row + 1);
colUsed[col] = downUsed[d] = upUsed[u] = false;
}
return total;
}
return count(0);
}def count_layouts(ceiling):
n = len(ceiling)
col_used = [False] * n
down_used = [False] * (2 * n - 1) # diagonals with the same row - col
up_used = [False] * (2 * n - 1) # diagonals with the same row + col
def count(row):
if row == n:
return 1 # one sensor in every row
total = 0
for col in range(n):
d, u = row - col + n - 1, row + col
if ceiling[row][col] == '#' or col_used[col] or down_used[d] or up_used[u]:
continue
col_used[col] = down_used[d] = up_used[u] = True
total += count(row + 1)
col_used[col] = down_used[d] = up_used[u] = False
return total
return count(0)Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.