您的位置:首页 > 其它

hdu 1045 Fire Net(dfs)

2014-05-12 20:14 344 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: