2015
07-17

# 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

#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;
}