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.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
随机推荐
- Python递归函数与斐波那契数列
定义:在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 阶乘实例 n = int(input(">>:")) def f(n): s ...
- Android学习总结——适配器
适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - 下拉列表控件.AutoComplet ...
- Funny Sheep(思维)
Problem 1606 - Funny Sheep Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 612 Accepted ...
- C的|、||、&、&&、异或、~、!运算(转)
位运算 位运算的运算分量只能是整型或字符型数据,位运算把运算对象看作是由二进位组成的位串信息,按位完成指定的运算,得到位串信息的结果. 位运算符有: &(按位与).|(按位或) ...
- 获取某月第一天,最后一天的sql server脚本
本文来自:http://blog.csdn.net/chaoowang/article/details/9167969 这是计算一个月第一天的SQL 脚本: SELECT DATEADD(mm, ...
- 怎样查询SCI和EI检索号
为了年终考核,花了一个早上才搞清楚,里面有非常多小问题.以下具体说明具体过程: SCI检索号 1.进入图书馆主页: 2.选择"电子数据库": 3.选择外文数据库中的"We ...
- 【贪心】【uva11520】 Fill the Square
填充正方形(Fill the Square, UVa 11520) 在一个n×n网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如 ...
- C# DES 加密解密
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...
- 使用Marshal.Copy把Txt行数据转为Struct类型值
添加重要的命名空间: using System.Runtime.InteropServices; 先建立结构相同(char长度相同)的Struct类型用于转换: [StructLayout(Layou ...
- SQL判断是否存在符合某条件的记录
IF EXISTS ( --判断是否存在合符条件的记录 ) FROM [DCL].[SecurityUser] WHERE [UserAccount] = @UserAccount ) BEGIN - ...