HDOJ题目地址:传送门

Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9556    Accepted Submission(s): 5567
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

题意:问可以放多少个碉堡,碉堡不能在同一行或列,除非有墙隔开,就是两个碉堡在同一行或列的条件是必须有墙隔开

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char Map[5][5];
int res;
int n;
/**判断是否成立
*/
bool judge(int x,int y){if(Map[x][y]!='.')return false; //X.S都不可以放for(int i=x-1;i>=0;i--){//判断点的上方if(Map[i][y]=='X')break;if(Map[i][y]=='O')return false;}for(int i=y-1;i>=0;i--){//判断点的左方if(Map[x][i]=='X')break;if(Map[x][i]=='O')return false;}return true;
}
/**DFS
*/
void DFS(int x,int y,int tot){if(x==n&&y==0){ //已经搜索了n行,求最大值res=max(res,tot);return;}if(y==n){ //这一行已经求完,从下一行第一列重新开始DFS(x+1,0,tot);return;}for(int i=y;i<n;i++){if(judge(x,i)){Map[x][i]='O';DFS(x,i+1,tot+1);Map[x][i]='.';}}DFS(x+1,0,tot);//上一行搜索结束,从下一行重新开始
}
int main(){while(scanf("%d",&n),n){res=0;for(int i=0;i<n;i++)scanf("%s",Map[i]);DFS(0,0,0);printf("%d\n",res);}return 0;}

ACM--DFS--最大碉堡数--HDOJ 1045--Fire Net相关推荐

  1. hdoj 1045 Fire Net 直接枚举 模拟就好了

    题目链接 直接枚举所有结果就可以了, 就是第一次模拟这样的数据 每个位置若没有阻挡物,存在着两种状态,一是有堡垒,二是空地.直接枚举就可以了. 自己的代码写的很繁琐,但是 首次想到了 将if(y> ...

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

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

  3. 【NOIP模拟】T2 管道(状压dp求图的dfs序方案数)

    f[i][j]: i表示整个图走没走过的状态 j表示当前到了第j个点 存的值就是在这种情形下 可以走到的地方的状态 dp[i][j]:i表示整个图走没走过的状态 j表示当前在j点 访问剩余能去到的点的 ...

  4. 路痴的单身小菡 BFS求最短路径+DFS求路径数

    路痴的单身小菡 题目描述 小菡很聪明,所以他打ACM非常给力,经常偷偷学习到深夜. 他是如此的努力学习,以至于他根本就没有时间完整的逛过学校. 有一天,他听说科大湖的黑天鹅非常好看,由于没有女朋友,他 ...

  5. 【启发式合并】【dfs】树数树(nowcoder 20107-C)

    正题 nowcoder 20107-C 题目大意 给一棵树,让你构造一个序列a,使得a中的数互不相等,且相邻点是祖先的关系,回答序列最长长度 解题思路 书中的每个点可以把字数的两个序列连起来 考虑维护 ...

  6. hdu 杭电 1045 Fire Net

    题意:地图中最多能放多少炮台. 解法:深搜. ac代码: View Code #include<iostream> using namespace std;char map[8][8]; ...

  7. ACM将一个十进制的数转换为一个十六进制的数输出

    Description 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. Input 每行一个整数x,0<= x <= 2^31. Output 每行输出对应的 ...

  8. hdu 1045 Fire Net

    经典建模. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ...

  9. acm c语言训练 数不同的单词数

    题目: WUST 零起点学算法92--单词数 Description BobLee 最近忙着考研,话说某一天当他正在看一篇英语阅读时,突然想到想去统计下这篇文章不同单词的个数,由于BobLee很忙,所 ...

最新文章

  1. python序列类型-Python内置序列类型之集合类型详解
  2. 2017南京师范大学计算机学院录取名单,南京师范大学2017年硕士研究生复试成绩及录取名单的通知...
  3. python 笔记 :Gym库 (官方文档笔记)
  4. 神策数据与 IPIP.NET 强强联合,精准 IP 让用户行为分析更精确
  5. BlackArch Linux安装VMware Tools教程
  6. 8g ubuntu 树莓派4b_树莓派4B安装Ubuntu系统,并安装桌面
  7. jsp 跳到servlet路径_请问如何从jsp中跳到servlet中?
  8. John's trip(POJ1041+欧拉回路+打印路径)
  9. shared_ptr四宗罪
  10. b-tree的索引页总览
  11. 题目 1097: 蛇行矩阵
  12. java读写excel,解决poi包中没有org.apache.poi.ss.usermodel.CellType的问题
  13. [iOS]让你的应用支持新iPad的Retina显示屏
  14. 市面上主流RTC竞品对比分析
  15. 【人工智能Prolog】mother、father和grandpa
  16. Payment:支付宝即时到账接口接入教程
  17. 《Metasploit魔鬼训练营》环境搭建与前两章经历体会
  18. 前台、后台、前端、后端的区别
  19. 阿里云python自测答案_阿里云技能测试python初级中级高级
  20. 【EXCEL技巧】制作一个信息查询表(仅可查看自己)

热门文章

  1. 爱壁纸hd电脑版|爱壁纸hd电脑版下载
  2. 网狐棋牌数据库配置问题
  3. Unity3D-VR《静夜诗》4-窗户门动画的播放
  4. AI内容生成时代:该如何和AI对话?
  5. 可能是最详细的字符编码详解
  6. bilibili网页版html5,Bilibili HTML5播放器网页全屏模式优化 脚本版
  7. Spring Cloud微服务开发笔记4——Ribbon框架使用方法
  8. 【手册】如何编译/修改三星手机Rom(二)
  9. linux借解压rar文件,在linux下解压rar文件
  10. 【甄选靶场】Vulnhub百个项目渗透——项目四十二:Moria1.1(MD5加盐爆破)