[ZJU 1002] Fire Net
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
Source: Zhejiang University Local Contest 2001
Solution:
每放一个点时,暴力判断是否合法。
#include<bits/stdc++.h>
using namespace std;
int a[][];
int n;
int ans=;
int check(int x,int y)
{
int tpx = x;
int tpy = y;
while (!a[tpx][tpy]) tpx--;
if (a[tpx][tpy] == ) return ;
tpx = x;
tpy = y;
while (!a[tpx][tpy]) tpy--;
if (a[tpx][tpy] == ) return ;
tpx = x;
tpy = y;
while (!a[tpx][tpy]) tpx++;
if (a[tpx][tpy] == ) return ;
tpx = x;
tpy = y;
while (!a[tpx][tpy]) tpy++;
if (a[tpx][tpy] == ) return ; return ;
}
void DFS(int dep)
{
ans = max(dep,ans);
for (int i=;i<=n;i++)
{
for (int j=;j<=n;j++)
if (a[i][j] == )
{
if (check(i,j))
{
a[i][j] = ;
DFS(dep+);
a[i][j] = ;
}
}
}
}
int main()
{
while (cin>>n,n)
{
memset(a,-,sizeof(a));
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
{
char tmp;
cin >> tmp;
if (tmp == 'X') a[i][j] = -;
else a[i][j] = ;
}
ans = ;
DFS();
cout << ans << endl;
}
}
ZJU第一题,加油!
[ZJU 1002] Fire Net的更多相关文章
- ZOJ(ZJU) 1002 Fire Net(深搜)
Suppose that we have a square city with straight streets. A map of a city is a square board with n r ...
- zju 1002
// zju 1002 // #include "stdafx.h" #include <string> #include <iostream> using ...
- [ZOJ 1002] Fire Net (简单地图搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋 ...
- zoj 1002 Fire Net (二分匹配)
Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with s ...
- ZOJ 1002 Fire Net
题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡.子弹不能穿透墙壁.问最多可以放置几个碉堡,保证它们不会相互误伤. 解法:从左上的顶点开始遍历,如果这个点不是墙,做深度 ...
- 1002 Fire Net
用递归实现各种情况的枚举,可以看做是考察DPS的简单实现. #include <stdio.h> ][]; int place(int x,int y){ int i; ;i--){ ) ...
- zoj 1002 Fire Net 碉堡的最大数量【DFS】
题目链接 题目大意: 假设我们有一个正方形的城市,并且街道是直的.城市的地图是n行n列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...
- ZOJ 1002 Fire Net(dfs)
嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...
- Fire Net(深度优先搜索)
ZOJ Problem Set - 1002 Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we ha ...
随机推荐
- springBoot(1) 环境安装
真鸡儿要命.今天做开发.实在是整不动了...所以决定回家,干一波... 发现MyEclipse2014好像真的不用能用了... 瞬间焦头烂额,本来,就跑不了.现在环境也没一个能用的. 于是 就有了今天 ...
- ActiveMQ学习教程/2.简单示例
ActiveMQ学习教程(二)——简单示例 一.应用IDEA构建Maven项目 File->New->Module...->Maven->勾选->选择->Next ...
- 精读《What's new in javascript》
1. 引言 本周精读的内容是:Google I/O 19. 2019 年 Google I/O 介绍了一些激动人心的 JS 新特性,这些特性有些已经被主流浏览器实现,并支持 polyfill,有些还在 ...
- 63 (OC)* NSAutoreleasePool 自动释放池
目录 0:ARC 1: 自动释放池 2:NSAutoreleasePool实现原理 3:autorelease 方法 4: Runloop和Autorelease的关系 5: Using Autore ...
- 剑指offer-旋转数组的最小数字-数组-python
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转 ...
- PowerEdge T630服务器安装机器学习环境(Ubuntu18.04、Nvidia 1080Ti驱动、CUDA及CUDNN安装)
安装步骤 在开始安装之前,我要说明一下,这个Ubuntu18.04系统的安装,使用的连接线(就是服务器和电脑显示器的连接线)必须两头都是VGA连接线,不能使用VGA转HDMI连接线,也不能用DVI转D ...
- MySQL存储过程、触发器、自定义函数、事务
1.存储过程 MySQL中存储过程的参数中有IN.OUT.INOUT类型,但是函数的参数只能是IN类型的. “in” 参数:跟 C 语言的函数参数的值传递类似, MySQL 存储过程内部可能会修改此参 ...
- LOAD - 装载或重载一个共享库文件
SYNOPSIS LOAD 'filename' DESCRIPTION 描述 这个命令装载一个共享库文件到PostgreSQL服务器的地址空间. 一旦一个文件被装载,如果该文件前面曾经装载过,那么服 ...
- Linux学习--第五天--vim使用、rpm命令
vim使用 三种模式: 编辑模式 插入模式 命令模式 |命令|作用| |--|--| |a|在光标字符后插入| |A|在光标行尾插入| |i|在光标字符前插入| |I|在光标行首插入| |o|在光标下 ...
- thinkphp5.0.19 request
一.请求类型 request类中 [F:\phpStudy\WWW\csweb\thinkphp\library\think\Request.php] 获取请求类型的方法分别是: isGet() .i ...