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. JMeter常用的4种参数化方式-操作解析

    目录结构 一.JMeter参数化简介 1.JMeter参数化的概念 2.JMeter参数化方式之使用场景对比 二.JMeter参数化的4种主要方式-操作演练 1.User Parameters(用户参 ...

  2. 可持久化并查集 by zky

    zz:https://www.cnblogs.com/cjoierljl/p/9567859.html https://www.cnblogs.com/peng-ym/p/9357220.html n ...

  3. maven 异常 提示 cannot be read or is not a valid ZIP file

    Archive for required library: 'D:/repository/Maven/org/springframework/spring-aop/4.3.6.RELEASE/spri ...

  4. 五、Zabbix-自动注册

    一.Zabbix Serber 1.进入动作界面 配置—>动作—>事件源—>自动注册—>创建动作 2.配置自动注册动作 1.配置动作 2.配置操作 Next step 添加成功 ...

  5. [转帖]探秘华为(二):华为和H3C(华三)的分道扬镳

    探秘华为(二):华为和H3C(华三)的分道扬镳 https://baijiahao.baidu.com/s?id=1620781715767053734&wfr=spider&for= ...

  6. 极*Java速成教程 - (8)

    Java高级特性 注解 注解可以在代码之外添加更多的信息,更加完整地描述程序,帮助编译器进行工作,或者实现某些特定的Java代码之外的功能. 注解可以简化某些重复的流程,自动化那些过程. 注解的使用 ...

  7. mysql数据库问题———登录进去无法操作显示You must reset your password using ALTER USER statement before executing this statement

    linux操作mysql数据库,可以登陆进去,但是操作所有命令都显示You must reset your password using ALTER USER statement before exe ...

  8. Python 入门之 Python三大器 之 生成器

    Python 入门之 Python三大器 之 生成器 1.生成器 (1)什么是生成器? 核心:生成器的本质就是一个迭代器 迭代器是Python自带的 生成器程序员自己写的一种迭代器 def func( ...

  9. css隐藏滚动条 兼容谷歌、火狐、IE等各个浏览器

    项目中,页面效果需要展示一个页面的移动端效果,使用的是一个苹果手机样式背景图,咋也没用过苹果,咋也不敢形容. 如下图所示: 在谷歌浏览器如图一滚动条顺利隐藏,但是火狐就如图二了,有了滚动条丑的一批. ...

  10. 算法学习之选择排序算法的python实现

    ——参考自<算法图解> def findSmallest(arr): # 假设第一个元素最小 smallest = arr[0] smallest_index = 0 for i in r ...