hdu 4499 Cannon(暴力)

题目链接: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(暴力)

时间: 2024-12-26 11:25:50

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

hdu 4499 Cannon 暴力dfs搜索

Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 338 Problem Description In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move

HDU 4499 Cannon (暴力搜索)

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

HDU 4499 Cannon (暴力求解)

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

HDU ACM 4499 Cannon 暴力DFS

分析:N和M很小,直接暴力搜索即可. #include<iostream> using namespace std; #define N 6 #define M 6 int vis[N][M]; //有炮设为2,无炮为0,其他为1 int n,m,q,ans; #define max(a,b) ((a)>(b)?(a):(b)) bool Valid(int i,int j) //只需要判断当前和前面的即可(行列) { int fg=0; int k; if(vis[i][j]==1)

HDU 4499.Cannon 搜索

Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 965    Accepted Submission(s): 556 Problem Description In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move

hdu 4499 Cannon dfs

Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 Description In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or vertically along the chess grid. At eac

HDU 4499 Cannon

题意: 思路: #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> #include<vector> #include<queue> #include<stack> #include<algorithm> using namespace std; const int MAXN

hdu 4876(剪枝+暴力)

题意:给定n,k,l,接下来给出n个数,让你从n个数中选取k个数围成一圈,然后从这k个数中随意选出连续的m(m>=1&&m<=k)个数进行异或后得到[l,r]区间的所有值,让你求最大的r. 分析:关键问题是需要剪枝! 代码实现: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #inclu

HDOJ 4499 Cannon

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4499 oder?如何参加? Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 513    Accepted Submission(s): 297 Problem Description In Chinese Chess, th