ZOJ Problem Set - 1002
Fire Net

Time Limit: 2 Seconds      Memory Limit: 65536 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 legalprovided 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.

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..
....
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
 
思路:由于数据量很少,直接执行DFS暴力搜索每一个点
 #include <iostream>
#include <cstdio> using namespace std; const int SZ = ;
int n;
char map[SZ][SZ];
bool vis[SZ][SZ]; int DFS(int y, int x)
{
if(y == n) return ;
if(map[y][x] == 'X')
{
if(x < n - ) return DFS(y, x + );
else return DFS(y + , );
}
bool ok = true;
for(int i = x - ; i > -; i--)
{
if(map[y][i] == 'X') break;
else if(vis[y][i])
{
ok = false;
break;
}
}
if(ok)
{
for(int i = y - ; i > -; i--)
{
if(map[i][x] == 'X') break;
else if(vis[i][x])
{
ok = false;
break;
}
}
}
if(ok)
{
int add0 = , add1 = ;
if(x == n - )
add0 += DFS(y + , );
else
add0 += DFS(y, x + );
vis[y][x] = true;
if(x == n - )
add1 += DFS(y + , );
else
add1 += DFS(y, x + );
vis[y][x] = false;
return add0 > add1 ? add0 : add1;
}
else
{
if(x < n - ) return DFS(y, x + );
else return DFS(y + , );
}
} int main()
{
char ch;
while(scanf("%d", &n) && n)
{
for(int i = ; i < n; i++)
{
scanf("%s", map[i]);
}
memset(vis, false, sizeof(vis));
printf("%d\n", DFS(, ));
}
return ;
}

最新文章

  1. Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)
  2. Ninject之旅之九:Ninject上下文绑定(附程序下载)
  3. 你真的了解UIControl吗?
  4. .net实现微信公众账号接口开发
  5. JQUERY 拖拽 draggable droppable resizable selectable sortable
  6. 第一章 Collections 类、泛型类和Timing类概述
  7. (转载)OC学习篇之---Foundation框架中的NSDirctionary类以及NSMutableDirctionary类
  8. Tomcat &amp; Nginx
  9. Unity脚本获取内存和FPS
  10. 01-资料管理器(Directory/DirectoryInfo操作文件夹类)
  11. robin 今日南
  12. STM32开发指南-DMA
  13. 阿里云安装wordpress遇到的问题
  14. 【安富莱二代示波器教程】第17章 附件B---功能扩展和改进方向
  15. map中的count方法
  16. 2017-2018-2 20165312 实验三《敏捷开发与XP实践》实验报告
  17. vs2013——单元测试&amp;&amp; 性能图
  18. 实现一个简单的shell
  19. 算法 数组中出现次数最多的数字 MD
  20. 规划行业GIS云平台“城智图”上线运行

热门文章

  1. 软工实践-Alpha 冲刺 (8/10)
  2. 面试Tips
  3. python接口自动化测试框架实现之字符串插入变量(字符串参数化)
  4. laravel获取当前认证用户登录
  5. Delphi通过ADO链接数据库及对数据库的增加,删除,修改,读取操作实例教程4
  6. 【前端学习笔记02】JavaScript字符串、数组的一些操作方法
  7. 【Python】Python基础教程系列目录
  8. hdu 6435 CSGO(最大曼哈顿距离)
  9. jquery 添加与删除的规律 当要添加时候要定位到自己的父元素 当要删除时候 通过事件函数传入的this找到自己的父元素进行删除
  10. hadoop跑第一个实例过程