HDU 4499 Cannon (暴力搜索)


题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮)


炮吃炮:在同一行或同一列且中间有一颗棋子。



#include <stdio.h>  
#include <iostream>  
#include <algorithm>  
#include <string.h>  
#include <queue> 
#include <math.h>  
#define M 50  
#define LL long long  
using namespace std;
using  namespace  std;  
int n,m,t,ans;
int ma[M][M];
bool bfs(int x,int y)//判断x,y能不能放
{
	int xx=x-1;
	int cnt=0;
	while(xx>=0)
	{
		
		if(ma[xx][y]==1&&cnt==1) return false;
		if(ma[xx][y]==1||ma[xx][y]) cnt++;
		xx--;
	}
	xx=x+1;
	cnt=0;
	while(xx<n)
	{
		
		if(ma[xx][y]==1&&cnt==1) return false;
		if(ma[xx][y]==1||ma[xx][y]) cnt++;
		xx++;
	}
	int yy=y-1;
	cnt=0;
	while(yy>=0)
	{
		
		if(ma[x][yy]==1&&cnt==1) return false;
		if(ma[x][yy]==1||ma[x][yy]) cnt++;
		yy--;
	}
	yy=y+1;
	cnt=0;
	while(yy<m)
	{
		
		if(ma[x][yy]==1&&cnt==1) return false;
		if(ma[x][yy]==1||ma[x][yy]) cnt++;
		yy++;
	}
	return true;
}
void dfs(int x,int y,int tmp)
{
	if(tmp>ans) ans=tmp;
	for(int i=x;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(i==x&&j<y) continue;
			if(ma[i][j]!=-1&&bfs(i,j))
			{
				ma[i][j]=1;
				dfs(i,j+1,tmp+1);
				ma[i][j]=0;
			}
		}
	}
}
int main()
{
	  while(~scanf("%d%d%d",&n,&m,&t))
	  {  
        memset(ma,0,sizeof(ma));  
        int  x,y;  
        while (t--)
		{  
            scanf( "%d %d" ,&x,&y);  
            ma[x][y]=-1;  
        }  
        ans=0;  
        dfs(0,0,0);  
        printf("%d\n",ans);  
    }  
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值