题目链接:hdu 4499 Cannon

题目大意:给出一个n*m的棋盘,上面已经存在了k个棋子,给出棋子的位置,然后求能够在这种棋盘上放多少个炮,要求后放置上去的炮相互之间不能攻击。

解题思路:枚举行放的情况,用二进制数表示,每次放之前推断能否放下(会不会和已经存在的棋子冲突),放下后推断会不会互相攻击的炮,仅仅须要对每一个新加入的炮考虑左边以及上边就能够了。

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = 10;
int c, si[N*5]; inline int bitCount(int x) {
return x == 0 ? 0 : bitCount(x/2) + (x&1);
} void mkdir() {
c = 0;
for (int i = 0; i < (1<<5); i++) {
if (bitCount(i) <= 3)
si[c++] = i;
}
} int n, m, k, ans, g[N][N], tp; void init () {
memset(g, 0, sizeof(g));
ans = 0;
tp = (1<<m)-1;
int x, y;
for (int i = 0; i < k; i++) {
scanf("%d%d", &x, &y);
g[x][y] = 1;
}
} inline bool judgeSet(int d, int s) {
for (int i = 0; i < m; i++)
if ((s&(1<<i)) && g[d][i])
return false;
return true;
} inline void Set(int d, int s, int val) {
for (int i = 0; i < m; i++)
if (s&(1<<i))
g[d][i] = val;
} inline bool checkup(int x, int y) {
int flag = 0;
for (int i = x - 1; i >= 0; i--) {
if (flag && g[i][y] == 2)
return true;
else if (flag && g[i][y] == 1)
return false;
else if (g[i][y])
flag = 1;
}
return false;
} inline bool checklf(int x, int y) {
int flag = 0;
for (int i = y - 1; i >= 0; i--) {
if (flag && g[x][i] == 2)
return true;
else if (flag && g[x][i] == 1)
return false;
else if (g[x][i])
flag = 1;
}
return false;
} bool judgeOk(int d) {
for (int i = 0; i < m; i++) {
if (g[d][i] == 2) {
if (checkup(d, i))
return false;
if (checklf(d, i))
return false;
}
}
return true;
} void dfs(int d, int cnt) { if (ans >= (n-d)*3+cnt)
return; if (d >= n) {
ans = max(ans, cnt);
return;
} for (int i = 0; i < c; i++) { if (si[i] > tp)
continue; if (judgeSet(d, si[i])) {
Set(d, si[i], 2); if(judgeOk(d)) {
dfs(d+1, cnt + bitCount(si[i]));
} Set(d, si[i], 0);
}
}
} int main () {
mkdir();
while (scanf("%d%d%d", &n, &m, &k) == 3) {
init();
dfs(0, 0);
printf("%d\n", ans);
}
return 0;
}

hdu 4499 Cannon(暴力)的更多相关文章

  1. HDU 4499 Cannon (暴力求解)

    题意:给定一个n*m个棋盘,放上一些棋子,问你最多能放几个炮(中国象棋中的炮). 析:其实很简单,因为棋盘才是5*5最大,那么直接暴力就行,可以看成一行,很水,时间很短,才62ms. 代码如下: #i ...

  2. HDU 4499 Cannon (暴力搜索)

    题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每一个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include & ...

  3. hdu 4499 Cannon dfs

    Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 D ...

  4. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  5. HDU 4499 Cannon (搜索)

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  6. HDU 5091---Beam Cannon(线段树+扫描线)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...

  7. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  8. HDU 5522 Numbers 暴力

    Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5522 ...

  9. hdu 5077 NAND(暴力打表)

    题目链接:hdu 5077 NAND 题目大意:Xiaoqiang要写一个编码程序,然后依据x1,x2,x3的值构造出8个字符.如今给定要求生成的8个字符.问 说Xiaoqiang最少要写多少行代码. ...

随机推荐

  1. Scene is unreachable due to lack of entry points and does not have an identifier for runtime access

    使用Storyboard时出现以下警告: warning: Unsupported Configuration: Scene is unreachable due to lack of entry p ...

  2. Delphi PChar与String互转

    1.String转化成PChar 例: var str: string; pStr:PChar; ... pStr := PChar(str); 2.PChar转String 例: var pStr: ...

  3. IDEA中添加各种依赖pom.xml文件内容

    刚实习的小白,今天准备进入项目,纳尼,前辈把框架什么的都搭建好了,默默的抹了一把辛酸泪,刚刚接触自学框架的时候,添加依赖的时候总是各种问题,让前辈发给我之后,才发现人家写的代码相当优美了.下面就是前辈 ...

  4. java记事本

    新知识点 1.撤销 textArea添加一个实现监听接口的类(添加了之后可以一直监视着添加的删除的情况,以便来撤销 textArea.getDocument().addUndoableEditList ...

  5. 高性能IO设计模式之阻塞/非阻塞,同步/异步解析

    提到高性能,我想大家都喜欢这个,今天我们就主要来弄明白在高性能的I/O设计中的几个关键概念,做任何事最重要的第一步就是要把概念弄的清晰无误不是么?在这里就是:阻塞,非阻塞,同步,异步. OK, 现在来 ...

  6. Java学习----方法的重载

    一个类中有多个同名的参数不一样的方法. 作用:可以根据不同的条件调用不同的方法. 注意:java不会因为方法的返回类型或者权限的不同而判断为不同的两个方法. public class Student ...

  7. Android学习----ADB

    adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.a ...

  8. java jdbc 连接mysql 数据库

    JDBC连接MySQL 加载及注册JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); Class.forName("com. ...

  9. hdu1003 Max Sum(经典dp )

      A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descr ...

  10. Codeforces 301_div.2_Ice Cave(BFS走冰块)

    Ice Cave Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Descripti ...