← 返回 google 的题目列表Grid Flower Placement with Row/Column Uniqueness and House Adjacency Constraints
类型:online_judge
Given an N x N grid:
'H' denotes a house
'0' denotes an empty cell
Place flowers 'F' on some cells such that:
Each row contains exactly one 'F'.
Each column contains exactly one 'F'.
For every house 'H', among its 4-directional neighbors (up/down/left/right), there must be exactly one flower 'F' (consider only neighbors within bounds).
Return any valid resulting N x N grid containing 'H', '0', and 'F'. If no solution exists, return/output empty (e.g., empty grid or print IMPOSSIBLE).
Example input:
H000
H000
000H
0000
Return any valid grid if one exists.
Constraints:
1 <= N <= ? (not specified in the post; treat as a backtracking/DFS problem)
Note: You can only place 'F' on cells that are originally '0' (do not overwrite houses).
Example
Input
4
H000
H000
000H
0000
Output
<any valid grid or IMPOSSIBLE>