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. 0-MAVEN SETTING

    localRepository : 表示本地库的保存位置,也就是maven主要的jar包保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径 Ser ...

  2. java 一些容易忽视的小点-数据类型和运算符篇

    注释 文档注释:   以"/**"开头以"*/"结尾,注释中包含一些说明性的文字及一些JavaDoc标签(后期写项目时,可以生成项目的API) 行注释:   以 ...

  3. Dom方法,解析XML文件

    Dom方法,解析XML文件的基本操作 package com.demo.xml.jaxp; import java.io.IOException; import javax.xml.parsers.D ...

  4. Linux使用定时器timerfd 和 eventfd接口实现进程线程通信

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  5. 图解前序遍历线索化二叉树,前序线索二叉树遍历,C\C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  6. <Scala><For beginners>

    Scala Overview Scala is object-oriented Scala is a pure object-oriented language in the sense that e ...

  7. L270 运动前要热身

    If I'm being really honest, the only time I actually remember to stretch before a workout is when I' ...

  8. Linux:进程

    进程 一.简略操作 1.查看进程:ps2.查看进程资源占用量:top3.查看进程关系(进程树):pstree4.查看当前系统信息:uname -a 二.详细操作 1.查看进程:ps 2.查看进程资源占 ...

  9. ob 函数的使用

    ob 函数的使用1. 页面静态化 $id = isset($_GET['id'])?$_GET['id']-0:0; $filename = "html/".date(" ...

  10. outlook2016中如何设置两个账户都自动有各自默认签名

    安装了Outlook2016以后,有些朋友不清楚,我们在发送邮件的时候,怎么添加邮件签名,其实在Outlook2016中添加邮件签名的方法也是比较简单的,这里小编介绍下在Outlook2016中设置添 ...