Diagonals
Time limit8sMemory limit1024 MB
Fill an n by n grid with diagonals so that given intersection counts match and no cycle of diagonals forms.
- Level
Medium6 of 10
- Topics
- Backtracking, Graph, Union-find, Brute force
- Solved
- No attempts yet
Problem
Diagonals is a pencil puzzle played on a square grid. The player must draw one diagonal line in every cell of the grid, corner to corner, either from top left to bottom right or from bottom left to top right. Two constraints apply:
- Some intersections of gridlines have an integer from to inclusive written on them, which is the exact number of diagonals that must touch that point.
- No set of diagonals may form a loop of any size or shape.
The following is a example together with its unique solution:

Given the numbers at the intersections of a grid, solve the puzzle.
Input
The first line of input contains an integer (), the size of the grid.
Each of the next lines contains a string (, ). These are the intersections of the grid, and '+' means there is no number at that intersection.
The input data is such that the puzzle has exactly one solution.
Output
Output exactly lines, each with exactly characters, representing the solution to the puzzle. Each character must be either '/' or '\'.
Sample 1 corresponds to the example in the problem description.