Problem : 火力网

Problem : 火力网

Description
给你张N*N的地图,地图上有墙,用”X”表示,有空地,用” . “表示。空地上可以放兵,每个兵可以攻击他的上下左右四条直线范围(不能穿墙)
问在防止互相误伤的前提(任何一个兵不在其他兵射程之内),最多摆放多少个兵。

Input
第一行给出一个数字n
以下n行是N*N的地图(0<=n<=8)

Output
对应每组数据最多能放多少兵,每组数据占一行。

Sample Input
4
.X..
….
XX..
….
Sample Output
5

#include<iostream>
#include<cstring>
using namespace std;
int read() {
    int x=0,f=1;
    char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='.')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())  x=x*10+ch-'0';//快读,将字符转换为数字
    return x*f;
}
bool cnt[105][105];
int n,san,wen;
char a;
void dfs(int x,int y) 
{
    bool dp[105][105];
    if(x>n)     
    {
        if(san>wen)wen=san;
        return ;
    }
    if(cnt[x][y])    
    {
        san++;
        for(int i=1; i<=n; i++)            
         for(int j=1; j<=n; j++)            
          {
                if(cnt[i][j])dp[i][j]=1;
                else dp[i][j]=0;
            }
        int i=y;
        while(i<=n)         
        {
            if(!cnt[x][i])break;
            else cnt[x][i]=0;
            i++;
        }
        i=x+1;
        while(i<=n)         
        {
            if(!cnt[i][y])break;
            else cnt[i][y]=0;
            i++;
        }
        if(y==n)dfs(x+1,1);
        else dfs(x,y+1);
        for(int i=1; i<=n; i++)             
        for(int j=1; j<=n; j++)            
         {
                if(dp[i][j])cnt[i][j]=1;
                else cnt[i][j]=0;
            }
        san--;
    }
    if(y==n)dfs(x+1,1);
    else dfs(x,y+1);
}
int main() 
{
    wen=0;
    san=0;
    memset(cnt,false,sizeof(cnt));
    n=read();
    for(int i=1; i<=n; i++)         
    for(int j=1; j<=n; j++) 
    {
            cin>>a;
            if(a=='.')cnt[i][j]=1;
    }
    dfs(1,1);
    cout<<wen;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41654438/article/details/81428925