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. Softmax回归(Softmax Regression, K分类问题)

    Softmax回归:K分类问题, 2分类的logistic回归的推广.其概率表示为: 对于一般训练集:                     系统参数为:      Softmax回归与Logist ...

  2. 搭建 github.io 博客站点

    前言 很多人都有搭建博客或知识库站点的想法,可自己买云服务器太不划算,部署管理也是个问题:基于免费又热门的 GitHub Pages 来搭建博客站点倒是省钱省力省事的好办法,于是上网一搜,满屏都是关于 ...

  3. 画布与SVG区别

  4. html+css3 实现各种loading效果

    效果见下图 代码: 建议直接去本人github上浏览代码 https://github.com/wuliqiangqiang/loading <!DOCTYPE html> <htm ...

  5. 关于mybatis编写sql问题.

    最近呢楼主回到长沙进行面试:被问了一个这样的问题,在mybatis中怎么进行模糊查询,望各位大佬在下方进行评论,好让我这菜鸡多学习一些.

  6. C# 设计模式之 单例模式

    单例模式三种写法: 第一种最简单,但没有考虑线程安全,在多线程时可能会出问题,不过俺从没看过出错的现象,表鄙视我…… public class Singleton{    private static ...

  7. Java线程状态图

    嘤,先盗图一张,后面再补充描述!

  8. linux线程篇 (三) 线程的同步

    1 互斥量 pthreat_mutex_t mymutex; //1. 创建 初始化 int pthread_mutex_init(pthread_mutex_t *mutex, const pthr ...

  9. Java语法糖 : try-with-resources

    先了解几个背景知识 什么是语法糖 语法糖是在语言中增加的某种语法,在不影响功能的情况下为程序员提供更方便的使用方式. 什么是资源 使用之后需要释放或者回收的都可以称为资源,比如JDBC的connect ...

  10. 前端页面加载速度优化---Ngnix之GZIP压缩

    gzip on; #开启Gzip gzip_static on;#是否开启gzip静态资源 #nginx对于静态文件的处理模块,该模块可以读取预先压缩的gz文件,这样可以减少每次请求进行gzip压缩的 ...