您的位置:首页 > 其它

HDU 4499 Cannon (暴力搜索,回溯)

2014-06-30 10:49 387 查看

Cannon

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 332 Accepted Submission(s): 186



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
There is only one line for each test case, containing the maximum number of cannons.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

8
9


题意:给出一个中国象棋棋盘(n*m,0<n,m<=5),已放置了Q个棋子,求在这个棋盘中最多还能放多少炮(任意两个炮不能相互吃掉的情况)

思路:看到这么小的数据,第一眼想到的是暴力

AC代码(1):

#include <stdio.h>
#include <string.h>
#define max(a,b) a>b?a:b;
int mp[10][10],n,m,q,ans;
int cal(int x,int y){
int mark=0,i;
if(mp[x][y]==1)
return 0;
for(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(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 i,j;
for(i=x;i<n;i++){
for(j=0;j<m;j++){
if(x==i&&j<y)
continue;
if(cal(i,j)){
mp[i][j]=2;
dfs(i,j+1,cnt+1);
mp[i][j]=0;
}
}
}
ans=max(ans,cnt);
}
int main(){
int i,x,y;
while(~scanf("%d%d%d",&n,&m,&q)){
memset(mp,0,sizeof(mp));
ans=0;
for(i=0;i<q;i++){
scanf("%d%d",&x,&y);
mp[x][y]=1;
}
dfs(0,0,0);
printf("%d\n",ans);
}
return 0;
}
AC代码(2):

#include <stdio.h>
#include <string.h>
#define max(a,b) a>b?a:b;
int mp[10][10],n,m,q,ans;
int cal(int x,int y){
int mark=0,i;
if(mp[x][y]==1)
return 0;
for(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(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,ty=y%m;//这样表示从棋盘每一行开始从左往右查,少了for循环
if(n==1) tx=0;
if(tx>=n)
return;
ans=max(ans,cnt);
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(){
int i,x,y;
while(~scanf("%d%d%d",&n,&m,&q)){
memset(mp,0,sizeof(mp));
ans=0;
for(i=0;i<q;i++){
scanf("%d%d",&x,&y);
mp[x][y]=1;
}
dfs(0,0,0);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: