Suppose that we had a square city with straight streets. A map of a city are a square board with n rows and n columns, each representing a street or a piece of wall.
A blockhouse is a small castle it has four openings through which to shoot. The four openings is facing north, East, south, and West, respectively. There'll be a machine gun shooting through each opening.
Here we assume this a bullet is so powerful the it can run across any distance and destroy a blockhouse on its it. On the other hand, a wall are so strongly built that can stop the bullets.
The goal is-to-place as many blockhouses-a city as possible so, no, and can destroy each of the other. A configuration of blockhouses is legal provided that no, blockhouses is on the same horizontal row or vertical column In a map unless there are at least one wall separating them. In this problem we'll consider small square cities (at most 4×4) that contain walls through which bullets cannot run thr Ough.
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 PI Ctures Show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; The second picture shows one-to-do it, but there is 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.
The input file contains one or more maps descriptions, followed by a line containing the number 0 that signals the end of T He file. Each map description begins with a line containing a positive an integer n that's the size of the city; N would be is at most 4. The next n lines each describe one row of the map, with a '. ' indicating an open space and a uppercase ' X ' indicating a W All. There is no spaces in the input file.
For each test case, output one line containing the maximum number of blockhouses so 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
More difficult than the eight queens, more walls, but in fact, similar to the point in the back as long as the left and the top is good, meet the miss can not put, search, remember to change back
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <iomanip> #include < cmath> #include <float.h> #include <string.h> #include <algorithm> #define SF scanf#define PF printf #define PB push_back#define mm (x,b) memset ((x), (b), sizeof (x)) #include <vector> #include <map> #define REP (i , A,n) for (int. i=a;i<n;i++) #define PER (i,a,n) for (int i=a;i>=n;i--) typedef long Long ll;typedef long double ld;type def double db;const ll Mod=1e12+100;const db e=exp (1); using namespace Std;const double Pi=acos ( -1.0); int N,max=0;char A[6] [6];int Check (int x,int y) {per (i,y-1,0) {if (a[x][i]== ' x ') return 0; else if (a[x][i]== ' X ') break; } per (i,x-1,0) {if (a[i][y]== ' x ') return 0; else if (a[i][y]== ' X ') break; } return 1;} void dfs (int deep,int sum) {int x=deep/n,y=deep%n; if (deep>=n*n) {Max=max (max,sum); return; } if (a[x][y]== '. ') {if (check (x, y)) { a[x][y]= ' x '; DFS (DEEP+1,SUM+1); A[x][y]= '. '; }} dfs (deep+1,sum);} int main () {while (1) {max=0; SF ("%d", &n); if (!n) return 0; MM (a,0); Rep (i,0,n) SF ("%s", &a[i]); DFS (0,0); PF ("%d\n", Max); }}
A-fire Net