HDU 1045 Fire Net (DFS)

Source: Internet
Author: User

Fire Netproblem Descriptionsuppose, we have 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..
....
2
Xx
. X
3
. X.
x.x
. X.
3
...
. Xx
. Xx
4
....
....
....
....
0 Sample Output5
1
5
2
4 meaning on the map of N*n to prevent as many blockhouse as possible, provided the bunker could not see the other bunker, then there was fire net on the map and could be separated by two bunkers. Answer completely violent Dfs, is to write that check function a little annoying. Unlike the classic Dfs, his parameters are not X and Y, but depth (depth). After each search is complete (no place to place the bunker), compare depth and ans size.
#include <iostream>#include<cstdio>#include<vector>#include<Set>#include<algorithm>using namespacestd;Charmp[ -][ -];intN,ans;BOOLCheckintXinty) {    inti; if(mp[x][y]!='.')return false; if(x>=0)// up         for(I=x; i>=0; i--)            if(mp[i][y]=='X')                 Break; Else if(mp[i][y]=='1')                return false; if(x<n)// Down         for(i=x; i<n; i++)            if(mp[i][y]=='X')                 Break; Else  if(mp[i][y]=='1')                return false; if(y>=0)// Left         for(I=y; i>=0; i--)            if(mp[x][i]=='X')                 Break; Else if(mp[x][i]=='1')                return false; if(y<n)// Right         for(i=y; i<n; i++)            if(mp[x][i]=='X')                 Break; Else if(mp[x][i]=='1')                return false; return true;}voidDfsintm) {    inti,j;  for(i=0; i<n;i++)    {         for(j=0; j<n;j++)        {            if(check (i,j)) {Mp[i][j]='1'; DFS (M+1); MP[I][J]='.'; }}} ans=Max (ans,m);}intMain () { while(cin>>n&&N) {ans=0;  for(intI=0; i<n; i++) Cin>>Mp[i]; DFS (0); printf ("%d\n", ans); }    return 0;}
View Code

HDU 1045 Fire Net (DFS)

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.