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:

  1. 4
  2. .X..
  3. ....
  4. XX..
  5. ....
  6. 2
  7. XX
  8. .X
  9. 3
  10. .X.
  11. X.X
  12. .X.
  13. 3
  14. ...
  15. .XX
  16. .XX
  17. 4
  18. ....
  19. ....
  20. ....
  21. ....
  22. 0

Sample output:

  1. 5
  2. 1
  3. 5
  4. 2
  5. 4

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

解题思路:回溯法

  1. #include<iostream>
  2. #include<string.h>
  3. #include<string>
  4. using namespace std;
  5. int n,tot,pro;
  6. int map[][];//将输入转为map[][]矩阵中,其中坐标从(1,1)开始,0表示障碍,1表示通路,2表示炮
  7. bool ok(int x,int y){
  8. int X=x,Y=y;
  9. while(--X>){//向上,遇墙停止,遇炮返回
  10. if(map[X][Y]==)break;
  11. if(map[X][Y]==)return ;
  12. }X=x;
  13. while(++X<=n){//向下,遇墙停止,遇炮返回
  14. if(map[X][Y]==)break;
  15. if(map[X][Y]==)return ;
  16. }X=x;
  17. while(--Y>){//向左,遇墙停止,遇炮返回
  18. if(map[X][Y]==)break;
  19. if(map[X][Y]==)return ;
  20. }Y=y;
  21. while(++Y<=n){//向右,遇墙停止,遇炮返回
  22. if(map[X][Y]==)break;
  23. if(map[X][Y]==)return ;
  24. }
  25. return ;
  26. }//判断在该位置放置火力点是否和其他火力点相互摧毁
  27. void dp(int x,int y){
  28. if(pro>tot)tot=pro;//更新最值
  29. for(int i=;i<=n;i++){//回溯模板
  30. for(int j=;j<=n;j++){
  31. if(map[i][j]== && ok(i,j)){
  32. map[i][j]=;pro++;
  33. dp(i,j);
  34. map[i][j]=;pro--;
  35. }
  36. }
  37. }
  38. }
  39. int main(){
  40. while(cin>>n && n){
  41. memset(map,,sizeof(map));
  42. string str;
  43. for(int i=;i<=n;i++){
  44. cin>>str;
  45. for(int j=;j<=n;j++){
  46. if(str[j-]=='.'){
  47. map[i][j]=;
  48. }
  49. }
  50. }
  51. tot=;pro=;
  52. dp(,);
  53. cout<<tot<<'\n';
  54. }
  55. }

[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. js中object类型模拟java中的map

  2. 转 A Week with Mozilla's Rust

    转自http://relistan.com/a-week-with-mozilla-rust/ A Week with Mozilla's Rust I want another systems la ...

  3. hibernate学习(设计多对多 关系 映射)

    // package org.crazy.app.domain; import java.util.HashSet; import java.util.Set; import javax.persis ...

  4. aps.net 图形验证码(转)

    参考文章: http://www.cnblogs.com/FayJack/articles/3063146.html 创建CreateCode.ashx文件: <%@ WebHandler La ...

  5. [整理][LaTex]小技巧之——首行缩进

    0. 简介 在LaTex编辑时,有时会遇到这样一个有关于首行缩进的问题.在汉语环境的编辑下,习惯上每段会进行一个两个字的缩进.但是在默认编辑模式下,一个章节下的首段是没有首行缩进的,本文的目的主要是解 ...

  6. 第三章 springboot + jedisCluster

    如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制.(具体使用方式:第九章 企业项目开发--分布式缓存Redis(1)  第十章 企业项目开发--分布式缓存Redis(2)) 如果 ...

  7. Android开发涉及有点概念&相关知识点(待写)

    前言,承接之前的 IOS开发涉及有点概念&相关知识点,这次归纳的是Android开发相关,好废话不说了.. 先声明下,Android开发涉及概念比IOS杂很多,可能有很多都题不到的.. 首先由 ...

  8. Oracle primary,unique,foreign 区别,Hibernate 关联映射

    Oracle primary,unique,foreign 区别 转:http://www.cnblogs.com/henw/archive/2012/08/15/2639510.html NOT N ...

  9. 深入浅出Symfony2 - 如何提高网站响应速度 [转]

    简介 Symfony2是一个基于PHP语言的Web开发框架,有着开发速度快.性能高等特点.但Symfony2的学习曲线也比较陡峭,没有经验的初学者往往需要一些练习才能掌握其特性.相对其他框架,Symf ...

  10. Spring+Struts2/Hibernate 学习笔记

    ============Spring与Struts2整合============ (1)拷JAR包(Spring.Struts2) (2)配置org.springframework.web.conte ...