首页 > 搜索 > DFS搜索 > HDU 4499-Cannon -DFS-[解题报告]HOJ
2015
07-17

HDU 4499-Cannon -DFS-[解题报告]HOJ

Cannon

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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 

样例输出:

8 
9

题意就是给你一个n*m的棋盘,然后上面已经有了 棋子,并给出这些棋子的坐标,但是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮,但是炮之间不能互相吃,吃的规则我们斗懂得 炮隔山打嘛,问你最多能放几个炮

肯定是搜索了,n,m最大才5,可能挺久没做了,对于回溯反而把握不好了,写了好久调试了好久,才过

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <set>
#include <queue>
#include <stack>
#define INF 999999999
#define eps 0.00001
#define LL long long
#define maxn 1000005
using namespace std;

int mp[10][10];
int tmp[10][10];

int n,m,q;
int ans ;

void init() {
	memset(mp,0,sizeof(mp));
}

int cal(int x,int y) {
	if(mp[x][y] == 1)return 0;
	int mark = 0;
	for(int i=y-1;i>=0;i--) {
		if(mp[x][i] == 1)
			mark++;
		else if(mp[x][i] == 2) {
			if(mark == 1) return 0;
			else mark++;
		}
	}
	mark = 0;
	for(int i=x-1;i>=0;i--) {
		if(mp[i][y] == 1)
			mark++;
		else if(mp[i][y] == 2) {
			if(mark == 1)return 0;
			else mark++;
		}
	}
	return 1;
}

void dfs(int x,int y,int cnt) {
	int tx = x/n;
	if(n == 1)tx = 0;
	int ty = y%m;
	ans = max(ans,cnt);
	if(tx >= n) return;
	if(tx == 0 && y >= n * m)return;
	if(cal(tx,ty)) {
		mp[tx][ty] = 2;
		dfs(x+1,y+1,cnt+1);
		mp[tx][ty] = 0;

		dfs(x+1,y+1,cnt);
	}
	else
		dfs(x+1,y+1,cnt);
}

int main() {
	while(scanf("%d %d %d",&n,&m,&q) == 3) {
		init();
		ans = 0;
		memset(tmp,0,sizeof(tmp));
		while(q--) {
			int x,y;
			scanf("%d %d",&x,&y);
			mp[x][y] = 1;
			tmp[x][y] = 1;
		}
		dfs(0,0,0);
		printf("%d\n",ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/yitiaodacaidog/article/details/35815151