Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.
For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
Sample input:
4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0
Sample output:
1
5
2
1 import java.util.*; 2 public class Main { 3 static int [][] traverse=new int[10][10]; 4 static int n,cout,maxNum; 5 public static int sign(int x,int y) { 6 for(int i=y;i>=0;i--){ 7 if(traverse[x][i]==1) 8 return 0; 9 if(traverse[x][i]==2) 10 break; 11 } 12 for(int i=y;i<n;i++){ 13 if(traverse[x][i]==1) 14 return 0; 15 if(traverse[x][i]==2) 16 break; 17 } 18 for(int i=x;i>=0;i--){ 19 if(traverse[i][y]==1) 20 return 0; 21 if(traverse[i][y]==2) 22 break; 23 } 24 for(int i=x;i<n;i++){ 25 if(traverse[i][y]==1) 26 return 0; 27 if(traverse[i][y]==2) 28 break; 29 } 30 return 1; 31 } 32 33 public static void dfs() { 34 if(cout>maxNum){ 35 maxNum=cout; 36 } 37 for(int k=0;k<n;k++){ 38 for(int j=0;j<n;j++){ 39 if(traverse[k][j]==0&&sign(k, j)==1){ 40 traverse[k][j]=1; 41 cout++; 42 dfs(); 43 traverse[k][j]=0; 44 cout--; 45 } 46 } 47 } 48 } 49 public static void main(String[] args) { 50 String city; 51 char[] cityStr=new char[10]; 52 Scanner scanner=new Scanner(System.in); 53 while((n=scanner.nextInt())!=0){ 54 maxNum=cout=0; 55 for(int i=0;i<n;i++){ 56 city=scanner.next(); 57 cityStr=city.toCharArray(); 58 for(int m=0;m<n;m++){ 59 traverse[i][m]=(cityStr[m]=='X'?2:0); 60 } 61 } 62 dfs(); 63 System.out.println(maxNum); 64 } 65 } 66 }