• 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
    ",ans);  
        }  
    }
    


  • 相关阅读:
    高速排序
    Android 中实现分享和第三方登陆---以新浪微博为例
    linux下用mail发送邮件
    hello.c内核模块编译 -- linux内核
    最小生成树
    JDBC连接Oracle数据库
    解决方案:zabbix(WEB界面的提供分布式系统监视以及网络监视功能)
    Grunt:grunt-tmod
    打包器:Webpack
    模板:art-template-loader
  • 原文地址:https://www.cnblogs.com/yfceshi/p/6902241.html
Copyright © 2020-2023  润新知