HDU 1045 Fire Net (pinch to find the maximum match)

Source: Internet
Author: User
Tags mapr

Problem 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 with a legal configuration.  input:the Input file contains one or more map descriptions, fol Lowed by a line containing the number 0 that signals the end of the 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 all test case, Output one line containing the maximum number of blockhouses that can is 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  Test Instructions: N-rows of streets, streets '. ' The open space, ' X ' represents an obstacle, now requires building a house on the street, but if there are no obstructions in each row, there can be only one house, and if there are obstructions, it may not just be possible to build a house, ask for a few houses to  1. Two points matching:
 Define a continuous non-obstacle block that cannot be extended to the left and right, and a continuous non-obstacle grid that cannot be extended to the upper and lower for the column connected block we set the row connected block to the x set, the column connected block corresponds to the y set, and if a row connected block has an intersection with a column connected block and a space, Then corresponding in the binary diagram there is an edge of the set of the house corresponding to a matching dichotomy, any two houses can not be co-existing in a row or column connected block, so the maximum number of houses = maximum number of matches #include<stdio.h>#include<string.h>#include<algorithm>#defineN 10using namespacestd;CharMap[n][n];intG[n][n];intUse[n], vis[n];intN, RN, CN;intFind (intu) {    inti;  for(i =1; I <= cn; i++)    {        if(!vis[i] &&G[u][i]) {Vis[i]=1; if(!use[i] | |Find (Use[i])) {Use[i]=u; return 1; }        }    }    return 0;}intMain () {intI, J, R, C, ans; intMapr[n][n], mapc[n][n];  while(SCANF ("%d", &N), N) {R= c = RN = CN = ANS =0; memset (G,0,sizeof(G)); memset (use,0,sizeof(use)); memset (MAPR,0,sizeof(MAPR)); memset (MAPC,0,sizeof(MAPC));  for(i =0; I < n; i++) scanf ("%s", Map[i]);  for(i =0; I < n; i++)             for(j =0; J < N; J + +)                if(Map[i][j] = ='X') Mapr[i][j]= Mapc[i][j] =-1;  for(i =0; I < n; i++)        {             for(j =0; J < N; J + +)            {                 while(Mapr[i][j] = =-1&& J <N) J++; R++;  while(Mapr[i][j]! =-1&& J <N) {mapr[i][j]= R;//establishing line-connected blocks                    if(Rn < r) rn =R; J++; }            }        }         for(j =0; J < N; J + +)        {             for(i =0; I < n; i++)            {                 while(Mapc[i][j] = =-1&& I <N) I++; C++;  while(Mapc[i][j]! =-1&& I <N) {mapc[i][j]= C;//establishing a column-connected block                    if(CN < C) CN =C; I++; }            }        }         for(i =0; I < n; i++)             for(j =0; J < N; J + +)                if(Mapr[i][j]! =-1&& Mapc[i][j]! =-1) G[mapr[i][j]][mapc[i][j]]=1;//re-framing         for(i =1; I <= RN; i++) {memset (Vis,0,sizeof(VIS)); if(Find (i)) ans++; } printf ("%d\n", ans); }    return 0;}

2. The problem can also be violent search:

#include <stdio.h>#include<algorithm>#defineN 10using namespacestd;CharMap[n][n];intN, Max;intJudge (intXintY//determine if the row has been built or has an obstruction{    inti;  for(i = x1; I >=0; i--)    {        if(Map[i][y] = ='#')//If you build a house, you can't build it again .            return 0; if(Map[i][y] = ='X')//If there are obstructions, they can be built again.             Break; }     for(i = y1; I >=0; i--)    {        if(Map[x][i] = ='#')            return 0; if(Map[x][i] = ='X')             Break; }    return 1;}voidDFS (intSintC//S represents the number of steps to walk, c means that you can build several houses{    intx, y; Max=Max (Max, c); if(s = = n*n)return ; X= S/n;//x indicates the rowy = s% n;//y indicates that the column    if(Map[x][y] = ='.'&&Judge (x, y)) {Map[x][y]='#'; DFS (S+1, c+1);//If this can build a house, then the number of steps increases and the total number of houses increasesMap[x][y] ='.'; } DFS (S+1, c);//even if this point cannot build a house, the number of steps will still increase}intMain () {inti;  while(SCANF ("%d", &N), N) { for(i =0; I < n; i++) scanf ("%s", Map[i]); Max=0; DFS (0,0); printf ("%d\n", Max); }    return 0;}

HDU 1045 Fire Net (pinch to find the maximum match)

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.