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.

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

————————————————————————————————————————————————————————————

题目大题意思是在迷宫里放若干个棋子,每两个子不能在同行会同列,除非中间有墙挡着,求最大可以放多少个子。、
方法一:由于数据不大,直接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;
}

HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏的更多相关文章

  1. HDU1181 变形课(DFS) 2016-07-24 13:31 73人阅读 评论(0) 收藏

    变形课 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒 ...

  2. HDU2212 DFS 2016-07-24 13:52 56人阅读 评论(0) 收藏

    DFS Problem Description A DFS(digital factorial sum) number is found by summing the factorial of eve ...

  3. HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏

    Red and Black Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  4. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  5. POJ2195 Going Home (最小费最大流||二分图最大权匹配) 2017-02-12 12:14 131人阅读 评论(0) 收藏

    Going Home Description On a grid map there are n little men and n houses. In each unit time, every l ...

  6. 二分图匹配 分类: ACM TYPE 2014-10-01 19:57 94人阅读 评论(0) 收藏

    #include<cstdio> #include<cstring> using namespace std; bool map[505][505]; int n, k; bo ...

  7. 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...

  8. 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> const ...

  9. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

随机推荐

  1. springmvc框架的搭建

    1引入jar包 jar包下载地址http://maven.springframework.org/release/org/ 以下是我引入的jar包 aopalliance-1.0.jaraspectj ...

  2. ArcGIS模型构建器案例学习-批量删除空要素类地理模型

    ArcGIS模型构建器案例学习笔记-批量删除空要素类地理模型 联系方式:谢老师,135-4855-4328,xiexiaokui@qq.com 目的:批量删除记录个数为0的矢量文件 优点:逻辑清晰,不 ...

  3. dp-最长公共子序列

    最长公共子序列(NYOJ36) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公 ...

  4. Managing Images on smartos

    SmartOS依赖images.images是包含在创建新zone或虚拟机时使用的磁盘或文件系统映像和元数据的模板. images使用imgadm工具进行管理. 使用此工具,您可以: 查看和下载在公共 ...

  5. RedisTemplate Redis 操作

    stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向r ...

  6. ubuntu安装谷歌拼音输入法

    在这篇教程中,我将告诉你如何在ubuntu系统上安装谷歌拼音输入法.谷歌拼音输入法有基于ibus框架的,也有基于fcitx框架的.我只演示fcitx框架下谷歌拼音输入法的安装,因为ibus框架的谷歌拼 ...

  7. phpStudy4——前端页面使用Ajax请求并解析php返回的json数据

    项目需求: 在html页面显示所有用户列表信息. 需求分析: 1. html页面使用ajax向后端php请求用户数据 2. php脚本查询数据库,并将查询后的结果以json格式返回前端html页面 3 ...

  8. PTA 习题集5-18 打印选课学生名单(哈希)

    假设全校有最多40000名学生和最多2500门课程.现给出每个学生的选课清单,要求输出每门课的选课学生名单. 输入格式: 输入的第一行是两个正整数:N(≤40000),为全校学生总数:K(≤2500) ...

  9. JavaScript的控制语句和循环语句和函数的总结

    10.控制语句---if语句 10_1:if-else语句 if(表达式){ 语句1: .... }else{ 语句1: .... }; 示例: var a = 1; if (a > 0){ a ...

  10. python的paramiko模块简单应用

    用法1,SSHClient 分别可以使用密码和秘钥登陆,然后执行命令,并且获取执行结果 import paramiko #创建一个SSH对象 ssh = paramiko.SSHClient() #允 ...