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. thinkphp5.0调用ajax无刷新加载数据

    控制器层那边就是调数据返回,这里不再赘述,视图层页面ajax部分写法如下 function shanchu(obj) { var code = $(obj).attr("code" ...

  2. 认识Jmeter操作界面

    使用工具:Jmeter(版本apache-jmeter-2.13) 安装前提:JDK的安装. 主要对GUI操作界面的讲解 (http://jmeter-plugins.org/downloads/al ...

  3. python2.7 安装Django

    目前Django最新版是2.0,不支持Python2,在使用pip 安装的时候会报错,pip默认安装的是最新的稳定版本 使用pip指定安装的版本:pip install django==1.11.4 ...

  4. Spring使用java代码配置Web.xml进行访问service

    方式一:继承WebMvcConfigurerAdapter类 1.使用一个类来继承 package com.wbg.springJavaConfig.spring; import org.spring ...

  5. 【数据结构】浅谈倍增求LCA

    思路 运用树上倍增法可以高效率地求出两点x,y的公共祖先LCA 我们设f[x][k]表示x的2k辈祖先 f[x][0]为x的父节点 因为从x向根节点走2k 可以看成从x走2k-1步 再走2k-1步 所 ...

  6. iOS中的应用启动原理

    iOS中的应用启动原理 来源: http://m.blog.csdn.net/article/details?id=50530090 http://m.warting.com/program/2016 ...

  7. Swift_闭包

    Swift_闭包 点击查看源码 闭包优化 //闭包优化 func testClosures() { //函数做参数 排序 let names = ["XuBaoAiChiYu", ...

  8. 简单几行代码使用百度地图API接口分页获取信息

    首发于: 万能助手扩展开发:使用百度地图API接口分页获取信息_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=426 使用 ...

  9. thinkphp5访问sql2000数据库

    大家都知道php跟mysql是绝配,但是因为有时候工作需要,要求php访问操作sql2000,怎么办呢? 一般来说有两种方式: 1. sqlsrv驱动方式 2. odbc方式 sqlsrv驱动方式,因 ...

  10. 集合之HashMap、Hashtable

    HashMap 基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtabl ...