Fire Net
Problem Description
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.
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
————————————————————————————————————————————————————————————
题目大题意思是在迷宫里放若干个棋子,每两个子不能在同行会同列,除非中间有墙挡着,求最大可以放多少个子。、
方法一:由于数据不大,直接dfs枚举即可
方法二:按行列给空格标号,然后最大二分图匹配
方式一:DFS爆搜
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int n,maxnum;
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
char mp[6][6];
bool cheak(int x, int y)
{
int flag = 0;
if (x >= 1 && x <= n&&y >= 1 && y <= n&&mp[x][y] == '.')
{
flag = 1;
for (int i = x; i >= 1; i--)
{
if (mp[i][y] == '#')
flag = 0;
if (mp[i][y] == 'X')
break;
}
for (int i = y; i >= 1; i--)
{
if (mp[x][i] == '#')
flag = 0;
if (mp[x][i] == 'X')
break;
}
}
return flag;
}
void dfs(int x,int y,int num)
{
if (x == n && y == n)
{
if (cheak(x, y))
{
num++;
}
if (num > maxnum)
maxnum = num;
return;
} if (cheak(x, y))
{
mp[x][y] = '#';
if (y < n)
dfs(x, y + 1, num+1);
else
dfs(x + 1, 1, num+1); mp[x][y] = '.';
}
if (y < n)
dfs(x, y + 1, num);
else
dfs(x + 1, 1, num);
return;
}
int main()
{
while (~scanf(" %d",&n)&&n)
{
for (int i = 1; i <= n;i++)
for (int j = 1; j <= n; j++)
{
scanf(" %c", &mp[i][j]);
}
maxnum = 0;
dfs(1, 1, 0);
printf("%d\n", maxnum); }
return 0;
}
方式二:二分图匹配
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
const int MAXN=1005;
int uN,vN; //u,v数目
int g[MAXN][MAXN];//编号是0~n-1的
int linker[MAXN];
bool used[MAXN]; bool dfs(int u)
{
int v;
for(v=0; v<vN; v++)
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
return false;
} int hungary()
{
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=0; u<uN; u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
} int main()
{
char s[10][10];
int x[10][10],y[10][10];
int n,n1,n2;
while(~scanf("%d",&n)&&n)
{
memset(x,0,sizeof x);
memset(y,0,sizeof y);
for(int i=0; i<n; i++)
{
scanf("%s",&s[i]);
}
uN=vN=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n;)
{
while(s[i][j]=='X'&&j<n)
{
x[i][j]=-1;
j++;
} while(s[i][j]!='X'&&j<n)
{
x[i][j]=uN;
j++;
}
uN++;
}
} for(int j=0; j<n; j++)
{
for(int i=0; i<n;)
{
while(s[i][j]=='X'&&i<n)
{
y[i][j]=-1;
i++;
} while(s[i][j]!='X'&&i<n)
{
y[i][j]=vN;
i++;
}
vN++;
}
}
memset(g,0,sizeof g);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
if(x[i][j]!=-1&&y[i][j]!=-1)
g[x[i][j]][y[i][j]]=1;
}
printf("%d\n",hungary()); }
return 0;
}