ACM--DFS--最大碉堡数--HDOJ 1045--Fire Net
HDOJ题目地址:传送门
Fire Net
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.
题意:问可以放多少个碉堡,碉堡不能在同一行或列,除非有墙隔开,就是两个碉堡在同一行或列的条件是必须有墙隔开
#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相关推荐
- hdoj 1045 Fire Net 直接枚举 模拟就好了
题目链接 直接枚举所有结果就可以了, 就是第一次模拟这样的数据 每个位置若没有阻挡物,存在着两种状态,一是有堡垒,二是空地.直接枚举就可以了. 自己的代码写的很繁琐,但是 首次想到了 将if(y> ...
- HDU-1045 Fire Net(最大碉堡数)
传送门 Problem Description Suppose that we have a square city with straight streets. A map of a city is ...
- 【NOIP模拟】T2 管道(状压dp求图的dfs序方案数)
f[i][j]: i表示整个图走没走过的状态 j表示当前到了第j个点 存的值就是在这种情形下 可以走到的地方的状态 dp[i][j]:i表示整个图走没走过的状态 j表示当前在j点 访问剩余能去到的点的 ...
- 路痴的单身小菡 BFS求最短路径+DFS求路径数
路痴的单身小菡 题目描述 小菡很聪明,所以他打ACM非常给力,经常偷偷学习到深夜. 他是如此的努力学习,以至于他根本就没有时间完整的逛过学校. 有一天,他听说科大湖的黑天鹅非常好看,由于没有女朋友,他 ...
- 【启发式合并】【dfs】树数树(nowcoder 20107-C)
正题 nowcoder 20107-C 题目大意 给一棵树,让你构造一个序列a,使得a中的数互不相等,且相邻点是祖先的关系,回答序列最长长度 解题思路 书中的每个点可以把字数的两个序列连起来 考虑维护 ...
- hdu 杭电 1045 Fire Net
题意:地图中最多能放多少炮台. 解法:深搜. ac代码: View Code #include<iostream> using namespace std;char map[8][8]; ...
- ACM将一个十进制的数转换为一个十六进制的数输出
Description 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. Input 每行一个整数x,0<= x <= 2^31. Output 每行输出对应的 ...
- hdu 1045 Fire Net
经典建模. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ...
- acm c语言训练 数不同的单词数
题目: WUST 零起点学算法92--单词数 Description BobLee 最近忙着考研,话说某一天当他正在看一篇英语阅读时,突然想到想去统计下这篇文章不同单词的个数,由于BobLee很忙,所 ...
最新文章
- python序列类型-Python内置序列类型之集合类型详解
- 2017南京师范大学计算机学院录取名单,南京师范大学2017年硕士研究生复试成绩及录取名单的通知...
- python 笔记 :Gym库 (官方文档笔记)
- 神策数据与 IPIP.NET 强强联合,精准 IP 让用户行为分析更精确
- BlackArch Linux安装VMware Tools教程
- 8g ubuntu 树莓派4b_树莓派4B安装Ubuntu系统,并安装桌面
- jsp 跳到servlet路径_请问如何从jsp中跳到servlet中?
- John's trip(POJ1041+欧拉回路+打印路径)
- shared_ptr四宗罪
- b-tree的索引页总览
- 题目 1097: 蛇行矩阵
- java读写excel,解决poi包中没有org.apache.poi.ss.usermodel.CellType的问题
- [iOS]让你的应用支持新iPad的Retina显示屏
- 市面上主流RTC竞品对比分析
- 【人工智能Prolog】mother、father和grandpa
- Payment:支付宝即时到账接口接入教程
- 《Metasploit魔鬼训练营》环境搭建与前两章经历体会
- 前台、后台、前端、后端的区别
- 阿里云python自测答案_阿里云技能测试python初级中级高级
- 【EXCEL技巧】制作一个信息查询表(仅可查看自己)