Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7929    Accepted Submission(s):
4521

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
 
用深搜对每一个点进行遍历,
 
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char map[][];
int n,now,ibest,current;
bool canput(int row,int col)//判断能不能放
{
int i;
for(i=row-;i>=;i--)//判断行
{
if(map[i][col]=='O')
return false;
if(map[i][col]=='X')
break;
}
for(i=col-;i>=;i--)//判断列
{
if(map[row][i]=='O')
return false;
if(map[row][i]=='X')
break;
}
return true;
}
void dfs(int k,int current)
{
int x,y;
if(k==n*n)//如果遍历完就返回
{
if(current>ibest)//更新最大的个数
ibest=current;
return ;
}
else
{
x=k/n;
y=k%n;
if(map[x][y]=='.'&&canput(x,y))
{
map[x][y]='O';
dfs(k+,current+);//下一次递归
map[x][y]='.';
}
dfs(k+,current);//当前不放碉堡
}
}
int main()
{
while(scanf("%d",&n),n)
{
getchar();
int k=,i;
ibest=;
current=;
for(i=;i<n;i++)
scanf("%s",map[i]);
dfs(,);
printf("%d\n",ibest);
}
return ;
}

Fire Net--hdu1045的更多相关文章

  1. HDU1045 Fire Net(DFS)

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

  2. HDU-1045 Fire Net

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

  3. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  4. HDU1045:Fire Net(二分图匹配 / DFS)

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

  5. HDU1045 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(缩点 + 二分匹配)

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

  7. hdu1045 Fire Net

    在一张地图上建立碉堡(X),要求每行没列不能放两个,除非中间有强挡着.求最多能放多少个碉堡 #include<iostream> #include<cstdio> #inclu ...

  8. hdu1045 Fire Net---二进制枚举子集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意: 给你一幅n*n的图,再给你一些点,这些点的上下左右不能再放其他点,除非有墙('X') ...

  9. 【HDU-1045,Fire Net-纯暴力简单DFS】

    原题链接:点击!   大致题意:白块表示可以放置炮台的位置——每个炮台可以攻击到上下左右的直线上的炮台(也就是说在它的上下左右直线上不可以再放置炮台,避免引起互相攻击),黑块表示隔离墙的位置——不可放 ...

  10. Fire Net(HDU-1045)(匈牙利最大匹配)(建图方式)

    题意 有一个 n*n 的图,. 代表空白区域,X 代表墙,现在要在空白区域放置结点,要求同一行同一列只能放一个,除非有墙阻隔,问最多能放多少个点 思路 只有在墙的阻隔情况下,才会出现一行/列出现多个点 ...

随机推荐

  1. css制作最简单导航栏

    css制作最简单导航栏

  2. python----抽象类

    #!/usr/local/python3.5/bin/python3.5 ####实现方法一 class Supper(object): def delegate(self): self.action ...

  3. c#POST请求和接收post请求

    c# post请求发送数据 /// <summary> /// POST请求 /// </summary> /// <param name="url" ...

  4. 在一个exe文件中查找指定内容,找到则返回起始位置, 否则返回0

    //在一个exe文件中查找指定内容,找到则返回起始位置, 否则返回0//如果某字符串, 直接传入字符串进来//如果要查找16进制,则用如下格式传参进来: #$1A#$2A#$3A function F ...

  5. eclipse下使用hibernate tools实现hibernate逆向工程

    一  安装hibernate tools插件 1 在线安装 通过Eclipse的Help->Install New Software 在线安装插件,插件连接为: eclipse helios(3 ...

  6. Linux系统编程(22)——响应信号

    进程对信号的响应 进程可以通过三种方式来响应一个信号: 1.忽略信号,即对信号不做任何处理,其中,有两个信号不能忽略:SIGKILL及SIGSTOP: 2.捕捉信号.定义信号处理函数,当信号发生时,执 ...

  7. zoj 3471 Most Powerful(状态压缩dp)

    Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These ato ...

  8. 移除UIView上面的所有控件

    ;i<[view.subviews count];i++){ [ [ view.subviews objectAtindex:i] removeFromsuperview]; }

  9. Pasha and String(思维,技巧)

    Pasha and String Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  10. A Bug's Life

    #include<stdio.h> #include<string.h> ],num[]; int find(int x){ int r=x; while(r!=bug[r]) ...