hdu 1045 Fire Net(dfs)

The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them.

题意:

在墙隔断的区域里, 每行每列都必须有一个blockhouses(碉堡);

代码如下~

起先判断的时候,把每行每列从0-n-1都判断了一下,第三个案例老是错,纠结了好久= =!我想应该是要是从头开始判断的话,先遇见X了,就会break掉了,就会少算了。。

智商是硬伤啊。。。。= =!各位机智的小伙伴们应该不会犯这种错吧。。。= =!我好笨。。。

#include <stdio.h>
#include <string.h>
char maze[6][6];
int max, n;
int judge(int x)
{
    int row, col, i;
    row=x/n; col=x%n;
    if(maze[row][col]!='.')
        return 0;
    for(i=row-1; i>-1; i--)
    {
        if(maze[i][col]=='#')
            return 0;
        if(maze[i][col]=='X')
            break;
    }
    for(i=col-1;i>-1; i--)
    {
        if(maze[row][i]=='#')
            return 0;
        if(maze[row][i]=='X')
            break;
    }
    return 1;
}
void dfs(int x, int cc)
{
    int row, col;
    row=x/n; col=x%n;
    if(x==n*n)
    {
        if(max<cc)
            max=cc;
        return ;
    }
    if(judge(x))
    {
        maze[row][col]='#';
        dfs(x+1, cc+1);
        maze[row][col]='.';
    }
    dfs(x+1,cc);
}
int main()
{
    while(~scanf("%d%*c", &n),n)
    {
        int i;
        for(i=0; i<n; i++)
            gets(maze[i]);
        max=0;
        dfs(0,0);
        printf("%d\n",max);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值