A

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.

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:

5
1
5
2
4

比八皇后难一些,多了墙,但是其实类似后面的点只要考虑左边和上面就好了,遇上放过的不能放,搜完记得改回去

#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;
typedef 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
",Max);
	}
}
原文地址:https://www.cnblogs.com/wzl19981116/p/9385311.html