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

.X..
....
XX..
.... XX
.X .X.
X.X
.X. ...
.XX
.XX ....
....
....
.... Sample Output

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

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

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

分析:直接暴力搜索。

AC代码:

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
#include<vector>
using namespace std;
#define N 120000
#define INF 0x3f3f3f3f vector<int >Q; int sum,n;
char maps[][]; int p(int x,int y)
{
for(int i=x;i>=;i--)
{
if(maps[i][y]=='O')
return ;
if(maps[i][y]=='X')
break;
} for(int i=y;i>=;i--)
{
if(maps[x][i]=='O')
return ;
if(maps[x][i]=='X')
break;
}
return ;
} void dfs(int x,int y)
{
if(x==n*n)
{
sum=max(sum,y);
return ;
}
else
{
int row=x/n;
int col=x%n;
if(maps[row][col]=='.'&&p(row,col))
{
maps[row][col]='O';
dfs(x+,y+);
maps[row][col]='.';
}
dfs(x+,y);
}
} int main()
{
int i; while(scanf("%d", &n),n)
{
sum=; for(i=;i<n;i++)
scanf("%s", maps[i]); dfs(,); printf("%d\n", sum);
}
return ;
}

HDU - 1045 Fire Net(搜索)的更多相关文章

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

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

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

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

  3. HDU 1045(Fire Net)题解

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

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

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

  5. HDU 1045 Fire Net(dfs,跟8皇后问题很相似)

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

  6. HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】

    Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. HDU 1045 Fire Net 状压暴力

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

  8. HDU 1045 Fire Net 二分图建图

    HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...

  9. HDU 1045 Fire Net(搜索剪枝)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 http://acm.hdu.edu.cn/showproblem.php?pid=1045 ...

随机推荐

  1. Scala 字段定义

    Scala 中定义字段只有val, var两种方式,都要在定义的同时即赋值,var 可以用占位符' _ '代替. 抽象类中定义的抽象字段不用赋初值,抽象方法也不用写方法体.(在子类中给抽象字段赋值和实 ...

  2. @Autowired与 @Resource

    @Autowired, @Resource 1.注解类型: Autowired可用于构造器.属性.方法.注解 @Target({ElementType.CONSTRUCTOR, ElementType ...

  3. WEB服务器:Apache、Tomcat、JBoss、WebLogic、Websphere、IIS的区别与关系

    1)Apache  免费,世界使用排名第一的Web服务器.它可以运行在几乎所有广泛使用的计算机平台上.Apache的特点是简单.速度快.性能稳定,并可做代理服务器来使用.Apache是以进程为基础的结 ...

  4. 移动端rem适配问题

    将下面这段代码,放在头部的script标签里,可解决字体适配问题 比例是28px = 1rem function __setFontSize__(){document.documentElement. ...

  5. Node.js API

    Node.js v4.4.7 Documentation(官方文档) Buffer Prior to the introduction of TypedArray in ECMAScript 2015 ...

  6. Weex-语法笔记 一

    p.p1 { margin: 0.0px 0.0px 2.0px 0.0px; font: 14.0px "PingFang SC Semibold"; color: #45454 ...

  7. Ubuntu操作相关笔记

    Eclipse添加图标 #sudo vim /usr/share/applications/eclipse.desktop 写入以下内容 [Desktop Entry] Name=Eclipse Co ...

  8. prototype原型解析

    每个对象都有原型, prototype能够实现实例共享,节约使您有能力向对象添加属性和方法. 如 <script type="text/javascript"> fun ...

  9. wps使用积累

    1.word加批注: 选中文字--插入--批注

  10. JAVA监听

    http://www.cnblogs.com/xdp-gacl/p/3961929.html 不要按汉字的字面意思理解这东西,其实它就是把某个操作绑到了某个按纽上,当你按那个按纽的时候,它所绑定的操作 ...