hdu4499 Cannon DFS

Cannon

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1062    Accepted Submission(s): 608


Problem Description
In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or vertically along the chess grid. At each move, it can either simply move to another empty cell in the same line without any other chessman along the route or perform an eat action. The eat action, however, is the main concern in this problem. 
An eat action, for example, Cannon A eating chessman B, requires two conditions: 
1、A and B is in either the same row or the same column in the chess grid. 
2、There is exactly one chessman between A and B. 
Here comes the problem. 
Given an N x M chess grid, with some existing chessmen on it, you need put maximum cannon pieces into the grid, satisfying that any two cannons are not able to eat each other. It is worth nothing that we only account the cannon pieces you put in the grid, and no two pieces shares the same cell.
 

Input
There are multiple test cases. 
In each test case, there are three positive integers N, M and Q (1<= N, M<=5, 0<=Q <= N x M) in the first line, indicating the row number, column number of the grid, and the number of the existing chessmen. 
In the second line, there are Q pairs of integers. Each pair of integers X, Y indicates the row index and the column index of the piece. Row indexes are numbered from 0 to N-1, and column indexes are numbered from 0 to M-1. It guarantees no pieces share the same cell.
 

Output
There is only one line for each test case, containing the maximum number of cannons.
 

Sample Input
  
  
4 4 2 1 1 1 2 5 5 8 0 0 1 0 1 1 2 0 2 3 3 1 3 2 4 0
 

Sample Output
  
  
8 9
 


类似于八皇后,只是把皇后换成了中国象棋的炮,而且棋盘上原先就有一些棋子,问最后最多能放多少个不相互攻击的炮,数据量很小,直接DFS搜索


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

const int INF = 0x7fffffff;

int n, m, q, ans;
int mp[10][10];

void dfs(int x, int y, int cnt) {
	if (x >= n) {
		ans = max(ans, cnt);
		return;
	}
	if (y >= m) {
		dfs(x + 1, 0, cnt);
		return;
	}
	if (!mp[x][y]) {
		int i;
		bool flag = true;
		for (i = x - 1; i >= 0 && !mp[i][y]; i--) {}
		i--; //因为炮要隔一个棋子攻击
		for (; i >= 0 && !mp[i][y]; i--) {}
		if (i >= 0 && mp[i][y] == 2) flag = false;

		if (flag) {
			for (i = x + 1; i < n && !mp[i][y]; i++) {}
			i++;
			for (; i < n && !mp[i][y]; i++) {}
			if (i < n && mp[i][y] == 2) flag = false;
		}

		if (flag) {
			for (i = y - 1; i >= 0 && !mp[x][i]; i--) {}
			i--;
			for (; i >= 0 && !mp[x][i]; i--) {}
			if (i >= 0 && mp[x][i] == 2) flag = false;
		}

		if (flag) {
			for (i = y + 1; i < m && !mp[x][i]; i++) {}
			i++;
			for (; i < m && !mp[x][i]; i++) {}
			if (i < m && mp[x][i] == 2) flag = false;
		}
		if (flag) {
			mp[x][y] = 2;
			dfs(x, y + 1, cnt + 1); //当前位置放炮
			mp[x][y] = 0;
		}
	}
	//当前位置不放炮,此时原因有三种
	//1.当前位置有棋子了
	//2.当前位置放炮会相互攻击而不放
	//3.故意不放,为后面DFS放其他棋子腾空间
	//但是效果是等价的,都是不放,所以合并为一条
	dfs(x, y + 1, cnt);
}

int main()
{
	while (~scanf("%d%d%d", &n, &m, &q)) {
		memset(mp, 0, sizeof(mp));
		for (int i = 0; i < q; i++) {
			int x, y;
			scanf("%d%d", &x, &y);
			mp[x][y] = 1;
		}
		ans = -INF;
		dfs(0, 0, 0);
		printf("%d\n", ans);
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值