HDUOJ---------(1045)Fire Net
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5175 Accepted Submission(s): 2908
Problem Description
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.
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.
Input
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.
Output
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..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
Sample Output
5
1
5
2
4
Source
Zhejiang University Local Contest 2001
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #define maxn 10
5 char maze[maxn][maxn];
6 int ans,n,count,max;
7 /*用p作为人的标记*/
8 /*判断竖横是否有城堡*/
9 bool judge(int x,int y)
10 {
11 int i;
12 bool flag=true;
13 for(i=y+1;i<=n;i++)
14 {
15 if(maze[x][i]=='P')
16 {
17 flag=false;
18 break;
19 }
20 else
21 if(maze[x][i]=='X')
22 break;
23 }
24 if(!flag)
25 return false;
26 for(i=y-1;i>=1;i--)
27 {
28 if(maze[x][i]=='P')
29 {
30 flag=false;
31 break;
32 }
33 else
34 if(maze[x][i]=='X')
35 break;
36 }
37 if(!flag)
38 return false;
39 for(i=x+1;i<=n;i++)
40 {
41 if(maze[i][y]=='P')
42 {
43 flag=false;
44 break;
45 }
46 else if(maze[i][y]=='X')
47 break;
48 }
49 if(!flag)
50 return false;
51 for(i=x-1;i>=1;i--)
52 {
53 if(maze[i][y]=='P')
54 {
55 flag=false;
56 break;
57 }
58 else if(maze[i][y]=='X')
59 break;
60 }
61 if(!flag)
62 return false;
63 else
64 return true;
65 }
66 void dfs()
67 {
68 for(int i=1;i<=n;i++)
69 {
70 for(int j=1;j<=n;j++)
71 {
72 if(maze[i][j]=='.'&&true==judge(i,j))
73 {
74 count++;
75 maze[i][j]='P';
76 dfs();
77 if(count>ans)
78 ans=count;
79 maze[i][j]='.';
80 count--;
81 }
82 }
83 }
84 }
85 int main()
86 {
87 int i,j;
88 while(scanf("%d",&n),n)
89 {
90 getchar();
91 max=0;
92 for( i=1;i<=n;i++)
93 {
94 for( j=1;j<=n;j++)
95 {
96 scanf("%c",&maze[i][j]);
97 }
98 getchar();
99 }
100 for(i=1;i<=n;i++)
101 {
102 for(j=1;j<=n;j++)
103 {
104 if(maze[i][j]!='X')
105 {
106 count=1;
107 ans=0;
108 maze[i][j]='P';
109 dfs();
110 maze[i][j]='.';
111 if(ans==0) max=1;
112 else
113 if(ans>max) max=ans;
114 }
115 }
116 }
117 printf("%dn",max);
118 }
119 return 0;
120 }
思路: 每次涂一个点,然后生成一张图,将其作为一张新图,又重新开始涂点.....周而复始.....
并不断记录点的最大个数.用栈,或者回溯都可以实现.....
- HDU 1575 Tr A(矩阵快速幂)
- Python之内置函数
- Python中的logger和handler到底是个什么鬼
- 1570. [POJ3461]乌力波
- biztalk rosettanet 自定义 pip code
- Python之线程
- 3555: [Ctsc2014]企鹅QQ
- 【实战】RFID Hacking(1):看我如何突破门禁潜入FreeBuf大本营
- P2885 [USACO07NOV]电话线Telephone Wire
- 实战-Fluxion与wifi热点伪造、钓鱼、中间人攻击、wifi破解
- 【下载】PyTorch实现的神经网络翻译框架——机器翻译工具包 nmtpytorch
- P2605 [ZJOI2010]基站选址
- MYSQL之索引原理与慢查询优化
- MYSQL之视图、触发器、存储过程、函数、事物、数据库锁和数据库备份
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法