【HDOJ1045】【DFS】
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
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.
#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】的更多相关文章
- Codeforces 383C . Propagating tree【树阵,dfs】
标题效果: 有一棵树,有两种操作模式对本树:1:表示为(1 x val),在NOx加在节点上val,然后x每个节点加上儿子- val.给每个儿子一个儿子在一起-(- val),加到没有儿子为止.2:表 ...
- [USACO08DEC]在农场万圣节Trick or Treat on the Farm【Tarja缩点+dfs】
题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...
- HDU 5409 CRB and Graph 【点双连通+DFS】
<题目链接> 题目大意: 给你一个连通的无向图,问你删除每一条边后,是否能够出现一对(u,v),使得u,v不连通,且u<v,如果有多对u,v,则输出尽量大的u,和尽量小的v. 解题分 ...
- ZOJ - 3761 Easy billiards 【并查集+DFS】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3761 题意 在一个桌面上,给出一些球 如果在A球的某个方向的前方 ...
- bzoj 1770: [Usaco2009 Nov]lights 燈【高斯消元+dfs】
参考:https://blog.csdn.net/qq_34564984/article/details/53843777 可能背了假的板子-- 对于每个灯建立方程:与它相邻的灯的开关次数的异或和为1 ...
- 【I'm Telling the Truth】【HDU - 3729】 【匈牙利算法,DFS】
思路 题意:该题主要说几个同学分别说出自己的名次所处区间,最后输出可能存在的未说谎的人数及对应的学生编号,而且要求字典序最大. 思路:刚刚接触匈牙利算法,了解的还不太清楚,附一个专门讲解匈牙利算法的博 ...
- 比特镇步行(Walk)【虚点+bfs+dfs】
Online Judge:NOIP2016十连测第一场 T3 Label:虚点,bfs,dfs 题目描述 说明/提示 对于100%数据,\(n<=200000\),\(m<=300000\ ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)B 杨老师的游戏【暴力/next-permutation函数/dfs】
链接:https://www.nowcoder.com/acm/contest/116/B 来源:牛客网 题目描述 杨老师给同学们玩个游戏,要求使用乘法和减法来表示一个数,他给大家9张卡片,然后报出一 ...
- 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】
目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...
- 【DFS】NYOJ-82 迷宫寻宝(一)-条件迷宫问题
[题目链接:NYOJ-82] #include<iostream> #include<cstring> using namespace std; struct node{ in ...
随机推荐
- centos6.5 安装php-5.6.31
1 从PHP官网下载所需要的PHP版本 下载地址: http://php.net/get/php-5.6.31.tar.gz/from/a/mirror 把下载好的文件上传到服务器 2 安装PHP ...
- Java 统计字母个数
原理: 将字符串转换成char字符数组 然后使用另一个数组存储 代码如下 public class CalChar { public static void main(String[] args) { ...
- 在项目中使用 SCSS
背景概述 1. CSS预处理器 css预处理器定义了一种新的编程语言,编译后成正常的CSS文件.为CSS增加一些编程的特性,无需考虑浏览器的兼容问题,让CSS更加简洁,适应性更强,可读性更佳,更易于代 ...
- EF-一对一关系
针对关系型数据库来说,需要明了每个对象之间的关系. 它们之间的关系有: 1.一对一(1:1):一个学生只能拥有一张身份证,一张身份证只能属于一个学生: 2.一对多(1:N):一个学生可以拥有几本书,而 ...
- SQL-34 对于表actor批量插入如下数据
题目描述 对于表actor批量插入如下数据CREATE TABLE IF NOT EXISTS actor (actor_id smallint(5) NOT NULL PRIMARY KEY,fir ...
- html页面技巧
Query获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); //为S ...
- Highcharts 柱图 每个柱子外围的白色边框
Highcharts 柱图中每条柱外会有默认白色的边框, 去边框代码如下: plotOptions: { bar: { borderColor: "",//去边框 } }
- 使用zabbix-java-gateway可以通过该网关来监听多个JVM
我们知道监控主机和网络性能指标情况可以使用zabbix很好解决,分析起来也很方便,Zabbix主要功能: - 自动发现服务器与网络设备 - 分布式监视以及WEB集中管理功能 - 可以无agent监视 ...
- chromium ③
chromium源码学习笔记(1) -- 学习计划 对于c++开发者来说,chromium几乎是目前最值得学习的优秀开源代码.先看看chromium包含了多少令人激动的特性: 1, ...
- jetty调优
jetty服务器使用遇到一下内存溢出的问题: java.lang.OutOfMemoryError: unable to create new native thread 无法创建新的进程 方法: ...