This page is still under construction.

Parts of this page are still being built. What you see may change.

Queen Kingdom

Time limit2sMemory limit128 MB

Summary
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 n×nn \times n 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 nn (1≤n≤101 \le n \le 10). The input ends with a line containing 00, which is not processed.

The next nn lines each contain nn 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.

Examples1

  1. Example 1

    Input
    3
    010
    111
    000
    6
    000000
    000000
    000000
    000000
    000000
    000000
    6
    000010
    001110
    111011
    010110
    011010
    010010
    0
    
    Expected output
    3 3
    6 4
    7 270