ZOJ Problem Set - 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的更多相关文章

  1. 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 ...

  2. zju 1002

    // zju 1002 // #include "stdafx.h" #include <string> #include <iostream> using ...

  3. [ZOJ 1002] Fire Net (简单地图搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋 ...

  4. zoj 1002 Fire Net (二分匹配)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  5. ZOJ 1002 Fire Net

    题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡.子弹不能穿透墙壁.问最多可以放置几个碉堡,保证它们不会相互误伤. 解法:从左上的顶点开始遍历,如果这个点不是墙,做深度 ...

  6. 1002 Fire Net

    用递归实现各种情况的枚举,可以看做是考察DPS的简单实现. #include <stdio.h> ][]; int place(int x,int y){ int i; ;i--){ ) ...

  7. zoj 1002 Fire Net 碉堡的最大数量【DFS】

    题目链接 题目大意: 假设我们有一个正方形的城市,并且街道是直的.城市的地图是n行n列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...

  8. ZOJ 1002 Fire Net(dfs)

    嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...

  9. Fire Net(深度优先搜索)

    ZOJ Problem Set - 1002 Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we ha ...

随机推荐

  1. 【Linux开发】IO streaming DMA buffer importing

    http://linuxtv.org/downloads/v4l-dvb-apis/dmabuf.html I/O流 (DMA缓存引用) 这是一个实验性接口,将来可能发生改变 DMABUF框架提供了在 ...

  2. linux rz上传-sz下载

    yum install lrzsz -y rz     上传文件    不能传目录 如果要传目录需要打包成文件再上传 需要往哪里传东西,先进入哪个目录 rz -y   上传覆盖 sz -y 文件名  ...

  3. [转帖]IIS7.5应用程序池集成模式和经典模式的区别介绍

    IIS7.5应用程序池集成模式和经典模式的区别介绍 之前转帖过一个 但是感觉不如这个说的细: https://www.jb51.net/article/31010.htm 关注脚本之家微信公众号(jb ...

  4. net 架构师-数据库-sql server-002-工具

    本章讲述的工具包括: SQL Server 联机丛书 SQL Server配置管理器 SQL Server Management Studio SQL Server Business Intellig ...

  5. [BZOJ3133] [Baltic2013]ballmachine(树上倍增+堆)

    [BZOJ3133] [Baltic2013]ballmachine(树上倍增+堆) 题面 有一个装球机器,构造可以看作是一棵树.有下面两种操作: 从根放入一个球,只要下方有空位,球会沿着树滚下.如果 ...

  6. Linux/Unix下pid文件作用浅析

    转载:http://blog.csdn.net/changli_90/article/details/8911191 在Linux系统的目录/var/run下面一般我们都会看到很多的*.pid文件.而 ...

  7. Centos7 yum安装LNMP

    1.Centos7系统库中默认是没有nginx的rpn包的,所以我们需要先更新下rpm依赖库 (1):使用yum安装nginx,安装nginx库 rpm -Uvh http://nginx.org/p ...

  8. 1、Java语言概述与开发环境——Java程序运行机制

    Java语言是一种特殊的高级语言,它既有解释型语言的特性,也具有编译型语言的特征,因为Java要经过先编译后解释两个步骤. 一.高级语言的运行机制 计算机高级语言按程序的执行方式可以分为编译型和解释型 ...

  9. Jmeter学习总结

    学习内容: 1.用户定义的变量 作用:多个地方使用同一个值,且该值在不同的环境下不同,方便脚本在不同环境下运行时修改. 2.基本的HTTP请求,请求方式:get 3.传入参数为json 4.HTTP信 ...

  10. iOS特殊界面旋屏设置的方法之一

    1.AppDelegate.h @property (assign, nonatomic) BOOL allowRotation; 2.AppDelegate.m #pragma mark - 自动旋 ...