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. Search - Dictionary

    Search III Your task is to write a program of a simple dictionary which implements the following ins ...

  2. linux-资料汇集

    1.http://www.debian.org/doc/ 2.鸟哥的私房菜 3.The Linux Command Line by William E. Shotts, Jr. 4.https://d ...

  3. linux nginx 配置php

    linux nginx 配置php   下载php源码 解压 configure ./configure --prefix=/usr/local/php --enable-fpm --with-mcr ...

  4. iOS应用软件沙盒sandbox相关知识(整理)

    1.iOS沙盒机制原理 iOS应用程序只能在该程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...

  5. 三、Hadoop 的 API

    1.环境搭建 <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop- ...

  6. 定位权限授权 - iOS

    关于介入地图相关功能后会遇到类似定位的子功能,由此引来了此定位权限授权相关. 首先,需要导入 CoreLocation 的框架并创建管理对象从而实现后续的相关操作; #import <CoreL ...

  7. 用IntelliJ IDEA 配置Maven并部署Maven工程到Tomcat(Windows中)

    近几天做一个新项目才接触Intellij IDEA 1.在官网下载了maven 解压并新建一个本地仓库文件夹 2.配置本地仓库路径 3.配置maven环境变量 4.在IntelliJ IDEA中配置m ...

  8. [SHELL]软件管理

  9. jQuery实现 自动滚屏操作

    实现自动滚屏思路: 1.滚屏即:文本的往上移动一段距离: 2.那么我们使文本每过一段时间就往上移动一段固定距离,就可实现滚屏: 3.直到文本底部出现在浏览器窗口中,专业点就是 文本移动的距离 + 浏览 ...

  10. 常用的JavaScript设计模式(一)Constructor(构造器)模式

    在es6中,新增了一个语法糖--class,可以说是为JavaScript引入了类的概念.而在传统的JavaScript中,则是通过构造器生成实例对象的. JavaScript支持特殊的constru ...