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.
  1. Sample Input
  2.  
  3. .X..
  4. ....
  5. XX..
  6. ....
  7.  
  8. XX
  9. .X
  10.  
  11. .X.
  12. X.X
  13. .X.
  14.  
  15. ...
  16. .XX
  17. .XX
  18.  
  19. ....
  20. ....
  21. ....
  22. ....
  23.  
  24. Sample Output

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045

********************************************

题意:n乘n 的矩阵,一般同一行同一列不能放两个'O',如果有'X'分割开来则可以,问你在'.'处最多可以放多少个'O'。

分析:二分匹配。

AC代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<queue>
  5. #include<algorithm>
  6. #include<time.h>
  7. #include<stack>
  8. #include<vector>
  9. using namespace std;
  10. #define N 1200
  11. #define INF 0x3f3f3f3f
  12.  
  13. int vv[N],x,y,n;
  14. char maps[][];
  15. int vis[N][N],v[N];
  16.  
  17. struct node
  18. {
  19. int x,y;
  20. } a[N][N];
  21.  
  22. int Hungary(int u)///匈牙利算法匹配
  23. {
  24. for(int i=; i<=y; i++)
  25. if(vis[u][i]&&!v[i])
  26. {
  27. v[i]=;
  28. if(!vv[i]||Hungary(vv[i]))
  29. {
  30. vv[i]=u;
  31. return ;
  32. }
  33. }
  34.  
  35. return ;
  36. }
  37.  
  38. int main()
  39. {
  40. int i,j;
  41.  
  42. while(scanf("%d", &n),n)
  43. {
  44. for(i=; i<n; i++)
  45. scanf("%s", maps[i]);
  46.  
  47. x=,y=;
  48. memset(vis,,sizeof(vis));
  49.  
  50. for(i=; i<n; i++)
  51. for(j=; j<n; j++)
  52. {
  53. ///把图分割,以相连的‘.’为行和列重新分配编号
  54. if(maps[i][j]=='.')
  55. {
  56. if(j==||maps[i][j-]=='X')
  57. x++;
  58. a[i][j].x=x;
  59. }
  60. if(maps[j][i]=='.')
  61. {
  62. if(j==||maps[j-][i]=='X')
  63. y++;
  64. a[j][i].y=y;
  65. }
  66. }
  67.  
  68. for(i=; i<n; i++)
  69. for(j=; j<n; j++)
  70. if(maps[i][j]=='.')
  71. {
  72. int u=a[i][j].x;
  73. int v=a[i][j].y;
  74. vis[u][v]=;///用行匹配列
  75. }
  76.  
  77. int ans=;
  78. memset(vv,,sizeof(vv));
  79. for(i=; i<=x; i++)
  80. {
  81. memset(v,,sizeof(v));
  82. if(Hungary(i)==)
  83. ans++;
  84. }
  85.  
  86. printf("%d\n", ans);
  87. }
  88. return ;
  89. }

HDU - 1045 Fire Net(二分匹配)的更多相关文章

  1. hdu 1045 Fire Net(二分匹配 or 暴搜)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  2. hdu 1045 Fire Net 二分图匹配 && HDU-1281-棋盘游戏

    题意:任意两个个'车'不能出现在同一行或同一列,当然如果他们中间有墙的话那就没有什么事,问最多能放多少个'车' 代码+注释: 1 //二分图最大匹配问题 2 //难点在建图方面,如果这个图里面一道墙也 ...

  3. HDU 1045 Fire Net(行列匹配变形+缩点建图)

    题意:n*n的棋盘上放置房子.同一方同一列不能有两个,除非他们之间被墙隔开,这种话. 把原始图分别按行和列缩点 建图:横竖分区.先看每一列.同一列相连的空地同一时候看成一个点,显然这种区域不可以同一时 ...

  4. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  5. HDU 1045 Fire Net 【连通块的压缩 二分图匹配】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    ...

  6. HDU 1045 Fire Net 【二分图匹配】

    <题目链接> 题目大意: 这题意思是给出一张图,图中'X'表示wall,'.'表示空地,可以放置炮台,同一条直线上只能有一个炮台,除非有'X'隔开,问在给出的图中最多能放置多少个炮台. 解 ...

  7. hdu 1045 Fire Net(最小覆盖点+构图(缩点))

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

  8. HDU 1045(Fire Net)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...

  9. hdu 1045:Fire Net(DFS经典题)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. JAVA环境变量配置详解

    JAVA环境变量JAVA_HOME.CLASSPATH.PATH设置详解 Windows下JAVA用到的环境变量主要有3个,JAVA_HOME.CLASSPATH.PATH. JAVA_HOME 指向 ...

  2. 实现table中checkbox复选框、以及判断checked是否被选中、js操作checkedbox选中

    上图是实现效果. 下面贴代码 表的第一行也就是<th>中的代码,onclick事件是实现全选或者全不选效果. <th> <input id="allboxs&q ...

  3. svn is already locked解决办法

    在出错文件夹下,鼠标右键

  4. python返回null和空的不同

    mysql数据库中有的字段是NULL, 有的字段是空白 写Python脚本,fetchall()得到结果,也是不同. NULL对应的是None, 空白对应的是'' (None, '') 所以根据结果进 ...

  5. 7. Shell 函数

    1. 格式 [ function ] funname [()] { action; [return int;] } 可以带function fun() 定义,也可以直接fun() 定义,不带任何参数 ...

  6. sql 查看语句的性能

    SET STATISTICS TIME:看cpu时间   SET STATISTICS IO:关注scan count(计数)------查询读取的表数量:logical read( 逻辑读)次数

  7. icon的使用

    在前端页面设计时,不免使用的就是图标,下面就我使用图标icon分享一下经验 1.icon插件,现在比较好的是bootstrap自带的,fontawesome,链接地址:http://fontaweso ...

  8. ios 软键盘顶起这个页面

    html { overflow: hidden; } ;;;; } ;;; } ;; left: 200px; overflow: auto;} 行内的滚动条.

  9. PAT 团体程序设计天梯赛-练习集 L1-018. 大笨钟

    微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜身体早点睡觉.不过由于笨钟自己作息也不是很规律,所以敲钟并不定时.一般敲钟的点数是根据敲钟时间而定的,如果正好在某个整点敲,那么“当”数就等于那 ...

  10. PAT 团体程序设计天梯赛-练习集 L1-017. 到底有多二

    一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值.如果这个数是负数,则程度增加0.5倍:如果还是个偶数,则再增加1倍.例如数字“-13142223336”是个11位数,其中有3个2,并且 ...