Fire Net

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

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
 题解:
二维数组的坐标用一个数字表示,这个点从0到n*n每次选或不选,加一个判断;
代码:

 #include<stdio.h>
#include<string.h>
#define MAX(x,y) x>y?x:y
const int INF=-0xfffffff;
char map[][];
int n,maxnum;
bool judge(int t){
int w,h,p,q;
h=t/n;w=t%n;
p=h;q=w;
while(h>=){
if(map[h][w]=='@')return false;
if(map[h][w]=='X')break;
h--;
}
w=q;h=p;
while(w>=){
if(map[h][w]=='@')return false;
if(map[h][w]=='X')break;
w--;
}
w=q;h=p;
while(w<n){
if(map[h][w]=='@')return false;
if(map[h][w]=='X')break;
w++;
}
w=q;h=p;
while(h<n){
if(map[h][w]=='@')return false;
if(map[h][w]=='X')break;
h++;
}
return true;
}
void dfs(int s,int num){int x,y;
if(s==n*n){
maxnum=MAX(maxnum,num);
return;
}
else{x=s/n;y=s%n;
if(map[x][y]=='.'&&judge(s)){
map[x][y]='@';
dfs(s+,num+);
map[x][y]='.';
}
dfs(s+,num);
}
}
int main(){
while(scanf("%d",&n),n){
maxnum=INF;
for(int i=;i<n;i++)scanf("%s",map[i]);
dfs(,);
printf("%d\n",maxnum);
}
return ;
}

Fire Net(dfs)的更多相关文章

  1. HDU1045 Fire Net(DFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)  ...

  2. ZOJ 1002 Fire Net(dfs)

    嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...

  3. hdu 1045 Fire Net(dfs)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  5. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  6. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  7. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  8. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  9. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

随机推荐

  1. [转]Activemq管理和基本介绍

    1.ActiveMQ服务器工作模型       通过ActiveMQ消息服务交换消息.消息生产者将消息发送至消息服务,消息消费者则从消息服务接收这些消息.这些消息传送操作是使用一组实现 ActiveM ...

  2. laravel5.3 笔记一

    laravel5.3 笔记 安装环境 laravel环境,laravel中文学习论坛上面有相关的教程 创建应用 laravel new blog 其中blog就是你的应用的名字 数据迁移 php ar ...

  3. 企业OA面临的问题,以及解决问题的推荐

          现在的企业不管大小都趋于软件话,而办公用的OA软件更是成为了企业中不可获取的一环,一个好的软件能让企业发展的更加顺利,而一个不合适的软件可能让公司哀声怨道反而起了反作用!       OA ...

  4. C#集合-队列

    本文来自:http://www.cnblogs.com/yangyancheng/archive/2011/04/28/2031615.html 队列是其元素以先进先出(FIFO)的方式来处理的集合. ...

  5. 第一个Spring MVC程序

    最近公司项目要开始使用Spring MVC替代Struts2了,就学习了一下Spring MVC的使用.这是第一个Spring mvc程序,分别使用xml和注解两种方式. 一.使用xml格式进行构建 ...

  6. 9. iptables 配置

    iptables 配置文件存放位置:  [root@Demon yum.repos.d]# vim /etc/rc.d/init.d/iptables   一.只给 Centos 6.5 打开 22 ...

  7. openssl 安装

    六.运行“nmake -f ms\ntdll.mak install”安装编译后的OpenSSL到指定目录. 七.查看安装结果C:\usr\local\ssl或C:\openssl-0.9.8.e下包 ...

  8. [AngularJS系列(4)] 那伤不起的provider们啊~ (Provider, Value, Constant, Service, Factory, Decorator)(转)

    用AngularJS做项目,但凡用过什么service啊,factory啊,provider啊,开始的时候晕没晕?!晕没晕?!感觉干的事儿都差不多啊,到底用哪个啊?!别告诉我你们几个就是为了跟我炫耀兄 ...

  9. 论i++与++i

    网上看到好多人问i++与++i到底怎么理解,网友给出的答案几乎都是一样的.如下: i++:先进行计算,然后i自增1 ++i:i自增1,然后进行计算 并且课本上给出的解释跟这个也差不多,不过这样记起来既 ...

  10. avalon前端js直接通过ajax请求传一个对象到后台

    代码如下:                //企业开票信息      vm.invoiceInfo = {       companyId : "",            //企 ...