ZOJ Problem Set - 1002
2010-02-16 21:30
267 查看
Fire Net
Time Limit: 1 Second Memory Limit: 32768 KB
Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
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 pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=0000%2F1002%2F1002.gif)
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.
The 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 integer n that is the size of the city; n will be at most 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 input file.
For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
Sample input:
Sample output:
Time Limit: 1 Second Memory Limit: 32768 KB
Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
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 pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=0000%2F1002%2F1002.gif)
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.
The 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 integer n that is the size of the city; n will be at most 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 input file.
For each test case, output one line containing the maximum number of blockhouses that 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
#include<stdio.h> #define BLOCK 79 #define WALL 88 #define STREET 46 int MAX; int Check(char map[][5], int n, int x, int y) { int i, j; for(i=x, j=y-1; j>=0; j--) /* left */ { if(map[i][j]==WALL) break; if(map[i][j]==BLOCK) return 0; /* illegal */ } for(i=x-1, j=y; i>=0; i--) /* up */ { if(map[i][j]==WALL) break; if(map[i][j]==BLOCK) return 0; /* illegal */ } return 1; /* legal */ } int Search(char map[][5], int n, int k, int count) { int x, y; if(k==n*n) { if(count>MAX) MAX=count; } else { x=k/n; y=k%n; if(map[x][y]==STREET && Check(map, n, x, y)) { map[x][y]=BLOCK; /* build block */ Search(map, n, k+1, count+1); map[x][y]=STREET; /* recall */ } Search(map, n, k+1, count); } } int main() { int i, n; char map[4][5]; scanf("%d", &n); while(n) { for(i=0; i<n; i++) scanf("%s", map[i]); Search(map, n, 0, 0); printf("%d\n", MAX); MAX=0; scanf("%d", &n); } }
相关文章推荐
- ZOJ Problem Set - 1002(DFS)
- ZOJ Problem Set - 1002 Fire net
- ZOJ Problem Set - 1002(DFS)
- ZOJ Problem Set - 1002 Fire Net (Python)
- ZOJ Problem Set - 1002 Fire Net
- ZOJ Problem Set 1002: Fire Net
- ZOJ Problem Set - 1002 Fire Net
- ZOJ Problem Set - 1002
- ZOJ Problem Set - 2412
- ZOJ Problem Set - 3666 Alice and Bob(SG)
- ZOJ Problem Set - 1383 Binary Numbers
- ZOJ Problem Set - 1109 Language of FatMouse
- ZOJ Problem Set - 1512 (Water Treatment Plants)
- ZOJ Problem Set - 1078(十进制对任意进制转换)
- ZOJ Problem Set – 1048 Financial Management
- ZOJ Problem Set – 1016 Parencodings
- ZOJ Problem Set - 1216 Deck
- ZOJ Problem Set - 3860Find the Spy (STL-> map 建立简单查询表 )
- ZOJ Problem Set - 1008
- ZOJ Problem Set - 1009