Fire Net

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

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
 
Source

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 10
char maze[maxn][maxn];
int ans,n,count,max;
/*用p作为人的标记*/
/*判断竖横是否有城堡*/
bool judge(int x,int y)
{
int i;
bool flag=true;
for(i=y+;i<=n;i++)
{
if(maze[x][i]=='P')
{
flag=false;
break;
}
else
if(maze[x][i]=='X')
break;
}
if(!flag)
return false;
for(i=y-;i>=;i--)
{
if(maze[x][i]=='P')
{
flag=false;
break;
}
else
if(maze[x][i]=='X')
break;
}
if(!flag)
return false;
for(i=x+;i<=n;i++)
{
if(maze[i][y]=='P')
{
flag=false;
break;
}
else if(maze[i][y]=='X')
break;
}
if(!flag)
return false;
for(i=x-;i>=;i--)
{
if(maze[i][y]=='P')
{
flag=false;
break;
}
else if(maze[i][y]=='X')
break;
}
if(!flag)
return false;
else
return true;
}
void dfs()
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(maze[i][j]=='.'&&true==judge(i,j))
{
count++;
maze[i][j]='P';
dfs();
if(count>ans)
ans=count;
maze[i][j]='.';
count--;
}
}
}
}
int main()
{
int i,j;
while(scanf("%d",&n),n)
{
getchar();
max=;
for( i=;i<=n;i++)
{
for( j=;j<=n;j++)
{
scanf("%c",&maze[i][j]);
}
getchar();
}
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
if(maze[i][j]!='X')
{
count=;
ans=;
maze[i][j]='P';
dfs();
maze[i][j]='.';
if(ans==) max=;
else
if(ans>max) max=ans;
}
}
}
printf("%d\n",max);
}
return ;
}

思路:   每次涂一个点,然后生成一张图,将其作为一张新图,又重新开始涂点.....周而复始.....

并不断记录点的最大个数.用栈,或者回溯都可以实现.....

HDUOJ---------(1045)Fire Net的更多相关文章

  1. DFS ZOJ 1002/HDOJ 1045 Fire Net

    题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...

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

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

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

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

  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 状压暴力

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

  7. HDU 1045(Fire Net)题解

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

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

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

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

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

  10. hdoj 1045 Fire Net

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

随机推荐

  1. jquery、js获取table,遍历输出tr中各个td的内容。

    首先,依赖jquery.. $('#btntb').click(function(){ $('#tab tr').each(function(i){ // 遍历 tr $(this).children ...

  2. uva 10618 Tango Tango Insurrection 解题报告

    Tango Tango Insurrection Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebu ...

  3. [3] 球(Sphere)图形的生成算法

    顶点数据的生成 bool YfBuildSphereVertices ( Yreal radius, Yuint slices, Yuint stacks, YeOriginPose originPo ...

  4. TCP的TIME_WAIT快速回收与重用

    声明一点: Linux中是无法修改tcp的TIME_WAIT值的,除非重新编译,起码我是没有找到怎么改.值得注意的是,net.ipv4.tcp_fin_timeout这个参数是FIN_WAIT_2的值 ...

  5. 【Dagger2】简介 配置 使用 MVP案例

    简介 dagger2:https://github.com/google/dagger Maven Central  2.11版本jar包下载 dagger:https://github.com/sq ...

  6. 如何解决 SQL Server 中的锁升级所致的阻塞问题

    概要 锁升级为表锁插入转换很多细粒度的锁 (如行或页锁) 的过程.Microsoft SQL Server 动态确定何时执行锁升级.作出决定之前,SQL Server 将特定的扫描,整个事务,并且用于 ...

  7. 解析XML并将信息封装到对象中

    [person.xml]要解析的内容 <?xml version="1.0" encoding="UTF-8"?> <students> ...

  8. IOS基本数据类型之枚举

    枚举是C语言中的一种基本数据类型,通过枚举可以声明一组常数,来代表不同的含义,它实际上就是一组整型常量的集合. 枚举是非常常用的一种类型,在现实生活中也很常见.比如有四个季节,在不同的季节需要显示不同 ...

  9. svn 错误集锦续

    4,svn出错:Error: File or directory '.' is out of date; try updating 出错原因:SVN服务器端的版本比你的版本要新,不允许提交. 解决方案 ...

  10. Mongo命令批量更新某一数组字段的顺序

      db.table.find().forEach(function (doc) {     var oldValue = doc.Column1;     var newValue = [sa[1] ...