Fire Net(dfs)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7944 Accepted Submission(s): 4534
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>
#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)的更多相关文章
- HDU1045 Fire Net(DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- ZOJ 1002 Fire Net(dfs)
嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...
- hdu 1045 Fire Net(dfs)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
随机推荐
- PKU 3667 Hotel(线段树)
Hotel The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- LeeCode-Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Python load json file with UTF-8 BOM header - Stack Overflow
Python load json file with UTF-8 BOM header - Stack Overflow 3 down vote Since json.load(stream) use ...
- HDMI相关知识
HDMI热插拔检测原理 HDMI(19Pin)/DVI(16 pin)的功能是热插拔检测(HPD),这个信号将作为主机系统是否对HDMI/DVI是否发送TMDS信号的依据.HPD是从显示器输出送往计算 ...
- myeclipse修改tomcat端口
myeclipse自带的tomcat可以直接在myeclipse下面进行配置,但是如果是配置的tomcat 只能自己手动修改了
- kettle工具同步数据乱码-Linux下乱码问题二
将写好的kettle工程部署到Linux下后,同步的数据都成了乱码,幸运的是数据库有备份. 下面就说一下,kettle工程如何同步两端编码格式都是utf8的数据库. 我们只需要更改kettle数据库连 ...
- 【Unity3d】【项目学习心得】从资源server下载资源(一)
项目里面的很多资源都是从资源server载入的,这样子能够减小client的包大小. 所以我们须要一个专门的类来管理下载资源. 资源分非常多类型,如:json表,txt文件,image文件,二进制文件 ...
- CSS-边框-效果
1.1边框 其中边框圆角.边框阴影属性,应用十分广泛,兼容性也相对较好,具有符合渐进增强原则的特性,我们需要重点掌握. 1.1.1边框圆角 border-radius 每个角可以设置两个值,x值,y值 ...
- CREATE DATABASE建库语句详解
原创地址:http://blog.csdn.net/guguda2008/article/details/5716939 一个完整的建库语句是类似这样的: IF DB_ID('TEST') IS NO ...
- bootstrap 之 列表组件使用
列表是几乎所有网站都会用到的一个组件,正好bootstrap也给我们提供了这个组件的样式,下面我给大家简单介绍一下bootstrap中的列表组件的用法! 首先,重提一下引用bootstrap的核心文件 ...