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.

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

题目大意:

假设我们有一个正方形的城市,其街道都是直的。在方形的地图上,有n行和n列,每个代表一条街道或一堵墙。每个碉堡有4个射击孔,分别正对东西南北方向。在每个射击孔配置一架高射机枪。

我们假设,子弹是如此强大,它的射程可以任意远,并能摧毁它射中的碉堡。另外,墙也是很坚固的,可以挡住子弹!

其目标是,在该城市布置尽可能多的碉堡,而碉堡之间又不好互相摧毁。合理布置碉堡的原则是:没有两个碉堡在一个水平方向或垂直方向,除非它们之间有墙相隔!

在本题中,假设城市很小,(n最大为4) ,而且子弹不能贯穿墙壁。

输入n,代表n行n列。

n为0是输入结束。

‘.’代表空地,’X’代表墙壁。

因为题目规模很小,直接采用深度优先算法就可以解决。

  1. import java.util.Scanner;
  2. public class Main{
  3. static int n;
  4. static int time;
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. while(sc.hasNext()){
  8. n =sc.nextInt();
  9. if(n==0){
  10. return ;
  11. }
  12. char[][] map = new char[n][n];
  13. for(int i=0;i<n;i++){
  14. String str = sc.next();
  15. for(int j=0;j<n;j++){
  16. map[i][j] = str.charAt(j);
  17. //System.out.print("a"+map[i][j]);
  18. }
  19. //System.out.println();
  20. }
  21. time=0;
  22. dp(map,0,0);
  23. System.out.println(time);
  24. }
  25. }
  26. /**
  27. * 利用深搜的方法,对每个单元格能否放置碉堡进行判断
  28. * @param map
  29. * @param k
  30. * @param count
  31. */
  32. private static void dp(char[][] map, int k, int count) {
  33. int x,y;
  34. if(k==n*n){//整个地图判断完毕
  35. if(count>time){//是否有更大的值
  36. time=count;
  37. return ;
  38. }
  39. }else{
  40. //将单元数转换为xy坐标
  41. x =k/n;
  42. y =k%n;
  43. //如果本单元格可以放置碉堡
  44. if(map[x][y]=='.'&&CanPut(map,x,y)){
  45. map[x][y]='O';//放置下一个碉堡
  46. //令count加一,并递归到下一个单元格
  47. dp(map,k+1,count+1);
  48. //递归完毕,恢复该单元格
  49. map[x][y]='.';
  50. }
  51. //本单元格不能放置碉堡
  52. dp(map,k+1,count);
  53. }
  54. }
  55. /**
  56. * 判断行x和列y处能不能配置碉堡
  57. * @param map
  58. * @param x
  59. * @param y
  60. * @return
  61. */
  62. private static boolean CanPut(char[][] map,int x, int y) {
  63. for(int i=x-1;i>=0;i--){//判断y列上的合法性
  64. if(map[i][y]=='O') return false;
  65. if(map[i][y]=='X') break;
  66. }
  67. for(int i=y-1;i>=0;i--){//判断x行上的合法性
  68. if(map[x][i]=='O') return false;
  69. if(map[x][i]=='X') break;
  70. }
  71. return true;
  72. }
  73. }

ZOJ(ZJU) 1002 Fire Net(深搜)的更多相关文章

  1. [ZJU 1002] Fire Net

    ZOJ Problem Set - 1002 Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we ha ...

  2. 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...

  3. 深搜(DFS),回溯,Fire Net

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 解题报告: 这里的深搜有一点不同,就是,在深搜每一个点时,都要深搜每 ...

  4. 深搜———ZOJ 1004:anagrams by stack

    细节问题各种虐!! 其实就是简单的一个深搜 看成二叉树来理解:每个节点有两个枝:入栈和出栈. 剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈 dfs写三个参数:depth搜索深 ...

  5. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  6. zoj 3820 Building Fire Stations(二分法+bfs)

    题目链接:zoj 3820 Building Fire Stations 题目大意:给定一棵树.选取两个建立加油站,问说全部点距离加油站距离的最大值的最小值是多少,而且随意输出一种建立加油站的方式. ...

  7. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  8. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  9. 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...

随机推荐

  1. 多线程下的performSelector和NSThread的使用

    多线程下的performSelector和NSThread的使用 NSThread的多线程使用: 我们可以使用这两种方法来使用线程中的问题 - (id)initWithTarget:(id)targe ...

  2. [转] 多线程下变量-gcc原子操作 __sync_fetch_and_add等

    http://blog.sina.com.cn/s/blog_6f5b220601013zw3.html 非常好的原子操作,不用加锁:__sync_fetch_and_add GCC 提供的原子操作 ...

  3. C#自定义事件:属性改变引发事件示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  4. C#操作INI配置文件示例

    源文件地址:http://pan.baidu.com/share/link?shareid=2536126078&uk=1761850335创建如图所示的控件: 源代码: using Syst ...

  5. Python分类统计数据

    在数据的常见分布中,有一种是一对多存储的数据,即一个是key,其他改key对应的多个value.例如气象数据等,每天有很多组,又或者是一个球员,他每天得多少分等等.我做这个东西有三种方法,即:常规编程 ...

  6. Codeforces 245G Suggested Friends - 交集set_intersection()

    一些人互相是朋友(无向图连一条无向边),现在要添加一个推荐朋友的功能,如果对于x,有y,x与y不是朋友,但是y和x有共同的朋友,并且共同的朋友最多,那么y可以推荐给x.问对于每一个人,有多少人值得推荐 ...

  7. Day11 - Mysql and ORM

    python 之路,Day11 - python mysql and ORM   本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 ...

  8. ie浏览器下input和select的上下居中问题!!!!

    在Google浏览器下的input和select标签里面的文字是根据它的高度自适应上下居中的,而ie浏览器下的input和select里面的文字就不会根据高度自适应上下居中,跟大家分享一下我的解决方法 ...

  9. Linq101-Partitioning

    using System; using System.Linq; namespace Linq101 { class Partitioning { /// <summary> /// Th ...

  10. 关于异常的疑难解答:System.Runtime.InteropServices.COMException

    COMException exception is thrown when an unrecognized HRESULT is returned from a COM method call.&qu ...