原题链接: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

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
 

题意

给你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 状压暴力的更多相关文章

  1. HDU 5765 Bonds 巧妙状压暴力

    题意:给一个20个点无向连通图,求每条边被多少个极小割集包括 分析:极小割集是边的集合,很显然可以知道,极小割集恰好吧原图分成两部分(这个如果不明白可以用反证法) 然后就是奉上官方题解:http:// ...

  2. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  3. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

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

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

  5. HDU 3605 Escape(状压+最大流)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

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

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

  7. HDU 1045(Fire Net)题解

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

  8. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

  9. hdu 5023 线段树+状压

    http://acm.hdu.edu.cn/showproblem.php?pid=5023 在片段上着色,有两种操作,如下: 第一种:P a b c 把 a 片段至 b 片段的颜色都变为 c . 第 ...

随机推荐

  1. 实验二 JSP基本动态元素的使用

    实验二  JSP基本动态元素的使用 实验性质:验证性          实验学时:  2学时      实验地点: 一 .实验目的与要求 1.掌握JSP中声明变量.定义方法.java程序片及表达式的使 ...

  2. static关键字所导致的内存泄漏问题

    大家都知道内存泄漏和内存溢出是不一样的,内存泄漏所导致的越来越多的内存得不到回收的失手,最终就有可能导致内存溢出,下面说一下使用staitc属性所导致的内存泄漏的问题. 在dalvik虚拟机中,sta ...

  3. leetcode-4-basic

    解题思路:这道题比较简单,代码不贴了.需要注意的是: 数字与字符串之间的转换, char str[100]; sprintf(str, "%d", num); 解题思路: 这道题是 ...

  4. IAR单片机启动文件与程序入口

    最近在做TI单片机TM4C123GE6PZ的BootLoader,需要对启动文件做出修改,折腾了半宿,弄得事实而非. IAR默认提供了单片机的启动文件,cstart.s或者其他cstartxxx.s, ...

  5. 【转】LAMP网站架构方案分析【精辟】

    [转]LAMP网站架构方案分析[精辟] http://www.cnblogs.com/mo-beifeng/archive/2011/09/13/2175197.html Xubuntu下LAMP环境 ...

  6. Leetcode26--->Remove Duplicates from Sorted Array(从排序数组中移除相同的元素)

    题目: 给定一个排序数组,移除重复出现的元素,保证每个元素最终在数组中只出现一次.返回新数组的长度length; 要求:不能分配额外的一个数组使用,必须使用原地排序的思想,且空间复杂度为O(1) 举例 ...

  7. SCOI 2010 滑雪

    题目描述 a180285非常喜欢滑雪.他来到一座雪山,这里分布着 MM 条供滑行的轨道和 NN 个轨道之间的交点(同时也是景点),而且每个景点都有一编号 ii ( 1 \le i \le N1≤i≤N ...

  8. matlab 初级画图

    matlab 初级画图 1.plot() plot(x,y)   plots each vector pairs (x,y) 画图函数画出每个点   每组变量 plot (y)   plots eac ...

  9. Leetcode 472.连接词

    连接词 给定一个不含重复单词的列表,编写一个程序,返回给定单词列表中所有的连接词. 连接词的定义为:一个字符串完全是由至少两个给定数组中的单词组成的. 示例: 输入: ["cat" ...

  10. 思路清奇:通过 JavaScript 获取移动设备的型号

    我们一般在浏览器里识别用户的访问设备都是通过 User Agent 这个字段来获取的,但是通过它我们只能获取一个大概的信息,比如你用的是 Mac 还是 Windows,用的是 iPhone 还是 iP ...