[ACM_图论] Fire Net (ZOJ 1002 带障碍棋盘布炮,互不攻击最大数量)
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 带障碍棋盘布炮,互不攻击最大数量)的更多相关文章
- Fire Net ZOJ - 1002
题意: 一个n * n 的棋盘 上面有些障碍物 放棋子 棋子不能在同一行 同一列 但可以在同一行或同一列隔着障碍物放 这题与poj1321 的思想差不多 对于一个位置 有两种状态放还是不放 参数i ...
- DFS ZOJ 1002/HDOJ 1045 Fire Net
题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...
- 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 (简单地图搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋 ...
- ZOJ 1002:Fire Net(DFS+回溯)
Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with s ...
- ZOJ 1002 Fire Net(dfs)
嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...
- [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 ...
- ZOJ 1002 Fire Net
题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡.子弹不能穿透墙壁.问最多可以放置几个碉堡,保证它们不会相互误伤. 解法:从左上的顶点开始遍历,如果这个点不是墙,做深度 ...
- zoj 1002 Fire Net 碉堡的最大数量【DFS】
题目链接 题目大意: 假设我们有一个正方形的城市,并且街道是直的.城市的地图是n行n列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...
随机推荐
- Border-radius属性--设置圆角边框
border-radius:该属性允许您为元素添加圆角边框! div { border:2px solid; border-radius:25px; -moz-border-radius:25px; ...
- A 浪哥的烦恼 完全背包dp
https://biancheng.love/contest-ng/index.html#/131/problems 首先,去到n点的最小时间是所有数加起来. 然后,如果我1 --- 2,然后再2-- ...
- 第三十八章 springboot+docker(maven)
回顾上一章的整个部署过程: 使用"mvn install"进行打包jar 将jar移动到与Dockerfile文件相同的文件夹下 编写Dockerfile文件 使用"do ...
- android studio 代理配置
android studio 代理设置,只支持http代理,不能用ss服务 中间加一层http转换 1远端ss 2client ss 端口 A 3client privoxy服服 代理ss A端口 到 ...
- LeetCode(四)
Find Kth Largest Number public class Solution { public int findKthLargest(int[] nums, int k) { retur ...
- Python-Windows下安装BeautifulSoup和requests第三方模块
http://blog.csdn.net/yannanxiu/article/details/50432498 首先给出官网地址: 1.Request官网 2.BeautifulSoup官网 我下载的 ...
- option2
option = { tooltip : { show: true, trigger: 'item' }, legend: { data:['邮件营销','联盟广告','直接访问','搜索引擎'] } ...
- 有关pascal的填充语句小技巧
背景 今天打代码,用了一次fillchar(a,sizeof(a),1); 结果a数组(of longint)所赋的值却不是1 探索 ···pascal program fillchartest; v ...
- android中 EditTex t的 inputType 属性
//文本类型,多为大写.小写和数字符号 android:inputType="none" android:inputType="text" a ...
- 关于hr标签兼容个浏览器的代码
hr标签,相信大家都能熟悉,我们一般用它来产生横线的效果.我们可以对它定义“颜色”.“高度”.“宽度”.“边框”等样式. 在此只讨论“颜色”和“边框”对于不同版本浏览器的兼容性. 颜色: 火狐.IE7 ...