http://acm.hdu.edu.cn/status.php?user=MekakuCityActors&pid=1045&status=5

Fire Net

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

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
 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include<iostream>
using namespace std;
char Map[][];
int res;
int n;
/**
判断是否成立
*/
bool judge(int x,int y){
if(Map[x][y]!='.')return false; //X.S都不可以放
for(int i=x-;i>=;i--){//判断点的上方
if(Map[i][y]=='X')break;
if(Map[i][y]=='O')return false;
}
for(int i=y-;i>=;i--){//判断点的左方
if(Map[x][i]=='X')break;
if(Map[x][i]=='O')return false;
}
return true;
}
/**
DFS
*/
void DFS(int x,int y,int tot){
if(x==n&&y==){ //已经搜索了n行,求最大值
res=max(res,tot);
return;
}
if(y==n){ //这一行已经求完,从下一行第一列重新开始
DFS(x+,,tot);
return;
}
for(int i=y;i<n;i++){
if(judge(x,i)){
Map[x][i]='O';
DFS(x,i+,tot+);
Map[x][i]='.';
}
}
DFS(x+,,tot);//上一行搜索结束,从下一行重新开始
}
int main(){
while(scanf("%d",&n),n){
res=;
for(int i=;i<n;i++)
scanf("%s",Map[i]);
DFS(,,);
printf("%d\n",res);
}
return ;
}

【HDOJ1045】【DFS】的更多相关文章

  1. Codeforces 383C . Propagating tree【树阵,dfs】

    标题效果: 有一棵树,有两种操作模式对本树:1:表示为(1 x val),在NOx加在节点上val,然后x每个节点加上儿子- val.给每个儿子一个儿子在一起-(- val),加到没有儿子为止.2:表 ...

  2. [USACO08DEC]在农场万圣节Trick or Treat on the Farm【Tarja缩点+dfs】

    题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...

  3. HDU 5409 CRB and Graph 【点双连通+DFS】

    <题目链接> 题目大意: 给你一个连通的无向图,问你删除每一条边后,是否能够出现一对(u,v),使得u,v不连通,且u<v,如果有多对u,v,则输出尽量大的u,和尽量小的v. 解题分 ...

  4. ZOJ - 3761 Easy billiards 【并查集+DFS】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3761 题意 在一个桌面上,给出一些球 如果在A球的某个方向的前方 ...

  5. bzoj 1770: [Usaco2009 Nov]lights 燈【高斯消元+dfs】

    参考:https://blog.csdn.net/qq_34564984/article/details/53843777 可能背了假的板子-- 对于每个灯建立方程:与它相邻的灯的开关次数的异或和为1 ...

  6. 【I'm Telling the Truth】【HDU - 3729】 【匈牙利算法,DFS】

    思路 题意:该题主要说几个同学分别说出自己的名次所处区间,最后输出可能存在的未说谎的人数及对应的学生编号,而且要求字典序最大. 思路:刚刚接触匈牙利算法,了解的还不太清楚,附一个专门讲解匈牙利算法的博 ...

  7. 比特镇步行(Walk)【虚点+bfs+dfs】

    Online Judge:NOIP2016十连测第一场 T3 Label:虚点,bfs,dfs 题目描述 说明/提示 对于100%数据,\(n<=200000\),\(m<=300000\ ...

  8. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)B 杨老师的游戏【暴力/next-permutation函数/dfs】

    链接:https://www.nowcoder.com/acm/contest/116/B 来源:牛客网 题目描述 杨老师给同学们玩个游戏,要求使用乘法和减法来表示一个数,他给大家9张卡片,然后报出一 ...

  9. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  10. 【DFS】NYOJ-82 迷宫寻宝(一)-条件迷宫问题

    [题目链接:NYOJ-82] #include<iostream> #include<cstring> using namespace std; struct node{ in ...

随机推荐

  1. css3 min-content,max-content,fit-content, fill属性

    css3里有四个属性,用来实现以内容为主的尺寸计算方式,intrinsic sizing min-content max-content fit-content fill 其中 fill 关键字,需要 ...

  2. 一款c语言实现的赛车游戏

    博主学习c语言已经有一段时间了,出于对自己学习检验的目的,自制了一款c语言赛车游戏. 由于本质是检验和尝试,所以并没有注重游戏的界面.下文是开发文档,在博主的github网页可以下载源码,注意本项目使 ...

  3. 利用FFMPEG命令进行文件分割

    ffmpeg -ss 00:00:00 -i input.mp4 -c copy -t 60 output.mp4 -ss 表示视频分割的起始时间,-t 表示分割时长,同时也可以用 00:01:00表 ...

  4. 使用Jenkins自动编译 .net 项目

    使用Jenkins自动编译我的.net 项目   1.Jenkins是什么? Jenkins是一个可扩展的持续集成的引擎,主要用于持续自动的构建.测试软件项目 监控一些定时执行的任务.   2.安装配 ...

  5. Traumland--梦乡--IPA--德语

    德国电影<<英俊少年>>的插曲.

  6. :工厂模式1:方法模式--Pizza

    #ifndef __PIZZA_H__ #define __PIZZA_H__ class Pizza { public: Pizza(){} virtual ~Pizza(){} virtual c ...

  7. MicroOrm.Dapper.Repositories 的使用

    https://github.com/geffzhang/MicroOrm.Dapper.Repositories 1.特性标记都是要引用: System.ComponentModel.DataAnn ...

  8. [Spring] Spirng中的AOP进行事务的传播属性和事务隔离级别

    通知注解 前置通知(@Before):在某连接点(join point)之前执行的通知,但这个通知不能阻止连接点前的执行(除非它抛出一个异常) 返回后通知(@AfterReturning):在某连接点 ...

  9. 基于centos的freeradius高可用lvs(UDP)

    最近在做freeradius的高可用配置,使用lvs的vip做轮询: freeradius的配置见前面的文章: 下面是lvs的keepalived的配置: global_defs { router_i ...

  10. VC下CString类型与int 、float等数据类型的相互转换

    一.常用转换 1. CString --> int转换 CString str("1234");    int i= _ttoi(str); 2. CString --> ...