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. .net数据库连接防注入参数查询 命令执行 读取 备份 导出导入转化XML格式

    ADO.NET是一组类库,让我们通过程序的方式访问数据库.SYSTEM.DATA这个类提供了统一的接口访问Oracle MSSQL Access.像SYSTEM.IO类操作文件一样. **connec ...

  2. scala sparseVetor, SprseMatrix 实现

    def rand(seed:Int):Double={ val rand=new Random(seed) rand.nextDouble()} def rand2(size:Int,seed:Int ...

  3. electron sendInputEvent keyboard

    https://github.com/electron/electron/issues/5005 webview.getWebContents().sendInputEvent({ type: 'ch ...

  4. 自己整理lnmp安装

    1. 操作系统   CentOS release 6.5(final)   2. 安装mysql   # yum install mysql-server   #vi /etc/my.cnf +def ...

  5. 【校招面试 之 C/C++】第1题 为什么优先使用构造函数的初始化列表

    1.首先看一个例子: #include<iostream> using namespace std; class Test1 { public: Test1() // 无参构造函数 { c ...

  6. Raft 一致性算法论文译文

    本篇博客为著名的 RAFT 一致性算法论文的中文翻译,论文名为<In search of an Understandable Consensus Algorithm (Extended Vers ...

  7. linux下安装php php-fpm(转载)

    centos安装php php-fpm 1.下载php源码包http://www.php.net/downloads.php2 .安装phptar -xvf php-5.5.13.tar.bz2cd ...

  8. jquery节点获取

    jQuery.parent(expr)  找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(& ...

  9. TASK 的使用

    http://www.tuicool.com/articles/IveiQbQ

  10. C语言点滴

    static修饰的变量和函数不可以在其他文件extern引用该变量或者函数. static变量放在静态内存区. static变量赋值只生效一次,再无法调用赋值语句.但是可以运算,例如++等. exte ...