回溯法之Fire Net

 

Fire Net

Suppose that we have a square city withstraight 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 hasfour openings through which to shoot. The four openings are facing North, East,South, and West, respectively. There will be one machine gun shooting througheach opening.

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

The goal is to place as many blockhousesin a city as possible so that no two can destroy each other. A configuration ofblockhouses is legal provided that no two blockhousesare on the same horizontal row or vertical column in a map unless there is atleast one wall separating them. In this problem we will consider small squarecities (at most 4x4) that contain walls through which bullets cannot runthrough.

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

Your task is to write a program that, givena description of a map, calculates the maximum number of blockhouses that canbe placed in the city in a legal configuration.

The input file contains one or more mapdescriptions, followed by a line containing the number 0 that signals the endof the file. Each map description begins with a line containing a positiveinteger n that is the size of the city; n willbe at most 4. The next n lines each describe one row of themap, with a '.' indicating an open space and anuppercase 'X' indicating a wall. There are no spacesin the input file.

For each test case, output one linecontaining the maximum number of blockhouses that can be placed in the city ina legal configuration.

Sample input:

4

.X..

....

XX..

....

2

XX

.X

3

.X.

X.X

.X.

3

...

.XX

.XX

4

....

....

....

....

0

Sample output:

5

1

5

2

4

 

 

解题思路:

本题可以采用回溯法来分析。由于本题的结点数目比较少,因此可以按照从上到下、从左到右的方式去遍历整个字符数组。在判断一个位置是否可以插入bullet 时,只需搜索同行同列前面的字符即可。

具体代码如下:

 

#include<iostream>

using namespace std;

 

#define maxn 6

char value[maxn][maxn];

int n;

int best;

void Init();

void backtrack(introw,int col,intcurrent);

bool isPlaces(introw,int col);

int main()

{

    cin>>n;

    while(n!=0)

    {

        Init();

        backtrack(1,1,0);

        cout<<best<<endl    ;

        cin>>n;

    }

    return 0;

}

void Init()

{

    for(int i=0;i<maxn;i++)

    {

        for(int j=0;j<maxn;j++)

        {

            value[i][j]='.';

        }

    }

    best=0;

    for(int i=1;i<=n;i++)

    {

        for(int j=1;j<=n;j++)

        {

            cin>>value[i][j];

        }

    }

}

void backtrack(introw,int col,intcurrent)

{

    intorow=row;

    intocol=col;

    if(row>n)

    {

        if(current>best)

            best=current;

        return;

    }

    if((value[row][col]=='.')&&(isPlaces(row,col)))

    {

        value[row][col]='o';

        col+=1;

        if(col>n)

        {

            row+=1;

            col=1;

        }

        backtrack(row,col,current+1);

        row=orow;

        col=ocol;

        value[row][col]='.';

    }

    col+=1;

    if(col>n)

    {

        row+=1;

        col=1;

    }

    backtrack(row,col,current);

}

bool isPlaces(introw,int col)

{

    for(int i=row-1;i>0;i--)

    {

        if(value[i][col]=='o')

            returnfalse;

        if(value[i][col]=='.')

            continue;

        if(value[i][col]=='X')

            break;

    }

    for(int i=col-1;i>0;i--)

    {

        if(value[row][i]=='o')

            returnfalse;

        if(value[row][i]=='.')

            continue;

        if(value[row][i]=='X')

            break;

    }

    return true;

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值