BNUOJ 33898 Cannon

Cannon

Time Limit: 1000ms
Memory Limit: 65535KB
This problem will be judged on HDU. Original ID: 4499
64-bit integer IO format: %I64d      Java class name: Main
 
 
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

Source

 
解题:搜索。攻击条件就是一行或一列有三棋,最前和最后的棋是炮就不行,炮棋棋可以,炮棋炮 炮炮炮 不行。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 int n,m,k,ans;
18 int mp[10][10];
19 void dfs(int cur,int has){
20     int x = cur/m;
21     int y = cur%m;
22     if(has > ans) ans = has;
23     if(cur == n*m) return;
24     if(mp[x][y]) {dfs(cur+1,has);return;}
25     dfs(cur+1,has);
26     int i,cnt = 0;
27     for(i = x-1; i >= 0; i--){
28         if(mp[i][y]) cnt++;
29         if(cnt == 2) break;
30     }
31     if(cnt == 2 && mp[i][y] == 2) return;
32     cnt = 0;
33     for(i = y-1; i >= 0; i--){
34         if(mp[x][i]) cnt++;
35         if(cnt == 2) break;
36     }
37     if(cnt == 2 && mp[x][i] == 2) return;
38     mp[x][y] = 2;
39     dfs(cur+1,has+1);
40     mp[x][y] = 0;
41 }
42 int main() {
43     int i,j,x,y;
44     while(~scanf("%d %d %d",&n,&m,&k)){
45        memset(mp,0,sizeof(mp));
46        for(i = 0; i < k; i++){
47             scanf("%d %d",&x,&y);
48             mp[x][y] = 1;
49        }
50        ans = 0;
51        dfs(0,0);
52        printf("%d
",ans);
53     }
54     return 0;
55 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/3926601.html