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.
4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0
5 1 5 2 4
题意:X表示一道墙,任意两个点不在同一行同一列,或者在同一行或同一列但中间隔一堵墙。求满足次条件的最多的点有多少。
思路:考虑当前点算还是不算,判断同行同列此点之前的点的情况,若前面在没遇到墙时已经放过点,则不能再把当前点算入了
后面和棋盘问题略微相似,只不过棋盘问题按行搜索,这题得从每个点考虑
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int n,Max;
char map[10][10];
int vis[10][10];
int Judge(int x,int y)
{
for(int i=x-1;i>=0;i--)
{
if(vis[i][y]==1) return 0;//前面有房 return 0;
if(map[i][y]=='X') break;//前面有X return 1;
}
for(int j=y-1;j>=0;j--)
{
if(vis[x][j]==1) return 0;
if(map[x][j]=='X') break;
}
return 1;
}
int sum=0;
void dfs(int k)
{
if(k==n*n)
{
Max=max(Max,sum);
return;
}
else
{
int x=k/n;
int y=k%n;
if(map[x][y]=='.'&&(Judge(x,y)==1))
{
vis[x][y]=1;
sum++;
dfs(k+1);
vis[x][y]=0;
sum--;
}
dfs(k+1);//不搜索第k个
}
}
int main()
{
while(~scanf("%d",&n)&&n)
{
Max=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
scanf("%s",map[i]);
dfs(0);
printf("%d\n",Max);
}
}