转载请注明出处:http://blog.csdn.net/u012860063
题目链接:http://acm.hdu.edu.cn/showproblem.php?
pid=4499
Cannon
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 363 Accepted Submission(s): 214
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.
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.
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
/*题意:
给你一个棋盘,最大是5*5的,问你最多能够放多少个炮,炮和炮之间不能够相互攻击,
这块仅仅的是仅仅能走一步,不存在两个炮中间三个棋子的情况..
给你一个棋盘,最大是5*5的,问你最多能够放多少个炮,炮和炮之间不能够相互攻击,
这块仅仅的是仅仅能走一步,不存在两个炮中间三个棋子的情况..
*/
代码+具体解释:
#include <cstdio>
#include <cstring>
int n, m, ans;
int g[7][7];
int MAX(int a, int b)
{
if(a > b)
return a;
return b;
}
void dfs(int x, int y, int cnt)
{
if(x >= n)//表示已经搜索完成
{
ans = MAX(ans,cnt);
return;
}
if(y >= m)//列出界。表示当前行已经搜索完成
{
dfs(x+1,0,cnt);//又一次从下一行的0開始
return;
}
if(g[x][y] == 1)//若当前位置已经有棋子
{
dfs(x,y+1,cnt);//则从下一个又一次開始搜索
return;
}
dfs(x,y+1,cnt);
int t, flag = 0;
for(t = x-1; t >= 0; t--)//以下的两个for是查找同一列是否存在
{ //前面已经有炮和炮架
if(g[t][y])
{
break;
}
}
for(int i = t-1; i >= 0; i--)
{
if(g[i][y])
{
if(g[i][y] == 2)
{
flag = 1;
}
break;
}
}
if(flag)
{
return;//假设存在上面说得情况,就返回上一层
}
for(t = y-1; t >= 0; t--)//以下的两个for是查找同一行是否存在
{ //前面已经有炮和炮架
if(g[x][t])
break;
}
for(int j = t-1; j >= 0 ; j--)
{
if(g[x][j])
{
if(g[x][j] == 2)
{
flag = 1;
}
break;
}
}
if(flag)
{
return;//假设存在上面说得情况,就返回上一层
}
g[x][y] = 2;//表示此处暂放一个炮
dfs(x,y+1,cnt+1);
g[x][y] = 0;//回溯
}
int main()
{
int Q, u, v, i;
while(~scanf("%d%d%d",&n,&m,&Q))
{
memset(g,0,sizeof(g));
for(i = 0; i < Q; i++)
{
scanf("%d%d",&u,&v);
g[u][v] = 1; //表示開始此处已经有棋子
}
ans = 0;
dfs(0, 0, 0);
printf("%d\n",ans);
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。