Fire Net zoj BFS的活学活用

来源:互联网 发布:淘宝修改论文靠谱吗 编辑:程序博客网 时间:2024/06/18 07:32
Fire Net

Time Limit: 2 Seconds Memory Limit: 65536 KB

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 islegal 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.

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

The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integern that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.

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

Sample input:

4.X......XX......2XX.X3.X.X.X.X.3....XX.XX4................0

Sample output:

51524
如果有时有人问一个搞acm搞了快一年的人,你会搜索不?很多人可能会说我会的,这个很简单,难不倒我,我之前也是这么想的
但是今日我发现自己对于搜索中BFS和DFS理解并不深入,而是在一个很浅的程度上,这个题目求能放进去的最大碉堡数,凡是能
放进去的碉堡都变成'D",这个题目中深度就是能放进去的碉堡数目,为什么呢?很简单,如果我们每次都遍历这个图,都会尽量
忘每个节点放入,每个节点一旦放入之后都有自己可以进行到最大深度,也就是放进去的最大碉堡数,还是比较好理解,但是不好想出来。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char map[10][10];int num;//最大的深度,也就是能放最多碉堡的程度;int n;int judge(int x,int y){    int i,j;    if(map[x][y]!='.')    return 0;    if(x>=0)    {        for(i=x-1;i>=0;i--)        {            if(map[i][y]=='X')            break;            else            if(map[i][y]=='D')            return 0;        }    }    if(x<n)    {        for(i=x+1;i<n;i++)        {            if(map[i][y]=='X')            break;            else            if(map[i][y]=='D')            return 0;        }    }    if(y>=0)    {        for(i=y-1;i>=0;i--)        {            if(map[x][i]=='X')            break;            else            if(map[x][i]=='D')            return 0;        }    }    if(y<n)    {         for(i=y+1;i<n;i++)        {            if(map[x][i]=='X')            break;            else            if(map[x][i]=='D')            return 0;        }    }    return 1;}void DFS(int d){    int i,j;    for(i=0; i<n; i++)    {       for(j=0;j<n;j++)       {           if(judge(i,j))           {               map[i][j]='D';               DFS(d+1);               map[i][j]='.';           }       }    }    num=(num>d?num:d);}int main(){    int i,j,k;    while(scanf("%d",&n)!=EOF)    {        if(n==0)            break;            for(i=0;i<7;i++)            {                for(j=0;j<7;j++)                {                    map[i][j]='.';                }            }        for(i=0; i<n; i++)            scanf("%s",map[i]);        num=0;        DFS(0);        printf("%d\n",num);    }    return 0;}


                                             
0 0
原创粉丝点击