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. markdown 的基本操作

    文档 http://mux.alimama.com/posts/620?spm=0.0.0.0.4VVnvp http://www.jianshu.com/p/1e402922ee32/ MdChar ...

  2. win7如何配置access数据源

    今天想做个管理系统,需要用到数据库,还要设置数据源,可是当我打开控制面板的管理工具的数据源发现只有SQL驱动,找不到access,因为我是用acess做的啊,所以就很郁闷,不过看到了一个方法,好吧,我 ...

  3. mac--mac杂记

    zsh路径补全.命令补全,命令参数补全,插件内容补全等等.触发补全只需要按一下或两下tab键,补全项可以使用ctrl+n/p/f/b上下左右切换. plugins=(git textmate ruby ...

  4. swift 定制自己的Button样式

    swift的UIButton类中有些公开方法可以重写,所以,如果想写出自己的UIButton,只要继承UIButton类,并重写相应的方法即可. 系统的UIButton可以添加图片,也可以添加标题,但 ...

  5. [转]初探 PhoneGap 框架在 Android 上的表现

    原文地址:http://topmanopensource.iteye.com/blog/1486929 phonegap是由温哥华的一家小公司研发的多平台的移动开发框架,支持流行的大多数移动设备(iP ...

  6. C#中不同的线程对控件的更改

    .net 不允许跨线程个性其它线程创建的控件. 要想实现这个功能就需要用 InvokeRequired 检查是不是由该线程创建的控件,如果是直接操作,如果不是则 用Invoke 添加一个委托再加上参数 ...

  7. 数据库(表)的逻辑备份与恢复<四>

    数据库(表)的逻辑备份与恢复  介绍 逻辑备份是指使用工具 export 将数据对象的结构和数据导出到文件的过程,逻辑恢复是指当数据库对象被误操作而损坏后使用 工具 import 利用备份的文件把数 ...

  8. eclipse 快捷键保存在哪里

    eclipse 快捷键文件地址:%Workspace%/.metadata/.plugins/org.eclipse.core.runtime /.settings/org.eclipse.ui.wo ...

  9. WebBrowser 中遍历所有的frames

    枚举所有iframe的IWebBrowser2 // Get the IDispatch of the document. // LPDISPATCH lpDisp = NULL; lpDisp = ...

  10. android 的touch event分析

    android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,有必要对它进行深入的了解.   一个最简单的屏幕触摸动作触发了一系列Touch事件:ACTION_DOWN ...