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

题目大意:输入一个n*n的棋盘,X表示障碍,点表示通路,欲在上面放置火力点(火力点可以摧毁直线上的任何东西,障碍物不能被摧毁),问最多能放多少火力点。

解题思路:回溯法

 #include<iostream>
#include<string.h>
#include<string>
using namespace std;
int n,tot,pro;
int map[][];//将输入转为map[][]矩阵中,其中坐标从(1,1)开始,0表示障碍,1表示通路,2表示炮
bool ok(int x,int y){
int X=x,Y=y;
while(--X>){//向上,遇墙停止,遇炮返回
if(map[X][Y]==)break;
if(map[X][Y]==)return ;
}X=x;
while(++X<=n){//向下,遇墙停止,遇炮返回
if(map[X][Y]==)break;
if(map[X][Y]==)return ;
}X=x;
while(--Y>){//向左,遇墙停止,遇炮返回
if(map[X][Y]==)break;
if(map[X][Y]==)return ;
}Y=y;
while(++Y<=n){//向右,遇墙停止,遇炮返回
if(map[X][Y]==)break;
if(map[X][Y]==)return ;
}
return ;
}//判断在该位置放置火力点是否和其他火力点相互摧毁
void dp(int x,int y){
if(pro>tot)tot=pro;//更新最值
for(int i=;i<=n;i++){//回溯模板
for(int j=;j<=n;j++){
if(map[i][j]== && ok(i,j)){
map[i][j]=;pro++;
dp(i,j);
map[i][j]=;pro--;
}
}
}
}
int main(){
while(cin>>n && n){
memset(map,,sizeof(map));
string str;
for(int i=;i<=n;i++){
cin>>str;
for(int j=;j<=n;j++){
if(str[j-]=='.'){
map[i][j]=;
}
}
}
tot=;pro=;
dp(,);
cout<<tot<<'\n';
}
}

[ACM_图论] Fire Net (ZOJ 1002 带障碍棋盘布炮,互不攻击最大数量)的更多相关文章

  1. Fire Net ZOJ - 1002

    题意: 一个n * n 的棋盘 上面有些障碍物  放棋子 棋子不能在同一行 同一列 但可以在同一行或同一列隔着障碍物放 这题与poj1321  的思想差不多 对于一个位置 有两种状态放还是不放 参数i ...

  2. DFS ZOJ 1002/HDOJ 1045 Fire Net

    题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...

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

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

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

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

  5. ZOJ 1002:Fire Net(DFS+回溯)

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

  6. ZOJ 1002 Fire Net(dfs)

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

  7. [ACM_图论] ZOJ 3708 [Density of Power Network 线路密度,a->b=b->a去重]

    The vast power system is the most complicated man-made system and the greatest engineering innovatio ...

  8. ZOJ 1002 Fire Net

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

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

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

随机推荐

  1. Framework7--Test

    <!doctype html> <html> <head> <title>{{title}}</title> <meta charse ...

  2. 说一说inline-block的奇葩之处

    今天本来想聊一下margin和padding,但是当我给div加了一个display:inline-block之后,发现一个问题: .box_demo{border: 1px solid #333;w ...

  3. scala 学习心得

    scala 安装步骤 文件下载地址:www.scala-lang.org(Please report bugs at https://issues.scala-lang.org/. We welcom ...

  4. 解决Ubuntu下 Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR)

    Ubuntu下CMake 编译时出现问题:Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR) 查找发现  # sudo apt-g ...

  5. [转载] 4. JebAPI 之 jeb.api.ui

    本文转载自: https://www.zybuluo.com/oro-oro/note/145250 JebInstance可以通过getUI()方法来获得jeb.api.ui.JebUI. JebU ...

  6. MySQL的binlog数据如何查看

    binlog介绍 binlog,即二进制日志,它记录了数据库上的所有改变. 改变数据库的SQL语句执行结束时,将在binlog的末尾写入一条记录,同时通知语句解析器,语句执行完毕. binlog格式 ...

  7. Codeforces 714C. Sonya and Queries Tire树

    C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...

  8. [MyBean-插件]MyBean通用报表免费无限制版本发布

      [优点]    1.开发时无需安装报表组件(可以直接用编译好的文件,注意版权说明,请自行编译一次相应的报表插件文件).    2.无带包烦恼所有版本Delphi都可以使用,不拖累Delphi版本的 ...

  9. ReferenceQueue的使用

    转:http://www.iflym.com/index.php/java-programe/201407140001.html 1 何为ReferenceQueue 在java的引用体系中,存在着强 ...

  10. C# HttpHelper 采集

    httphelper http://www.sufeinet.com/thread-6-1-1.html