HDU 1045 Fire Net 状压暴力
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8073 Accepted Submission(s): 4626
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很小,所以可以直接暴力求所有状态,然后检查,更新答案。
代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#define MAX_N 10
#define MAX_S (1<<16)
using namespace std; bool G[MAX_N][MAX_N];
int n;
bool tmp[MAX_N][MAX_N]; int ones[MAX_S]; void init() {
for (int i = ; i < MAX_S; i++) {
int s = i;
while (s > ) {
ones[i] += (s & );
s >>= ;
}
}
} bool check(int s) {
for (int i = ; i < n; i++)
for (int j = ; j < n; j++) {
tmp[i][j] = (s & );
if (G[i][j] && tmp[i][j])return false;
s >>= ;
}
for (int i = ; i < n; i++) {
bool flag = false;
for (int j = ; j < n; j++) {
if (G[i][j])flag = false;
else if (tmp[i][j]) {
if (flag)return false;
else flag = true;
}
}
}
for (int j = ; j < n; j++) {
bool flag = false;
for (int i = ; i < n; i++) {
if (G[i][j])flag = false;
else if (tmp[i][j]) {
if (flag)return false;
else flag = true;
}
}
}
return true;
} int main() {
init();
while (true) {
scanf("%d", &n);
if (n == )break;
for (int i = ; i < n; i++) {
string s;
cin >> s;
for (int j = ; j < n; j++) {
if (s[j] == 'X')G[i][j] = ;
else G[i][j] = ;
}
}
int ans = ;
for (int i = ; i < ( << (n * n)); i++)
if (check(i))
ans = max(ans, ones[i]);
printf("%d\n", ans);
}
return ;
}
HDU 1045 Fire Net 状压暴力的更多相关文章
- HDU 5765 Bonds 巧妙状压暴力
题意:给一个20个点无向连通图,求每条边被多少个极小割集包括 分析:极小割集是边的集合,很显然可以知道,极小割集恰好吧原图分成两部分(这个如果不明白可以用反证法) 然后就是奉上官方题解:http:// ...
- hdu 3247 AC自动+状压dp+bfs处理
Resource Archiver Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Ot ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- HDU 3605 Escape(状压+最大流)
Escape Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Sub ...
- 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 5765 Bonds(状压DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...
- hdu 5023 线段树+状压
http://acm.hdu.edu.cn/showproblem.php?pid=5023 在片段上着色,有两种操作,如下: 第一种:P a b c 把 a 片段至 b 片段的颜色都变为 c . 第 ...
随机推荐
- 封装,封装的原理,Property ,setter ,deleter,多态,内置函数 ,__str__ , __del__,反射,动态导入模块
1,封装 ## 什么是封装 what 对外隐藏内部的属性,以及实现细节,并给外部提供使用的接口 学习封装的目的:就是为了能够限制外界对内部数据的方法 注意 :封装有隐藏的意思,但不是单纯的隐藏 pyt ...
- [译]The Python Tutorial#8. Errors and Exceptions
[译]The Python Tutorial#Errors and Exceptions 到现在为止都没有过多介绍错误信息,但是已经在一些示例中使用过错误信息.Python至少有两种类型的错误:语法错 ...
- python中文件操作的其他方法
前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...
- HDU - 1465 不容易系列之一(错排)
HDU有个网名叫做8006的男性同学,结交网友无数,最近该同学玩起了浪漫,同时给n个网友每人写了一封信,这都没什么,要命的是,他竟然把所有的信都装错了信封!注意了,是全部装错哟! 现在的问题是:请大家 ...
- Mysql显示某个数据库的所有表
显示表名: show tables; //先用use进入要查看表的库 mysql> use mysql; Database changed mysql> show tables; +--- ...
- TensorFlow TFRecord封装不定长的序列数据(文本)
TensorFlow TFRecord封装不定长的序列数据(文本) 在实验室环境中,通常数据都是一次性导入内存的,然后使用手工写的数据mini-batch函数来切分数据,但是这样的做法在海量数据下显得 ...
- springmvc始终跳转至首页,不报404错误(续)
上篇博客说到,当我执行程序时,springmvc的控制下,它始终跳转到首页,而不正常跳转.当时通过换一个服务器解决了问题,以为是缓存的事儿.但后来又发生了同样的事儿,顿时感觉出事儿了.就立马降低了日志 ...
- iOS--app自定义相册--给图片重写exif数据-定义相册时间戳
1.Exif简介 可交换图像文件格式常被简称为Exif(Exchangeable image file format),是专门为数码相机的照片设定的,可以记录数码照片的属性信息和拍摄数据. Exif可 ...
- 【Luogu】P2764最小路径覆盖(拆点求最大匹配)
题目链接 这个……学了一条定理 最小路径覆盖=原图总点数-对应二分图最大匹配数 这个对应二分图……是什么呢? 就是这样 这是原图 这是拆点之后对应的二分图. 然后咱们的目标就是从这张图上跑出个最大流来 ...
- Centos7系统rc.local不起作用问题
Centos7系统rc.local不起作用问题 来源 https://www.cnblogs.com/xjz00/p/7729405.html Centos7已经写了要chmod +x /etc/rc ...