HDU4499 - Cannon->DFS

G - Cannon
Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 4499

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
 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int N,M,Q,a,b,Max;
int maps[10][10];
void dfs(int x, int y, int step) {
    if(x>=N) {
        Max=max(Max,step);
        return;
    }
    if(y >= M) { //重新从下一行的0开始
        dfs(x+1,0,step);
        return;
    }
    if(maps[x][y]==1) { //若当前位置已经有棋子则不处理,从同行的下一个开始处理
        dfs(x,y+1,step);//
        return;
    }
    dfs(x,y+1,step);//当前位置不选择放东西
    bool success=true;
    int flag=0;
    for(int i=y-1; i>=0; i--) {//检查同行不能有冲突
        if(maps[x][i]==1) {
            flag++;
        }
        if(maps[x][i]==2) {
            if(flag==1) {
                success=false;
                break;
            } else {
                flag++;
            }
        }
    }
    flag=0;
    for(int i=x-1; i>=0; i--) {//检查同列不能有冲突
        if(maps[i][y]==1) {
            flag++;
        }
        if(maps[i][y]==2) {
            if(flag==1) {
                success=false;
            } else {
                flag++;
            }
        }
    }
    if(!success)return;
    maps[x][y]=2;
    dfs(x,y+1,step+1);
    maps[x][y]=0;
}
int main() {
    while(~scanf("%d%d%d",&N,&M,&Q)) {
        memset(maps,0,sizeof(maps));
        for(int i=0; i<Q; i++) {
            scanf("%d%d",&a,&b);
            maps[a][b]=1;
        }
        Max=0;
        dfs(0,0,0);
        printf("%d\n",Max);
    }
    return 0;
}

你可能感兴趣的:(HDU4499 - Cannon->DFS)