hdu1045Fire Net(经典dfs)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15657 Accepted Submission(s): 9477
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.
题意:在有墙的矩阵里面放置炮台,炮台之间可以行之间和列之间相互攻击,不能让他们相互攻击,墙可以挡住炮弹。X表示墙,.表示空位置,问最多可以放多少炮台
题解:dfs搜索。边搜索边计数,如果遇到不能放的情况就回溯。从左上角开始放,放到右下角的时候退出dfs。判断该位置能不能放:找该位置前面和上面(因为是从左上角开始放的,所以只要看前面和上面)有没有炮台,如果有炮台又没有墙挡住那就不能放,其他情况都可以放(只要该位置为空)。
有个n*n二维数组的技巧:如果该位置是第k个元素,那么该位置的行坐标为k/n,列坐标为k%n!!!!
#include<bits/stdc++.h>
using namespace std;
int n;
char s[][];int ans;
int check(int x,int y) {
for(int i=x-; i>=; i--) { //同一列往上找
if(s[i][y]=='')//遇到有碉堡肯定不能放
return ;
if(s[i][y]=='X')
return ;
}
for(int i=y-; i>=; i--) { //同一行往左边找
if(s[x][i]=='')//遇到有碉堡肯定不能放
return ;
if(s[x][i]=='X')
return ;
}
return ;
}
void dfs(int k,int num) {
int x,y;
if(k==n*n)
{
ans=max(ans,num);return ;
}
else
{
x=k/n;//重要的技巧!!!
y=k%n;
if(s[x][y]=='.'&&check(x,y))
{
s[x][y]='';
dfs(k+,num+);//继续寻找并计数
s[x][y]='.';//回溯
}
dfs(k+,num);//放不了,继续寻找,数量保持不变
}
}
int main() {
while(~scanf("%d",&n),n) {
memset(s,'\0',sizeof(s));
ans=;
for(int i=; i<n; i++) {
getchar();//用来输入回车,不然他会把回车存到s数组里面
for(int j=; j<n; j++) {
scanf("%c",&s[i][j]);
}
}
dfs(,);//(0,0)开始走
printf("%d\n",ans);
}
return ;
}
hdu1045Fire Net(经典dfs)的更多相关文章
- 洛谷 P1019 单词接龙【经典DFS,温习搜索】
P1019 单词接龙 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在 ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ 1321 - 棋盘问题 - [经典DFS]
题目链接:http://poj.org/problem?id=1321 Time Limit: 1000MS Memory Limit: 10000K Description 在一个给定形状的棋盘(形 ...
- 经典DFS问题实践
八皇后问题: //八皇后问题 经典的DFS问题实践 #include<iostream> #include<cmath> #include<algorithm> # ...
- 蓝桥杯之剪格子(经典dfs)
如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |10* 1|52| +--****--+ |20|30* 1| *******--+ | 1| 2| 3| +--+--+-- ...
- LeetCode51 N皇后——经典dfs+回溯(三段式解法)
代码如下: class Solution { public: // record[row] 该行对应的列 vector<vector<string> > ans; // 结果集 ...
- HDU 1241 Oil Deposits(经典DFS)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 很经典的一道dfs,但是注意每次查到一个@之后,都要把它变成“ * ”,然后继续dfs ...
随机推荐
- 利用MSF批量打RMI漏洞
声明:不会Java. 参考:https://www.secpulse.com/archives/6203.html 下载mjet,https://github.com/mogwaisec/mjet 按 ...
- [19/04/01-星期一] IO技术_字节流分类总结(含字节数组(Array)流、字节数据(Data)流、字节对象(Object)流)
一.字节流分类概括 -->1.ByteArrayInputStream /ByteArrayOutputStream(数组字节输入输出) InputStream/OutputStr ...
- 关于 Vue.js+Element-UI 日期控件 日期范围选择
理想效果 : 也就是说前面时间框的时间能选的范围应该小于等于后面的时间框; 后面时间框能选的范围应该大于等于前面的时间框; 示例代码 : 页面: <el-form-item label=&quo ...
- 配置git服务
https://www.cnblogs.com/sumuncle/p/6362697.html
- 自定义属性之LinearLayout ImageView TextView模拟图片文字按钮
一.资源文件: 1.文字选择器: <?xml version="1.0" encoding="utf-8"?> <selector xmlns ...
- 微信小程序开发——进阶篇
由于项目的原因,最近的工作一直围绕着微信小程序.现在也算告一段落,是时候整理一下这段时间的收获了.我维护的小程序有两个,分别是官方小程序和一个游戏为主的小程序.两个都是用了wepy进行开发,就是这个: ...
- ARM v8-A 系列CPU的MMU隐射分析
ARM v8-A 系列CPU的MMU隐射分析 摘要:V8-A系列CPU有ELX,X为0~3等4个异常级别,又有AArch64和AArch32两种执行状态,异常级别可以决定执行状态,而对应着执行状态,M ...
- 使用单例模式来打造ActivityManager类
单例(Singleton)模式 定义 单例模式是一种对象创建型模式,使用单例模式,可以保证为一个类只生成唯一的实例对象.也就是说,在整个程序空间中,该类只存在一个实例对象. GoF对单例模式的定义是: ...
- 利用MyFlash闪回丢失数据(续)
last night,i've tested flashback by MyFlash tool,but failed,now let's do some other test with ...
- 分析nginx 日志常用命令
一.概念 并发连接数 客户端向服务器发起请求,并建立了TCP连接.每秒钟服务器链接的总TCP数量,就是并发连接数.请求数 请求数指的是客户端在建立完连接后,向http服务发出GET/POS ...