[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列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...
随机推荐
- Python 2.7_Second_try_爬取阳光电影网_获取电影下载地址并写入文件 20161207
1.昨天文章http://www.cnblogs.com/Mr-Cxy/p/6139705.html 是获取电影网站主菜单 然后获取每个菜单下的电影url 2.今天是对电影url 进行再次解析获取下 ...
- thinkphp加载 和url_model
1.加载thinkphp.php requrie('./ThinkPHP/ThinkPHP.php'); 2.加载核心文件 ./thinkPHP/LIB/core 3.加载项目的文件 分析URL 调用 ...
- vc 中调用COM组件的方法
需求:1.创建myCom.dll,该COM只有一个组件,两个接口: IGetRes--方法Hello(), IGetResEx--方法HelloEx() 2.在工程中导入组件或类型库 #im ...
- pcA降维算法
http://ufldl.stanford.edu/wiki/index.php/主成分分析 if ~exist('train_IM_all','var')||~exist('train_LA_all ...
- 基于WebView的混合编程
近日公司需求变更,以前一个页面是后台返回HTML字段,然后我们直接用webView接收,现在新增一个页面,数据后台返回非HTML,页面跟前面一直,所幸自己会点HTML,所以偷了个懒,自己用代码把数据组 ...
- JAVA学习<四>
一. 数组: Java 中操作数组只需要四个步骤: 1. 声明数组 语法: 数据类型[ ] 数组名: 或者 数据类型 数组名[ ]: 其中,数组名可以是任意合法的变量名. 2. 分配空间 简单地 ...
- Highcharts 的实际实践一
题记: 原先是想用chart.js 这个轻量级来完成我的需求的,结果基于我的数据不规则,所以实现不了. 我的需求: XX后台系统会产生有些报警日志. 我负责把这些数据按照图标的方式来展示. 这写报警日 ...
- [转] 你是as3老鸟吗?但是有些你可能目前都不知道的东西
你是as3老鸟吗?如果以下内容对你有莫大的帮助,请顶下! 一:加载swf库中的图片 new 的过程就是图片解压缩的过程.处于 Class 状态时,图片占用的内存和 SWF 文件中这个图片占用的磁盘空间 ...
- oracle创建用户、授予权限及删除用户
创建用户 oracle对表空间 USERS 无权限 alter user 用户名 quota unlimited on users; //创建临时表空间 create temporary ta ...
- sublime text保存时删除行尾空格
打开sublime text,点击在Preferences, Settings-User打开的用户配置中加入以下一行: "trim_trailing_white_space_on_save& ...