Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15657    Accepted Submission(s): 9477

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

题意:在有墙的矩阵里面放置炮台,炮台之间可以行之间和列之间相互攻击,不能让他们相互攻击,墙可以挡住炮弹。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)的更多相关文章

  1. 洛谷 P1019 单词接龙【经典DFS,温习搜索】

    P1019 单词接龙 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在 ...

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

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

  3. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

  5. POJ 1321 - 棋盘问题 - [经典DFS]

    题目链接:http://poj.org/problem?id=1321 Time Limit: 1000MS Memory Limit: 10000K Description 在一个给定形状的棋盘(形 ...

  6. 经典DFS问题实践

    八皇后问题: //八皇后问题 经典的DFS问题实践 #include<iostream> #include<cmath> #include<algorithm> # ...

  7. 蓝桥杯之剪格子(经典dfs)

    如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |10* 1|52| +--****--+ |20|30* 1| *******--+ | 1| 2| 3| +--+--+-- ...

  8. LeetCode51 N皇后——经典dfs+回溯(三段式解法)

    代码如下: class Solution { public: // record[row] 该行对应的列 vector<vector<string> > ans; // 结果集 ...

  9. HDU 1241 Oil Deposits(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 很经典的一道dfs,但是注意每次查到一个@之后,都要把它变成“ * ”,然后继续dfs ...

随机推荐

  1. centos7 firewall指定IP与端口访问(常用)

    1.启动防火墙 systemctl start firewalld.service 2.指定IP与端口 firewall-cmd --permanent --add-rich-rule="r ...

  2. Linux磁盘分区,挂载

    分区基础知识 分区的方式:   1) mbr分区:     1.最多支持四个主分区     2.系统只能安装在主分区     3.扩展分区要占一个主分区     4.MBR最大只支持2TB,但拥有最好 ...

  3. Gradle Goodness: Adding Tasks to a Predefined Group

    In Gradle we can group related tasks using the group property of a task. We provide the name of our ...

  4. C# 类中的静态字段始终继承自基类

    我们试想一下现在有一个类Parent,它有一个static的int类型字段number,然后如果类Parent有三个子类Child01.Child02和Child03,那么改变Parent.numbe ...

  5. javascript入门教程 (2)

    这篇我就不铺垫和废话了,我们开始正式进入JS核心语法的学习… 首先我们从基础入手... 一. 基础语法 1.1 区分大小写 JS语法规定变量名是区分大小写的 比如: 变量名 learninpro 和变 ...

  6. 我的前端工具集(七)div背景网格

    我的前端工具集(七)div背景网格   liuyuhang原创,未经允许禁止转载 目录 我的前端工具集 有时候总觉得div颜色过于白,于是给了10%的灰 但是并不一定能解决问题,因为页面中会有不均衡的 ...

  7. 关于MySQL自增主键的几点问题(上)

    前段时间遇到一个InnoDB表自增锁导致的问题,最近刚好有一个同行网友也问到自增锁的疑问,所以抽空系统的总结一下,这两个问题下篇会有阐述. 1. 划分三种插入类型 这里区分一下几种插入数据行的类型,便 ...

  8. URL参数获取/转码

    JS中对URL进行转码与解码 1.escape 和 unescape escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值. 采用unicode字符集对指定的字符串 ...

  9. iOS 11.2 - 11.3.1 越狱教程

    iOS 11.2 - 11.3.1 越狱教程 一.准备相应的工具 (1) 下载 CydiaImpactor,官网地址是 http://cydiaImpactor.com (2) 下载 Electra, ...

  10. Vue性能优化之组件按需加载(以及一些常见的性能优化方法)

    关于Vue中的按需加载我就简单介绍一下:大概就是我们所有的东西都会在app.js里面,但是我们并不需要把所有的组件都一次性加载进来,我们可以在需要它的时候再将它加载进来,话不多说,开车! 1.webp ...