A - Fire Net
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.
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.
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<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e12+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
int n,Max=0;
char a[6][6];
int check(int x,int y)
{
per(i,y-1,0)
{
if(a[x][i]=='x') return 0;
else if(a[x][i]=='X') break;
}
per(i,x-1,0)
{
if(a[i][y]=='x') return 0;
else if(a[i][y]=='X') break;
}
return 1;
}
void dfs(int deep,int sum)
{
int x=deep/n,y=deep%n;
if(deep>=n*n)
{
Max=max(Max,sum);
return ;
}
if(a[x][y]=='.')
{
if(check(x,y))
{
a[x][y]='x';
dfs(deep+1,sum+1);
a[x][y]='.';
}
}
dfs(deep+1,sum);
}
int main()
{
while(1)
{
Max=0;
sf("%d",&n);
if(!n) return 0;
mm(a,0);
rep(i,0,n)
sf("%s",&a[i]);
dfs(0,0);
pf("%d\n",Max);
}
}
A - Fire Net的更多相关文章
- 关于SequeezeNet中的Fire Module
在论文<SQUEEZENET: ALEXNET-LEVEL ACCURACY WITH 50X FEWER PARAMETERS AND <0.5MB MODEL SIZE>中,作者 ...
- FZU 2150 Fire Game
Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- Fire
Fire 分析: 首先,明确题意:b1,b2,--,bn 交换为b2,--,bn,b1,但这并不是意味着只能从b1开始交换,(这点从样例中可以看出),并且也不意味着交换的必须是连续的一串,可以是几个单 ...
- Android 轻量级输入校验库:Fire Eye
Fire Eye是一款轻量级简单易用的Android校验库. FireEye 2.0 在 1.0 的基础上,全部重写了代码,并优化了架构,性能上和逻辑上都大大提升.只需要几行代码,即可验证用户输入,并 ...
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- UVA 11624 Fire!(广度优先搜索)
题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...
- Amazon的Fire Phone之于Android开发者
在上周Amazon也耐不住加入了手机竞争行列之中,发布了自己的Fire Phone,于是Android家族又多了一位变种成员,Android系统的碎片化程度也进一步加剧.因为工作的关系,我有幸在上个月 ...
- Fire!(BFS)
Fire! Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descr ...
- FZU Problem 2150 Fire Game
Problem 2150 Fire Game Accept: 145 Submit: 542 Time Limit: 1000 mSec Memory Limit : 32768 KB P ...
- hdu 1045:Fire Net(DFS经典题)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- html5调用手机陀螺仪实现方向辨识
获取移动设备的陀螺仪,需要知道陀螺仪包含什么. 我们可以让document监听deviceorientation 来获取相关的数据,里面包括3个值 alpha.beta和gamma. 这三个值分别代表 ...
- XMPP接受发送消息
在现阶段的通信服务中,各种标准都有,因此会出现无法实现相互连通,而XMPP(Extensible Message and presence Protocol)协议的出现,实现了整个及时通信服务协议的互 ...
- 微信小程序wxss的background本地图片问题
在web 或者webapp开发中我们习惯了直接饮用本地图片做背景,例如 .aaa { background: url('img/1.png'); } 但是这种引用方式在微信小程序中是无法使用的,控制台 ...
- PL/SQL学习笔记之记录
一:记录 记录是一种高可以容纳不同数据类型的数据的数据结构. PL/SQL可以处理记录的以下几种类型: 基于数据表 基于游标的记录 用户自定义的记录 二:使用 %ROWTYPE属性 创建基于表格或基于 ...
- 【转发】Cookie存储的值大小限制和个数问题
Cookie以及LocalStorage.SessionStorage的区别 缓存机制对于用户访问网页来说是很方便的.如果将用户名,偏好设定或者其他一些数据存储到客户端,而不必每次访问网页都输入数据, ...
- 卸载系统自动jdk
执行下面的代码可以看到当前各种JDK版本和配置: sudo update-alternatives --config java 卸载系统自动jdk [root@localhost soft]# r ...
- 设计模式C++实现——工厂模式
软件领域中的设计模式为开发人员提供了一种使用专家设计经验的有效途径.设计模式中运用了面向对象编程语言的重要特性:封装.继承.多态,真正领悟设计模式的精髓是可能一个漫长的过程,需要大量实践经验的积累.最 ...
- [转]oracle 常用的指令
1.显示当前用户名 select user from dual; show user 2.显示当然用户有哪些表 select * from tab; 3.显示当所有用户的表 select * from ...
- 11G新特性 -- 收缩临时表空间
当大任务执行完毕,并不会立即释放临时表空间.有时候通过删除然后重建临时表空间的速度可能更快.不过对于在线系统可能不会那么容易删除重建,所以11g中可以在线收缩临时表空间或单个临时数据文件. 收缩临时表 ...
- 【转】AlphaGo Zero 和强人工智能
AlphaGo Zero 和强人工智能 前段时间比较热门的是 AlphaGo(阿法狗)的升级版:AlphaGo Zero(阿法狗零).跟阿法狗不同,阿法狗零不依赖于任何人类对弈记录,完全从围棋的规则出 ...