blockhouses

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述
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.

输入
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.
样例输入
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
样例输出
5
1
5
2
4
来源
2012年10月份月赛(高年级组)

x代表墙,其余的地方建造炮弹,每一行,每一列只能有一个,除非他们中间有墙

AC代码:

#include <bits/stdc++.h>
using namespace std;
char mp[5][5];
int n,i,j;
int maxn;
int flag;
int cha(int x,int y){flag=1;for(i=x;i>=0;i--){         //查询同列的上边是否有blockhouses if(mp[i][y]=='X')break;if(mp[i][y]=='b'){flag=0;break;}}for(i=y;i>=0;i--){         //查询同行的左边是否有blockhousesif(mp[x][i]=='X')break;if(mp[x][i]=='b'){flag=0;break;}}return flag;
}
int DFS(int a,int b){int i=a/n,j=a%n;if(a==n*n){   //控制搜索结束 maxn=max(maxn,b);return maxn;}if(mp[i][j]=='.'&&cha(i,j)==1){mp[i][j]='b';DFS(a+1,b+1);mp[i][j]='.';}DFS(a+1,b);
}
int main()
{while(scanf("%d",&n),n){memset(mp,0,sizeof(mp));for(i=0;i<n;i++)scanf("%s",mp[i]);maxn=0;printf("%d\n",DFS(0,0));}return 0;
}

blockhouses相关推荐

  1. HDU1045 Fire Net 递归回溯

    点击打开链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. 【HDU - 1045】Fire Net (dfs 或二分图)

    题干: Suppose that we have a square city with straight streets. A map of a city is a square board with ...

  3. 【Acm】算法之美—Fire Net

    题目概述:Fire Net Suppose  that we have a square city with straight streets. A map of a city is a square ...

  4. zoj 1002 Fire Net(DFS~ 哈哈哈哈,终于过了!!!)

    1002 从一开始做ACM就郁闷的一道题,在1001下面它应该觉得压力很大啊!!哈哈.. 做完八皇后,党说这个能做了... 嘿嘿.这题是找放置最多数目的blockhouses(碉堡..)使之不能同行同 ...

  5. HDU-1045 Fire Net(最大碉堡数)

    传送门 Problem Description Suppose that we have a square city with straight streets. A map of a city is ...

  6. ACM--DFS--最大碉堡数--HDOJ 1045--Fire Net

    HDOJ题目地址:传送门 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  7. 【2018.07.29】(深度优先搜索/回溯)学习DFS算法小记

    参考网站:https://blog.csdn.net/ldx19980108/article/details/76324307 这个网站里有动态图给我们体现BFS和DFS的区别:https://www ...

最新文章

  1. Boost--Graph
  2. Redis源码剖析(九)对象系统概述
  3. 令人惋惜的天才新秀:16岁上剑桥大学,27岁就出名,数学事业一路畅通无阻,但自从结婚后,人生从此翻天覆地······
  4. (22)FPGA面试技能提升篇(MicroBlaze、PowerPC)
  5. 写给人类的机器学习 翻译完成
  6. 服务器与普通电脑的区别?
  7. python实践项目(二)
  8. 2022牛客寒假算法基础集训营1 签到题7题
  9. 职场奇袭!3分钟完成一天工作
  10. AM5728 IPC机制解析
  11. python代码怎么弄颜色_python的颜色显示
  12. linux i3 桌面,Linux 桌面平铺管理器 - i3wm
  13. 基于matlab道路交通标志牌,基于MATLAB的道路交通标志识别
  14. 〖四信智慧方案〗LoRa无线技术在输电监测中的应用
  15. ftp-cmd常用命令
  16. 状态方程简约为能控能观型
  17. GREAT-UPD 开源软件使用笔记
  18. 三极管工作原理_10分钟分析稳压三极管工作原理
  19. 2022智源大会议程公开 | 视觉模型论坛
  20. iar stm32_STM32强大的生态,在这里一起总结!

热门文章

  1. Call Exec in PeopleCode
  2. getRemoteAddr()和getRemoteHost() 区别
  3. [BZOJ] 3191 [JLOI2013]卡牌游戏
  4. TestNG测试带参构造函数的类
  5. 在linux查看内存的大小
  6. 人生第一次删好友,删的就是你!连路飞都怒了!
  7. MySQL性能调优的14板斧
  8. 设计模式--原型模式
  9. 支持将数据导出到Excel文档的时候设置单元格格式的.NET控件Spire.DataExport
  10. Revit Family API 添加参数与尺寸标注