Hdoj1045--fire Net (binary graph conversion)

Source: Internet
Author: User

Fire Net

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8620 Accepted Submission (s): 4976


Problem Descriptionsuppose 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.

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

Outputfor each test case, output one line containing the maximum number of blockhouses so can be placed in the city in a Legal configuration.

Sample input4.x ... Xx...... 2XX. X3. X.x.x.x.3 .... Xx. XX4 .......... 0

Sample Output51524

Sourcezhejiang University Local Contest 2001

Recommendwe has carefully selected several similar problems for you:1175 1067 1258 1053 10,262 the row and column matching, mainly the processing process;
#include <cstdio>#include<cstring>using namespacestd;Charap[5][5];intgra[5][5], vis[5], dis[5];intdx[5], dy[5], left[5], right[5];intN;structnode{intx, y;} a[5][5];voidPrintf () { for(inti =0; I < n; i++){         for(intj =0; J < N; J + +) {printf ("%c", Ap[i][j]); } printf ("\ n"); }}intx, y;BOOLSearch (inta) {     for(inti =1; I <= y; i++){        if(Gra[a][i] &&!Vis[i]) {Vis[i]=1; if(!dis[i] | |Search (Dis[i])) {Dis[i]=A; return true; }        }    }    return false;}intMain () { while(SCANF ("%d", &n)! = EOF &&N) {         for(inti =0; I < n; i++) {GetChar ();  for(intj =0; J < N; J + +) scanf ("%c", &Ap[i][j]); } x= y =0;  for(inti =0; I < n; i++)//row and column matching;             for(intj =0; J < N; J + +)            {                if(Ap[i][j] = ='.')                  {                    if(J = =0|| ap[i][j-1] =='X') {x++; /***********************/} a[i][j].x=x; }                if(Ap[j][i] = ='.')                {                    if(J = =0|| ap[j-1][i] = ='X') {y++; /***********************/} a[j][i].y=y; }} memset (Gra,0,sizeof(Gra));  for(inti =0; I < n; i++)             for(intj =0; J < N; J + +){                if(Ap[i][j] = ='.'){                    intU =a[i][j].x; intv =a[i][j].y; GRA[U][V]=1; }            }        intAns =0; memset (DIS,0,sizeof(DIS));  for(inti =1; I <= x; i++) {memset (Vis,0,sizeof(VIS)); if(Search (i)) ans++; } printf ("%d\n", ans); }    return 0;} 

A mob search;

#include <cstdio>intN, CNT;Charmap[5][5];BOOLBuild (intRowintCol) {    intI, J;  for(inti = row; I >=0; i--){        if(Map[i][col] = ='O')            return false; if(Map[i][col] = ='X')             Break; }     for(inti = col; I >=0; i--){        if(Map[row][i] = ='O')            return false; if(Map[row][i] = ='X')             Break; }    return true ;} voidDfs (intIintnum) {    if(i = = nN) {    //printf ("%d%d\n", I, num);        if(Num >CNT) CNT=num; return; }    Else{        introw = I/N; intCol = i%N; if(Map[row][col] = ='.'&&Build (Row, col)) {Map[row][col]='O'; Dfs (i+1, num+1); Map[row][col]='.'; } Dfs (I+1, num); }}intMain () { while(SCANF ("%d", &N), N) {cnt=0;  for(inti =0; I < n; i++) scanf ("%s", Map[i]); Dfs (0,0); printf ("%d\n", CNT); }        return 0;}

Hdoj1045--fire Net (binary graph conversion)

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.