Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1286066
  • 博文数量: 196
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2019年(31)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类:

2009-06-14 20:53:25

Fire Net

Time Limit:1sMemory limit:32M
Accepted Submit:183Total Submit:312

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.

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 integer n 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..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0

Sample output:

5
1
5
2
4

#include <stdio.h>
#include <math.h>
#include <string.h>

#define N 30
int graph[30][30];
int num;

int find(int i, int m, int g[][N], int mat[], int tmat[])
{
     int v,j;
     for(j = 0; j < m; j++)
     {
         if(g[i][j] && tmat[j] == 0)
         {
            tmat[j] = 1;
            v = mat[j];
            mat[j] = i;
            if(v == -1 || find(v, m, g, mat, tmat))
                return 1;
            mat[j] = v;
         }
     }
     return 0;
}
int match(int g[][N],int n,int m)
{
     int i, k=0, tmat[N], mat[N];

     for (i = 0; i < m; i++)
         mat[i] = -1;
     for (i = 0; i < n; i++)
     {
         memset(tmat, 0, sizeof(tmat));
         k += find(i, m, g, mat, tmat);
     }
     return k;
}

int main()
{
    int i, j;
    int fa[6], fb[6], ca, cb;
    char str[10];

    while (scanf("%d", &num) && num)
    {
        ca = cb = 0;
        memset(graph, 0, sizeof(graph));
        for (i = 0; i < num; ++i)
        {
            fa[i] = i;
            fb[i] = i;
        }
        
        for (i = 0; i < num; ++i)
        {
            scanf("%s", &str);
            for (j = 0; j < num; ++j)
            {
                if (str[j] == '.')
                    graph[fa[i]][fb[j]] = 1;
                else
                {
                    fa[i] = num + ca;
                    fb[j] = num + cb;
                    ++ca, ++cb;
                }
            }
        }

        printf("%d\n", match(graph, num + ca, num + cb));
    }

    return 0;
}

阅读(1540) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~