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. JavaScript开发规范

    作为一名开发人员(WEB前端JavaScript开发),不规范的开发不仅使日后代码维护变的困难,同时也不利于团队的合作,通常还会带来代码安全以 及执行效率上的问题.本人在开发工作中就曾与不按规范来开发 ...

  2. PyQuery查询html信息

    以下代码主要演示使用pyquery进行对html文件的解析,包括设定编码,对子块进行查询等操作: from pyquery import PyQuery as pq import os from lx ...

  3. js 对日期加减

    function getDate(days) { var now = new Date(), newDate = new Date(now.getTime() - 86400000 * days), ...

  4. ZendFramework2 文件结构

    Application |__ config // 配置文件目录 |__ language // 语言包目录 |__ src |__ Application |__ Controller // 控制器 ...

  5. python之3内置容器

    所谓内置容器,就是不需要第三方模块,就可以使用的. 1.list容器,一个有序序列,类似于数组,但比数组强大很多 1.1.如何初始化list,直接一个[]即可,这是一个空的list >>& ...

  6. python - list, cllections模块的deque对象

    list.count() list.pop()/list.pop(i) list.insert(i,element) list.sort()和sorted(list) list.reverse()和r ...

  7. MVC5学习相关资源整理

    1  官方 Getting Started http://www.asp.net/mvc/tutorials/mvc-5/introduction/getting-started 英文不好,英文好的同 ...

  8. org.quartz.impl.jdbcjobstore.LockException

    说明:在使用Tomcat6.0.32+Spring3.05+Quartz1.8.6+Mysql5.5.9 此项目在我本机上没有问题,当我把mysql 脚本导入到服务器上,将数据源配置修改为服务器对应的 ...

  9. Linux系统编程(21)——信号的产生

    1.通过终端按键产生信号 通过上一篇我们知道了SIGINT的默认处理动作是终止进程,SIGQUIT的默认处理动作是终止进程并且Core Dump,现在我们来验证一下. 首先解释什么是Core Dump ...

  10. Android 操作系统的内存回收机制[转]

    转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-android-mmry-rcycl/ Android APP 的运行环境 Andro ...