HDU 1045--fire Net —————— "maximum matching, composition, adjacency matrix practices"

Source: Internet
Author: User

Fire NetTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 1045

Description

Suppose that we had a square city with straight streets. A map of a city are a square board with n rows and n columns, each representing a street or a piece of wall.

A blockhouse is a small castle it has four openings through which to shoot. The four openings is facing north, East, south, and West, respectively. There'll be a machine gun shooting through each opening.

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

The goal is-to-place as many blockhouses-a city as possible so, no, and can destroy each of the other. A configuration of blockhouses is legal provided that no, blockhouses is on the same horizontal row or vertical column In a map unless there are at least one wall separating them. In this problem we'll consider small square cities (at most 4×4) that contain walls through which bullets cannot run thr Ough.

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 PI Ctures Show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; The second picture shows one-to-do it, but there is 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.

Input

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

Output

For each test case, output one line containing the maximum number of blockhouses so 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

The main topic: give you the n*n matrix, there are some places in the wall. To put the shells in the empty place, the missiles will attack the upper and lower left and right four directions, the missile attack can not pass through the wall to touch the wall will stop. Ask you how many shells you can put in it.

Problem solving idea: Because of the barrier of the wall, so can not simply divide the ranks directly into two parts. So consider a row, if you encounter a wall, you can think that is already wrapped, let the number of rows plus one. Similarly, the columns are considered. In the sense of us, there is a place where there is a convergence, even one side. To find the maximum match.

#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream>using namespace Std;int vis[110][110], g[110][110], linker[110], Used[110];char map[110][110];bool dfs (int u,int _n) {for (int v = 1; v <= _n;            v++) {if (G[u][v] &&!used[v]) {used[v] = u;                if (linker[v] = = 1 | | DFS (LINKER[V],_N)) {Linker[v] = u;            return true; }}} return false;}    int Hungary (int un,int vn) {int ret = 0;    memset (LINKER,-1,SIZEOF (linker));        for (int i = 1; I <= un; i++) {memset (used,0,sizeof (used));    if (Dfs (I,VN)) ret++; } return ret;}    int main () {int n;            while (scanf ("%d", &n)!=eof&&n) {for (int i = 1; I <= n; i++) {GetChar ();            for (int j = 1; J <= N; j + +) {scanf ("%c", &map[i][j]);        }} memset (Vis,0,sizeof (VIS));        int vn = 0, flag; for (int i = 1; i <= N;            i++) {flag = 1;                for (int j = 1; J <= N; j + +) {if (map[i][j] = = ' X ') {flag = 1;                        }else{if (flag) {vn++;                    Flag = 0;                } Vis[i][j] = vn;        }}} memset (G,0,sizeof (G));        int un = 0;            for (int j = 1; J <= N; j + +) {flag = 1;                for (int i = 1; I <= n; i++) {if (map[i][j] = = ' X ') {flag = 1;                        }else{if (flag) {un++;                    Flag = 0;                } G[un][vis[i][j]] = 1;        }}} int ans = Hungary (UN,VN);    printf ("%d\n", ans); } return 0;}

  

HDU 1045--fire Net —————— "maximum matching, composition, adjacency matrix practices"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.