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. JQuery笔记(一)jq的使用方法

    我用的jq版本是支持pc版为主的最高1版本里最高的1.124版本 官网的链接是只有最新的3下载,我把我在官网下载的jq代码链接发出来,如下 点我获取jq代码 和js不同的是,jq开发者封装了一些方法 ...

  2. 我的JAvA第三天

  3. leaflet地图在选项卡中不正常显示

    可以在选项卡中加个click事件,调用下 <a href="#tab1" >tab1</a><a href="#tab2" onc ...

  4. 高一的我曾对自己说"要放慢脚步去生活"!?

    看了高一的时候自己写的日记,瞬间被自己感动到了.以下是当时的几段感慨: 慢是一种放松.是生活的一种良好心态,喜欢这样放慢步伐地生活,那是一种享受! 但我们生活在一个快节奏的时代,我们总是被迫卷进那潮流 ...

  5. OOP的方法

    <?php class Computer{ //我要创建一个构造方法 public function __construct(){ echo '我是比较先进的构造方法!'; //构造方法一般用于 ...

  6. java中部分知识点的验证实现

    java中运算符重载 满足以下条件的两个或多个方法构成"重载"关系: (1)方法名相同: (2)参数类型不同,参数个数不同,或者是参数类型的顺序不同. 注意:方法的返回值不作为方法 ...

  7. Dubbo.xml配置源-Dubbo.xsd分析

      我们使用Dubbo时,一般都会使用xml配置基本信息,如项目名称(application).注册中心(register).协议(protocal).服务(service),如下所示: 1 2 3 ...

  8. 大数据时代之hadoop(二):hadoop脚本解析

    “兵马未动,粮草先行”,要想深入的了解hadoop,我觉得启动或停止hadoop的脚本是必须要先了解的.说到底,hadoop就是一个分布式存储和计算框架,但是这个分布式环境是如何启动,管理的呢,我就带 ...

  9. ZOJ 2476 Total Amount

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2476 Time Limit: 2 Seconds         ...

  10. 读书笔记之C# delegate

    c#代理的使用主要在:需要将一个方法当做参数传递到另一个方法时. 比如启动一个线程执行任务,而这个线程要执行的方法可以通过代理传递过来. 代理包括一个方法或者多个方法的地址和C++的函数指针很相似,但 ...