hdu 4499

来源:互联网 发布:大疆官网模拟软件p 编辑:程序博客网 时间:2024/05/02 23:25

Cannon

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1006    Accepted Submission(s): 577


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.
 

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
题意:给定一个棋盘,和其他的几个棋子。问最多放置多少个炮试两个炮之间不能相互打到。

思路:5x5的图回溯硬暴就行了。

#include<cstdio>#include<cstring>#include<iostream>using namespace std;int n,m;int vis[10][10];int ans;void dfs(int x,int y,int num){    int dy = (y+1)%m;    int dx = x+(y+1)/m;    if(dx == n)    {        ans = max(num,ans);        return ;    }    if(vis[dx][dy])    {        dfs(dx,dy,num);        return ;    }    bool ok = true;    for(int i = 0;i < dx;i++)    {        if(vis[i][dy] == 2)        {            int num = 0;            for(int j = i+1;j < dx;j++)            {                if(vis[j][dy])num++;            }            if(num == 1)                ok = false;        }    }    for(int i = 0;i < dy;i++)    {        if(vis[dx][i] == 2)        {            int num = 0;            for(int j = i+1;j < dy;j++)            {                if(vis[dx][j])num++;            }            if(num == 1)                ok = false;        }    }    if(ok)    {        vis[dx][dy] = 2;        dfs(dx,dy,num+1);        vis[dx][dy] = 0;        dfs(dx,dy,num);    }    else        dfs(dx,dy,num);    return ;}int main(){    int q;    while(scanf("%d%d%d",&n,&m,&q)!=EOF)    {        ans = 0;        memset(vis,0,sizeof(vis));        while(q--)        {            int a,b;            scanf("%d%d",&a,&b);            vis[a][b] = 1;        }        dfs(0,-1,0);        printf("%d\n",ans);    }    return 0;}



0 0
原创粉丝点击