您的位置:首页 > 其它

HDU 1045 Fire Net(DFS)

2016-03-30 21:32 441 查看

Fire Net

[align=left]Problem Description[/align]
Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.

A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.

Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.

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. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.

The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.

#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
char mp[120][120];
int n,ans;
bool check(int x,int y)
{
int i;
if(mp[x][y]!='.') return false;
if(x>=0)//up
for(i=x; i>=0; i--)
if(mp[i][y]=='X')
break;
else if(mp[i][y]=='1')
return false;
if(x<n)//down
for(i=x; i<n; i++)
if(mp[i][y]=='X')
break;
else  if(mp[i][y]=='1')
return false;
if(y>=0)//left
for(i=y; i>=0; i--)
if(mp[x][i]=='X')
break;
else if(mp[x][i]=='1')
return false;
if(y<n)//right
for(i=y; i<n; i++)
if(mp[x][i]=='X')
break;
else if(mp[x][i]=='1')
return false;
return true;
}
void dfs(int m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(check(i,j))
{
mp[i][j]='1';
dfs(m+1);
mp[i][j]='.';
}
}
}
ans=max(ans,m);
}
int main()
{
while(cin>>n&&n)
{
ans=0;
for(int i=0; i<n; i++)
cin>>mp[i];
dfs(0);
printf("%d\n",ans);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: