[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列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...
随机推荐
- Visual Studio 换皮肤
通过字体和颜色修改 Visual Studio 提供了修改配色的入口,你完全可以根据自己的喜好进行自定义,下面就通过该方法把编辑器背景设置成 “豆沙绿”. 选择 工具 / 选项 / 环境 / 字体和颜 ...
- 动端逐渐出了许多的移动端的框架,比如Sencha Touch、JQTouch、Jquery-moblie、jqMobi等等。这些框架都有优缺点,不同的框架应用在不同的项目中。现简单阐述一下各框架的优缺点:
移动前端工作的那些事---前端制作之微信小技巧篇 (2013-11-15 15:20) 转载▼ 标签: it css3/javascript html5 webapp 手机网站搭建 分类: 前端制 ...
- Fedora20安装fcitx输入法
Fedora20安装fcitx输入法 Fedora20默认安装的是ibus输入法,总有一些原因让我们选择fcitx输入法: ibus出词顺序有bug 在输入人名的时候,有些名字输入两三次后还是不会出现 ...
- 更改appstore开发商名字
个人账号,名字要改,google了半天也没找出解决方案,最后发邮件求助水果,发来解决办法. 您好: 感谢您参与 Apple 开发者计划.我是 Daniel , 非常荣幸能就更改 iTunes Conn ...
- Android-Service生命周期
Service的基本概念,以及Service的生命周期: 一.Service的基本概念: 一个Service就是应用程序的组件,可以在后台长期跑,或者是为其他的应用提供功能上的支持.Service一般 ...
- CSS从大图片上截取小图标的操作以及三角形的画法
#name{ background:url(images/name.png) no-repeat 2px 2px; background-position: -2px -70px;//其中这个是定位图 ...
- [golang]反射的用处--代码自动生成
背景: go语言处理db.json的时候,具体代码的变量定义和db字段.json输出的时候可能不一样. 这个时候,我们需要用tag的方式来进行定义. 例如: type MyStruct struct ...
- LaTex学习笔记(一):review
1.百分号 \% 2.下标 b_{ij} 3.表格(excel2latex) 符号说明 \begin{table}[H] \centering \begin{tabular}{m{100pt}< ...
- [MOSEK] Mosek求解中遇到的奇葩内存问题
在使用mosek优化库的时候,使用http://docs.mosek.com/7.0/capi/MSK_getxx_.html的 MSKrescodee MSK_getxx ( MSKtask_t t ...
- TNetHTTPClient演示
TNetHTTPClient演示 TNetHTTPClient是DELPHI新增加的异步HTTP通信控件(区别于INDY的阻塞控件). unit Unit1; interface uses Winap ...