原题链接: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. 用Python实现小说中的汉字频率统计

     环境: Python 3的代码,亲测可用. 思路: 是先把每个字符提出来放在列表里:再过滤掉其中的标点符号:最后用字典对某个字出现的频率进行累加. 扩展: 用处很多,稍微改改,既可以用来统计小说或文 ...

  2. hdu 5459

    Problem Description I've sent Fang Fang around 201314 text messages in almost 5 years. Why can't she ...

  3. Linux学习-核心编译的前处理与核心功能选择

    硬件环境检视与核心功能要求 根据自己的需求来确定编译的选项 保持干净原始码: make mrproper 我们还得要处理一下核心原始码底下的残留文件才行!假设我们是第一次 编译, 但是我们不清楚到底下 ...

  4. Http协议——基本概念

    一.浏览网页的过程 用户输入一个url,浏览器根据url给web服务器发送一个Request,web服务器接收到Request后进行处理,并返回浏览器一个Response,可以响应一个静态页面或者图片 ...

  5. Angular Vue React 框架中的 CSS

    框架中的 CSS Angular Vue React 三大框架 Angular Vue 内置样式集成 React 一些业界实践 Angular Angular . js (1.x):没有样式集成能力 ...

  6. POJ 3241 曼哈顿距离最小生成树 Object Clustering

    先上几个资料: 百度文库有详细的分析和证明 cxlove的博客 TopCoder Algorithm Tutorials #include <cstdio> #include <cs ...

  7. Http协议中的get和post

    Http中post和get区别,是不是用get的方法用post都能办到? Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符 ...

  8. ThreeJs 基础入门

    本文来自网易云社区 作者:唐钊 Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它在 web 中创建各种三维场景,包括了摄影机.光影.材质等各种对象.使用它可以让我们更加直观的了解 we ...

  9. Eclipse调试程序及项目的导入导出

    Eclipse调试程序 调试概述: ①   调试就是测试程序的方法,主要的目的就是解决程序的逻辑问题,流程是:发现问题.修改问题.正确执行; ②   以前我们可以使用System.out.printl ...

  10. 【bzoj4031】[HEOI2015]小Z的房间 矩阵树定理

    题目描述 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. 你想要打通一 ...