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.
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 blockhouses 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 map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
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.
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 blockhouses 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 map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
Input
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.
Output
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
5 1 5 2 4本题题意是在给定的地方放置士兵,士兵能横或竖着打枪,.表示空地x表示有墙可以拦住子弹,也就是说士兵不能放一排或一竖列除非有墙挡着问最多放几个士兵。这是一道深搜题和n皇后问题类似,但难度有所提升,因为n皇后只用一个数组,分别存第几层放在第几个位置就可以而且返回后不用重置但本题要返回后,将上次造成的改变恢复应此我定义了两个函数,一个进行标记,一个恢复上次标记前的状态#include<iostream> #include<stdio.h> #include<queue> #include<cstring> #include<string> #include<algorithm> #include<map> #include<math.h> using namespace std; int b[150][150];//标记数组标记的是被影响的空地 char a[150][150];//存地形 int n; int s; int ma; int sq(int t,int s,int k)//恢复上次标记的数组,t,s表示上次放士兵的位置k表示上次放的是第几个士兵 { int i,j; for(i=s;i<n;i++)//因为有墙所以从t,s为中心向上,向下,向左向右清除标记遇到墙停止(其实清除时可以将标记数组从前到后进行清除,但我不想写是直接复制的标记函数,只改了一些条件就可以了) { if(a[t][i]=='.'&&b[t][i]==k) b[t][i]=0; if(a[t][i]=='X') break; } for(i=s;i>=0;i--) { if(a[t][i]=='.'&&b[t][i]==k) b[t][i]=0; if(a[t][i]=='X') break; } for(i=t;i<n;i++) { if(a[i][s]=='.'&&b[i][s]==k) b[i][s]=0; if(a[i][s]=='X') break; } for(i=t;i>=0;i--) { if(a[i][s]=='.'&&b[i][s]==k) b[t][i]=0; if(a[i][s]=='X') break; } return 0; } int sp(int t,int s,int k)//标记数组,t,s表示放士兵的位置k表示放的是第几个士兵 { int i,j; for(i=s;i<n;i++)//因为有墙所以从t,s为中心向上,向下,向左向右标记遇到墙停止 { if(a[t][i]=='.'&&b[t][i]==0)//表示没有被标记过 b[t][i]=k; if(a[t][i]=='X') break; } for(i=s;i>=0;i--) { if(a[t][i]=='.'&&b[t][i]==0) b[t][i]=k; if(a[t][i]=='X') break; } for(i=t;i<n;i++) { if(a[i][s]=='.'&&b[i][s]==0) b[i][s]=k; if(a[i][s]=='X') break; } for(i=t;i>=0;i--) { if(a[i][s]=='.'&&b[i][s]==0) b[t][i]=k; if(a[i][s]=='X') break; } return 0; } void ss(int t,int k) { int j; for(int i=t;i<n;i++) { if(i==t) j=k; else j=0; for(;j<n;j++) { if(a[i][j]=='.'&&b[i][j]==0)//表示是空地,而且没有被标记过,可以放士兵 { s++,b[i][j]=s,sp(i,j,s); ss(i,j); sq(i,j,s); s--; } } } ma=max(ma,s); if(s!=0)return; } int main() { while(scanf("%d",&n)) { if(n==0) break; scanf("\n"); memset(b,0,sizeof(b)); int i; for(i=0;i<n;i++) { gets(a[i]); } s=0; ma=0; ss(0,0); printf("%d\n",ma); } }