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 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:

  1. 4
  2. .X..
  3. ....
  4. XX..
  5. ....
  6. 2
  7. XX
  8. .X
  9. 3
  10. .X.
  11. X.X
  12. .X.
  13. 3
  14. ...
  15. .XX
  16. .XX
  17. 4
  18. ....
  19. ....
  20. ....
  21. ....
  22. 0

Sample output:

  1. 5
  2. 1
  3. 5
  4. 2
  5. 4

题意

大小为n*n的城市建造碉堡,要求碉堡建在‘.’位置,每两个碉堡不能在一行或一列,或者在一行一列的时候中间有‘X’隔开,问在这个城市中最多能建多少碉堡

思路

从左上角往右下角进行dfs,用一个check函数来判断当前位置是否可以建造碉堡,如果可以的话,将该位置做特殊标记。回溯寻找最大值

AC代码

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <limits.h>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <vector>
  11. #include <set>
  12. #include <string>
  13. #define ll long long
  14. #define ull unsigned long long
  15. #define ms(a) memset(a,0,sizeof(a))
  16. #define pi acos(-1.0)
  17. #define INF 0x7f7f7f7f
  18. #define lson o<<1
  19. #define rson o<<1|1
  20. const double E=exp(1);
  21. const int maxn=1e3+10;
  22. const int mod=1e9+7;
  23. using namespace std;
  24. char ch[maxn][maxn];
  25. int n;
  26. int ans;
  27. bool check(int x,int y)
  28. {
  29. for(int i=x-1;i>=0;i--)
  30. {
  31. if(ch[i][y]=='%')
  32. return false;
  33. if(ch[i][y]=='X')
  34. break;
  35. }
  36. for(int i=y-1;i>=0;i--)
  37. {
  38. if(ch[x][i]=='%')
  39. return false;
  40. if(ch[x][i]=='X')
  41. break;
  42. }
  43. return true;
  44. }
  45. void dfs(int s,int sum)
  46. {
  47. if(s==n*n)
  48. {
  49. ans=max(ans,sum);
  50. return ;
  51. }
  52. int x=s/n;
  53. int y=s%n;
  54. if(ch[x][y]=='.'&&check(x,y))
  55. {
  56. ch[x][y]='%';
  57. dfs(s+1,sum+1);
  58. ch[x][y]='.';
  59. }
  60. dfs(s+1,sum);
  61. }
  62. int main(int argc, char const *argv[])
  63. {
  64. ios::sync_with_stdio(false);
  65. while(cin>>n&&n)
  66. {
  67. ans=0;
  68. for(int i=0;i<n;i++)
  69. cin>>ch[i];
  70. dfs(0,0);
  71. cout<<ans<<endl;
  72. }
  73. return 0;
  74. }

ZOJ 1002:Fire Net(DFS+回溯)的更多相关文章

  1. ZOJ 1002 Fire Net(dfs)

    嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...

  2. zoj 1002 Fire Net 碉堡的最大数量【DFS】

    题目链接 题目大意: 假设我们有一个正方形的城市,并且街道是直的.城市的地图是n行n列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...

  3. zoj 1002 Fire Net (二分匹配)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  4. [ZOJ 1002] Fire Net (简单地图搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋 ...

  5. ZOJ 1002 Fire Net

    题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡.子弹不能穿透墙壁.问最多可以放置几个碉堡,保证它们不会相互误伤. 解法:从左上的顶点开始遍历,如果这个点不是墙,做深度 ...

  6. DFS ZOJ 1002/HDOJ 1045 Fire Net

    题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...

  7. [ZJU 1002] Fire Net

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

  8. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  9. NOJ 1074 Hey Judge(DFS回溯)

    Problem 1074: Hey Judge Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  10. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. matlab global persistent变量

    global变量是全局的,在使用global变量的函数里需要用global声明所使用的变量. persistent类似global,不过仅对当前函数有作用,这样避免了外面的影响.当这个函数被clear ...

  2. bzoj1096

    题解: 斜率优化dp 代码: #include<bits/stdc++.h> typedef long long ll; ; using namespace std; int n,l,r, ...

  3. Python3.7.0的安装方法

    安装Python3.7.0 1.下载Python-3.7.0.tar.xz包安装 wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.t ...

  4. Centos7部署open-falcon 0.2

    参考: https://www.cnblogs.com/straycats/p/7199209.html http://book.open-falcon.org/zh_0_2/quick_instal ...

  5. Class file collision

    ecplise报错,提示:Class file collision (类文件冲突) 原因是:文件保存(编译)后,生成了class文件起了冲突,windows 系统认为Test.class 和test. ...

  6. mybatis学习(一)----入门

    一.Mybatis介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...

  7. 创建自己的docker基础镜像

    1.下载镜像 centos7 docker pull centos: 2.创建容器加载镜像 docker run -i -t --name centos7 centos: docker run 参数详 ...

  8. el-container 实践上的布局问题

    当自己利用element-ui上面的例子来实现整体布局的时候, 就是自己分开成单独的vue组件时,发现布局是不对的,效果是这样的: 代码是这样的,代码一模一样,只是拆开了各个组件,如下图: 后来发现是 ...

  9. webpack进阶构建项目(一):1.理解webpack加载器

    1.理解webpack加载器 webpack的设计理念,所有资源都是“模块”,webpack内部实现了一套资源加载机制,这与Requirejs.Sea.js.Browserify等实现有所不同. We ...

  10. <YaRN><Official doc><RM REST API's>

    Overview ... YARN Architecture The fundamental idea of YARN is to split up the functionalities of re ...