# Untitled

unknown
plain_text
a year ago
4.6 kB
1
Indexable
Never
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
/*Chess rook
In chess, the rook is a piece that can move any number of squares vertically or horizontally.

In this problem we will consider small chess boards (at most 4 ×4) that can also contain walls through which rooks cannot move.

The goal is to place as many rooks on a board as possible so that no two can capture each other.

A configuration of rooks is legal provided that no two rooks are on the same horizontal row or vertical column unless there is at least one wall separating them.

The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations.

For this board, the maximum number of rooks in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.

Your task is to write a program that, given a description of a board, calculates the maximum number of rooks that can be placed on the board in a legal configuration.

Input

First line of input is number of test cases. Each test case contains a board description.

Each board description begins with a line containing a positive integer N that is the size of the board; N will be at most 4.

The next N lines each describe one row of the board, with a ‘.’ indicating an open space and an uppercase ‘X’ indicating a wall. There are no spaces in the input file.

Output

For each test case, output one line containing the maximum number of rooks that can be placed on the board in a legal configuration.

Note that first output line of each test case should be Case #tn, where tn is the test case number.

Sample input

5

4

.X..

....

XX..

....

2

XX

.X

3

.X.

X.X

.X.

3

...

.XX

.XX

4

....

....

....

....

Sample output

Case #1

5

Case #2

1

Case #3

5

Case #4

2

Case #5

4
* */
public class ChessRook {
static {
try {
System.setIn(new FileInputStream("src/inp.txt"));
// System.setOut(new PrintStream("src/out.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

static int dir[][] = { { 0, -1 }, { 0, 1 }, { 1, 0 }, { -1, 0 } }, res;
static Scanner scanner = new Scanner(System.in);
static int[][] sqidx;

static void markdir(char[][] mat, int dirpos, int sx, int sy, int val, int n, int[][] vis, boolean inv) {
vis[sx][sy] = !inv ?
(vis[sx][sy]!=0 ? vis[sx][sy] : val):
(vis[sx][sy]!=val ? vis[sx][sy] : 0);
int nx = sx + dir[dirpos][0], ny = sy + dir[dirpos][1];
if (nx >= 0 && nx < n && ny >= 0 && ny < n && mat[nx][ny] != 'X')
markdir(mat, dirpos, nx, ny, val, n, vis, inv);
}

static void mark(int x, int y, int n, int[][] vis, char[][] mat, boolean inv) {
for (int dir = 0; dir < 4; ++dir)
markdir(mat, dir, x, y, sqidx[x][y], n, vis, inv);
}

static void run(int[][] vis, char[][] mat, int n, int x, int y, int cnt) {
if (x == n) {
res = Math.max(res, cnt);
return;
}

if (vis[x][y]==0 && mat[x][y] != 'X') {
mark(x, y, n, vis, mat, false);
run(vis, mat, n, x, y, cnt + 1);
mark(x, y, n, vis, mat, true);
}

if (y + 1 < n)
run(vis, mat, n, x, y + 1, cnt);
else
run(vis, mat, n, x + 1, 0, cnt);
}

static void exec(int test) {
/*
* rook dc phep di chuyen ngang/doc 1 ban co 4x4 chua nhung buc tuong
* rook ko the di den dat nhieu con rook nhat co the sao cho ko an dc
* nhau 1 config hop le khi ko co chung hang/cot, nhung co 1 buc tuong
* ngan cach dem so cau hinh hop le inp: kich thuoc cua ban co .: o
* trong, x: co chua buc tuong output: so luong rook toi da co the dat
* trong 1 ban co
*/
int n = Integer.parseInt(scanner.next());
char[][] mat = new char[n][n];

for (int i = 0; i < n; ++i) {
char[] tmp = scanner.next().toCharArray();
for (int j = 0; j < n; ++j)
mat[i][j] = tmp[j];
}

int cnt = 1;
sqidx = new int[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
sqidx[i][j] = cnt++;

res = 0;
int[][] vis = new int[n][n];
run(vis, mat, n, 0, 0, 0);

System.out.printf("Case #%d\n%d\n", test, res);
}

public static void main(String[] args) {
int t = Integer.parseInt(scanner.next());

for (int test = 1; test <= t; ++test)
exec(test);

scanner.close();
}
}