HDU 1045 Fire Net(DFS 与8皇后问题类似)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14780 Accepted Submission(s): 8932
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.
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
1
5
2
4
题目意思:
题目给出一个n和一幅N*N的图,图中包含'.','X'两种字符,
要求在上面放上碉堡,碉堡能攻击他所在的行和列,但不能攻击墙,求最多能放多少个碉堡.
X是墙,不能放炮台
与八皇后类似,炮台不能存在于同行同列,除非中间隔墙
直接dfs
code:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
char G[][];
int n,sum;
bool check(int x,int y)//检查该点能不能放炮台
{
if(G[x][y]=='X')//该点是墙,不能放直接退出
return false;
for(int i=x;i<n;i++)//判断该点的下面不能右炮台(除非有墙)
{
if(G[i][y]=='X')
break;
if(G[i][y]=='S')
return false;
}
for(int i=x;i>=;i--)//上
{
if(G[i][y]=='X')
break;
if(G[i][y]=='S')
return false;
}
for(int j=y;j<n;j++)//右
{
if(G[x][j]=='X')
break;
if(G[x][j]=='S')
return false;
}
for(int j=y;j>=;j--)//左
{
if(G[x][j]=='X')
break;
if(G[x][j]=='S')
return false;
}
return true;
}
void dfs(int k)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(check(i,j))
{
G[i][j]='S';
dfs(k+);
G[i][j]='.';
}
}
}
if(sum<k)
sum=k;
}
int main()
{
while(cin>>n,n)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
cin>>G[i][j];
}
}
sum=;
dfs();
cout<<sum<<endl;
}
return ;
}
HDU 1045 Fire Net(DFS 与8皇后问题类似)的更多相关文章
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 1045 Fire Net(DFS)
Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...
- HDU 1045 Fire Net(dfs,跟8皇后问题很相似)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1045 Fire Net(最小覆盖点+构图(缩点))
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS Memory Limit:32768KB ...
- HDU 1045(Fire Net)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...
- HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】
Fire Net Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- HDU 1045 Fire Net 【连通块的压缩 二分图匹配】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1045 Fire Net 状压暴力
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1045 Fire Net 二分图建图
HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
随机推荐
- Heka 最简单例子
技术人员学习都是从简单例子开始的, Heka的应用也是从简单开始的. 需求: 监控一个日志文件的内容, 在标准输出显示出来. 操作步骤: 使用下载好或者编译好的 heka 已经编译好的 rel ...
- MySql数据快速导入
使用LOAD DATA INFILE 插入速度可以提升很多 左侧是直接导入100W花费135s ,Dos界面通过Load方式导入450W只用时23s,性能一下子显示出来了.
- VC 在调用main函数之前的操作
title: VC 在调用main函数之前的操作 tags: [VC++, 反汇编, C++实现原理] date: 2018-09-16 10:36:23 categories: VC++反汇编分析 ...
- 配置centos7 网卡
进入root模式,输入 cd /etc/sysconfig/network-scripts/ 按Tab键查看网卡配置文件名称,然后进入编辑: 如: cd /etc/sysconfig/network- ...
- 移动web中的幻灯片切换效果
百度或者谷歌下类似的插件有很多,原理都差不多,关键适合自己的项目,如果移动端要引入jquery这么大的插件,只能呵呵了.... 下面是工作中针对webkit内核的浏览器写的,html很简单: < ...
- (1-1)line-height的定义和行内框盒子模型
(1-1)line-height的定义和与行内框盒子模型的关系 一.line-height的定义 line-height的定义: 行高,又称为两基线的距离.默认基线对齐(因为CSS所有*线:总之就是各 ...
- JS常见的几种数组去重方法
总结一下JS中用到的数组去重的方法 方法一: 该方法利用对象的属性值不能相同: function arrDelLikeElement (array) { const result = []; con ...
- vue-router 的使用
vue-router 是 vue 的 一个特色. 下面介绍vue-router 的使用: 一.先将vue-router作为vue 的一个插件使用 import Vue from 'vue' imp ...
- ERP与电子商务的集成
目前现状: 一般来说,企业中存在三种流:物资流.资金流和信息流,其中,信息流不是孤立存在的,它与物资流和资金流密切相关,反映了物资和资金流动前.流动中和流动后的状况. 电子商务与ERP被分裂开来,没有 ...
- flask框架下的jinja2模板引擎(1)(模板渲染)
#转载请留言联系 模板是什么? 在 flask 框架中,视图函数有两个作用:处理业务逻辑和返回响应内容.在大型应用中,把业务逻辑和表现内容放在一起,会增加代码的复杂度和维护成本.模板作用即是承担视图函 ...