ZOJ 1002 Fire Net

来源:互联网 发布:淘宝客隐藏优惠券设置 编辑:程序博客网 时间:2024/04/29 08:01
Fire Net

Time Limit: 2 Seconds    Memory Limit: 65536 KB

Suppose that we have a square city with straight streets. A mapof a city is a square board with n rows and n columns, eachrepresenting a street or a piece of wall.

A blockhouse is a small castle that has four openings throughwhich to shoot. The four openings are facing North, East, South,and West, respectively. There will be one machine gun shootingthrough each opening.

Here we assume that a bullet is so powerful that it can runacross any distance and destroy a blockhouse on its way. On theother hand, a wall is so strongly built that can stop thebullets.

The goal is to place as many blockhouses in a city aspossible so that no two can destroy each other. A configuration ofblockhouses is legal provided that no two blockhousesare on the same horizontal row or vertical column in a map unlessthere is at least one wall separating them. In this problem we willconsider small square cities (at most 4x4) that contain wallsthrough which bullets cannot run through.

The following image shows five pictures of the same board. Thefirst picture is the empty board, the second and third picturesshow legal configurations, and the fourth and fifth pictures showillegal configurations. For this board, the maximum number ofblockhouses in a legal configuration is 5; the second picture showsone way to do it, but there are several other ways.

ZOJ <wbr>1002 <wbr>Fire <wbr>Net

Your task is to write a program that, given a description of amap, calculates the maximum number of blockhouses that can beplaced in the city in a legal configuration.

The input file contains one or more map descriptions, followedby a line containing the number 0 that signals the end of the file.Each map description begins with a line containing a positiveinteger n that is the size of the city; n will be atmost 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 inputfile.

For each test case, output one line containing the maximumnumber of blockhouses that can be placed in the city in a legalconfiguration.

Sample input:

4.X......XX......2XX.X3.X.X.X.X.3....XX.XX4................0

Sample output:

51524

Source: Zhejiang University Local Contest2001
源码:
ZOJ <wbr>1002 <wbr>Fire <wbr>Net
ZOJ <wbr>1002 <wbr>Fire <wbr>Net

解题报告:
这一题和八皇后是一个题型,先尝试,后回溯,不同的是,八皇后回溯的目的是求共有的方法,而这一题的目的是为了能求出所有的方法中的个数最大值。
每次落下一个blockhouse,判断是否空位,还有判断是否legal。
这一类型的解题模板:
ZOJ <wbr>1002 <wbr>Fire <wbr>Net

0 0
原创粉丝点击