Cannon
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 999 Accepted Submission(s): 573
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.
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.
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
Source
2013 ACM-ICPC吉林通化全国邀请赛——题目重现
大体题意:
给你一个n*m的棋盘,并且告诉你一些已经放好的棋子,让你放置最大的炮的数量,使得任意两个炮不能相互吃掉。
思路:
直接回溯法暴力,dfs真神奇,本以为超时就是不超时,以为不超时结果就是超时= =!
判断每一个可以放棋的位置 是否可以放炮,回溯暴力即可!
坑:有个坑自己卡了许久,就是在放炮的时候向四个方向看能否被吃掉,是四个方向都不能被吃才可以!
注:可以加一个剪枝 会快很多, 就是如果棋盘没有棋子的话,答案就是min(m,n)*2,如果回溯中发现已经到达了这个值,可以剪枝!
详细见代码:
大体题意:
给你一个n*m的棋盘,并且告诉你一些已经放好的棋子,让你放置最大的炮的数量,使得任意两个炮不能相互吃掉。
思路:
直接回溯法暴力,dfs真神奇,本以为超时就是不超时,以为不超时结果就是超时= =!
判断每一个可以放棋的位置 是否可以放炮,回溯暴力即可!
坑:有个坑自己卡了许久,就是在放炮的时候向四个方向看能否被吃掉,是四个方向都不能被吃才可以!
注:可以加一个剪枝 会快很多, 就是如果棋盘没有棋子的话,答案就是min(m,n)*2,如果回溯中发现已经到达了这个值,可以剪枝!
详细见代码:
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int maxn = 10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int a[maxn][maxn];
int n,m,q;
int ans=0;
int tot;
bool vis(int x,int y){
for (int i = x-1; i >= 0; --i){
if (a[i][y] != -1){
for (int j = i -1; j >= 0; --j){
if (a[j][y] != -1){
if (a[j][y] == 0)return true;
//return false;
break;
}
}
break;
}
}
for (int i = x+1; i < n; ++i){
if (a[i][y] != -1){
for (int j = i +1; j < n; ++j){
if (a[j][y] != -1){
if (a[j][y] == 0)return true;
// return false;
break;
}
}
break;
}
}
for (int i = y - 1; i >= 0; --i){
if (a[x][i] != -1){
for (int j = i -1; j >= 0; --j){
if (a[x][j] != -1){
if (a[x][j] == 0)return true;
// return false;
break;
}
}
break;
}
}
for (int i = y + 1; i < m; ++i){
if (a[x][i] != -1){
for (int j = i +1; j < m; ++j){
if (a[x][j] != -1){
if (a[x][j] == 0)return true;
// return false;
break;
}
}
break;
}
}
return false;
}
void dfs(int x,int y,int cur){
if (ans >= tot)return;
for (int i = x; i < n; ++i){
for (int j = ( i == x ? (y+1) : 0 ) ; j < m ; ++j){
if (a[i][j] != -1 || vis(i,j))continue;
a[i][j] = 0;
dfs(i,j,cur+1);
a[i][j] = -1;
}
}
ans = max(ans,cur);
}
int main(){
while(scanf("%d%d%d",&n,&m,&q) == 3){
ans = 0;
tot = min(n,m)*2;
memset(a,-1,sizeof a);
for (int i = 0; i < q; ++i){
int x,y;
scanf("%d%d",&x,&y);
a[x][y] = 1;
}
dfs(0,-1,0);
printf("%d\n",ans);
}
return 0;
}