[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 ...
随机推荐
- Django-DRF组件学习-视图学习
1.请求与响应 drf除了在数据序列化部分简写代码以外,还在视图中提供了简写操作.所以在django原有的django.views.View类基础上,drf封装了多个子类出来提供给我们使用. Djan ...
- 编译中出现的undefined reference to XXX
主要是在链接时发现找不到某个函数的实现文件.可以查找是否在makefile里没有增加待编译的.c文件,或者静态库没有引用
- 1000行基本SQL
/* Windows服务 */ -- 启动MySQL net start mysql -- 创建Windows服务 sc create mysql binPath= mysqld_bin_path(注 ...
- Centos中使用Docker部署Apollo
采用微服务开发框架开发项目时会涉及多个系统,如果要更改配置参数需要在多个系统间逐一更改,比较费时,而且容易遗漏,效率低下,次问题可以采用Apollo配置中心的方式解决,下面将介绍如何配置: 准备环境: ...
- JavaDoc注释
标签 说明 JDK 1.1 doclet 标准doclet 标签类型 @author 作者 作者标识 √ √ 包. 类.接口 @version 版本号 版本号 √ √ 包. 类.接口 @param 参 ...
- MyBatis框架 课程笔记
MyBatis框架 课程笔记 第1章 MyBatis简介 1.1 MyBatis历史 1)MyBatis是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Softw ...
- 如何去完成一个html网页
其实我也是菜鸟一个,不过我还是想说说,昨晚看的视频好回忆回忆. 看到这样一个网页我们要怎么入手,这个就要像庖丁解牛一样,我们要对它的各个位置都要了解,分析出页面的结构,要有一个总体的把握,先把这个页面 ...
- go 关键字之 defer
我是谁 defer - 顾名思义翻译过来叫 延迟, 所以我们通常称呼 defer func() 这样 defer 后面紧跟的函数为 延迟函数. 作者注: 不过从实际应用来讲, 延迟函数通常用来做一些函 ...
- go & cron
https://github.com/robfig/cron - 源码 cron - GoDoc 参考 go---定时任务 cron,gin 静态文件 go语言里比较好用的计划任务调度模块
- HTTPS和HTTP的区别,http协议的特征
http协议传输的数据都是没有经过加密的,也就是明文,所以http用于传输数据并不安全.而https是是使用了ssl(secure socket layer)协议+http协议构成的可加密传输,身份认 ...