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 . 第 ...
随机推荐
- Applied Nonparametric Statistics-lec6
Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/8 前面都是对一两个样本的检查,现在考虑k个样本的情 ...
- leetcode-23-DynamicProgramming-1
357. Count Numbers with Unique Digits 解题思路: 用arr[i]存放长度为i时,各位互不相同的数字的个数,所以arr[1]=10,arr[2]=9*9.(第一位要 ...
- LeetCode(205)Isomorphic Strings
题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ch ...
- hdu-1338 game predictions(贪心题)
Suppose there are M people, including you, playing a special card game. At the beginning, each playe ...
- hdu 1011 Starship Troopers(树形背包)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Python属性描述符(二)
Python存取属性的方式特别不对等,通过实例读取属性时,通常返回的是实例中定义的属性,但如果实例未曾定义过该属性,就会获取类属性,而为实例的属性赋值时,通常会在实例中创建属性,而不会影响到类本身.这 ...
- Django底层原理简介与安装
Django环境目录搭建一栏: 利用wsgiref模块封装好的socket搭建服务端: #利用wsgiref模块封装好的socket演示操作(例如accept\recv) #也可以实现socket服务 ...
- LibreOJ β Round #4
A游戏 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: qmqmqm 提交提交记录统计讨论测试数据 题目描述 qmqmqm和subline ...
- PTA 11-散列1 电话聊天狂人 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/722 5-14 电话聊天狂人 (25分) 给定大量手机用户通话记录,找出其中通话次数 ...
- hdu6085[压位+暴力] 2017多校5
/*hdu6085[压位+暴力] 2017多校5*/ /*强行优化..*/ #include <bits/stdc++.h> using namespace std; struct bit ...