NYOJ587blockhouses


题目大意就是说在一个至多为4*4的格子中放置城堡,要求同行、同列只能有一个城堡,除非在同行、同列有墙隔着,给你初始化的地图,问你最多能放置多少个城堡.

思路:深搜的思想,每一个除了是墙的格子,都有放城堡或者不放城堡的选择,然后对每个格子都进行搜索一遍就OK了!!!

代码:
#include <cstdio>
#include <cstring>

char s[10][10];
int ans,n;
int v[10][10];

bool up(int x,int y)
{
    while(x > 0)
    {
        --x;
        if(s[x][y] == '.' && v[x][y])
            return false;
        if(s[x][y] == 'X')
            return true;
    }
    return true;
}
bool down(int x,int y)
{
    while(x < n - 1)
    {
        ++x;
        if(s[x][y] == '.' && v[x][y])
            return false;
        if(s[x][y] == 'X')
            return true;

    }
    return true;
}
bool right(int x,int y)
{
    while(y < n - 1)
    {
        ++y;
        if(s[x][y] == '.' && v[x][y])
            return false;
        if(s[x][y] == 'X')
            return true;
    }
    return true;
}
bool left(int x,int y)
{
    while(y > 0)
    {
        --y;
        if(s[x][y] == '.' && v[x][y])
            return false;
        if(s[x][y] == 'X')
            return true;
    }
    return true;
}
void dfs(int x,int y,int cnt)
{
    if(x == n)
    {
        if(cnt > ans)
            ans = cnt;
        return ;
    }
    if(s[x][y] == '.')
    {
        if(!v[x][y] && up(x,y) && down(x,y) && left(x,y) && right(x,y))
        {
            v[x][y] = 1;
            if(y + 1 == n)
                dfs(x + 1,0,cnt + 1);
            else
                dfs(x,y + 1,cnt + 1);
            v[x][y] = 0;
        }

        if(y + 1 == n)
            dfs(x + 1,0,cnt);
        else
            dfs(x,y + 1,cnt);


    }
    if(s[x][y] == 'X')
    {
        if(y + 1 == n)
            dfs(x + 1,0,cnt);
        else
            dfs(x,y + 1,cnt);
    }
}
int main()
{
    while(~scanf("%d",&n) && n)
    {
        memset(v,0,sizeof(v));
        ans = 0;
        for(int i = 0; i < n; ++i)
            scanf("%s",s[i]);
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j)
                if(s[i][j] == 'X')
                    v[i][j] = 1;
        dfs(0,0,0);
        printf("%d\n",ans);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值