Queen Kingdom
Time limit2sMemory limit128 MB
On an n by n board with pillars blocking queens' attacks, find the maximum number of non-attacking queens and the number of placements attaining it.
- Level
Medium7 of 10
- Topics
- Backtracking, Brute force, Bit manipulation, DFS
- Solved
- No attempts yet
Problem
A game designer wants to build a modified chessboard. On some squares stand pillars: a pillar blocks a piece from being placed on that square, and it also blocks any attack from passing through it.
Before sending the design to a manufacturer, the designer wants to know, for each board layout, the maximum number of queens that can be placed so that no two attack each other. As on a normal chessboard, a queen attacks along its row, its column, and both diagonals, extending outward until it reaches the edge of the board — but on this board an attack also stops the moment it reaches a pillar. The designer also wants to know how many distinct placements achieve that maximum.
Input
The input contains several scenarios. Each scenario begins with a line holding a single integer (). The input ends with a line containing , which is not processed.
The next lines each contain characters describing the rows of the board. A 0 is an open square and a 1 is a pillar.
Output
For each scenario, print one line with two integers separated by a single space: the maximum number of queens that can be placed so that no two attack each other, followed by the number of distinct placements that achieve this maximum.