Fire Net


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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'表示墙,现在往地图上放置炮塔,要求两两炮塔不能同行同列(除非之间有墙),给定一种地图,问这个地图最多可以放置多少个炮塔?
解析 AC代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
const int maxn=;
using namespace std;
typedef long long ll;
int n;
int ans; //最多可放炮塔数
char map[][];
int canput(int r,int c) //判断(r,c)位置能否放置炮塔
{
int i; //因为行列是递增的,因此只需向上和向左搜索是否有碉堡即可
for(i=r-;i>=&&map[i][c]!='X';i--) //向上
{
if(map[i][c]=='O')
return ;
}
for(i=c-;i>=&&map[r][i]!='X';i--) // 向左
{
if(map[r][i]=='O')
return ;
}
return ;
}
void solve(int k,int current)
{
int x,y;
if(k==n*n) //整个图判断完毕
{
if(current>ans) //更新最优解
{
ans=current;
return;
}
}
else
{
x=k/n; //将单元数转化为坐标
y=k%n;
if(map[x][y]=='.'&& canput(x,y)==) //如果单元格可以放置炮塔
{
map[x][y]='O'; //放置一个炮塔
solve(k+,current+); //递归到下一个单元格
map[x][y]='.'; //恢复单元格 ,回溯
}
solve(k+,current); //本单元格不能放置炮塔
}
}
int main(int argc, char const *argv[])
{
while(scanf("%d",&n)!=EOF&&n)
{
ans=;
for(int i=;i<n;i++)
{
scanf("%s",map[i]);
}
solve(,);
printf("%d\n",ans);
}
return ;
}

ZOJ 1002 DFS的更多相关文章

  1. DFS ZOJ 1002/HDOJ 1045 Fire Net

    题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...

  2. ZOJ 1002 Fire Net(dfs)

    嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...

  3. zoj 1002 Fire Net 碉堡的最大数量【DFS】

    题目链接 题目大意: 假设我们有一个正方形的城市,并且街道是直的.城市的地图是n行n列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...

  4. ZOJ 1002:Fire Net(DFS+回溯)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  5. [ZOJ 1002] Fire Net (简单地图搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋 ...

  6. zoj 1002 Fire Net (二分匹配)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  7. POJ 1979 Red and Black (zoj 2165) DFS

    传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  8. HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝

    传送门: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1010 ZOJ:http://acm.zju.edu.cn/onlinejudge/showPr ...

  9. POJ 1562 Oil Deposits (HDU 1241 ZOJ 1562) DFS

    现在,又可以和她没心没肺的开着玩笑,感觉真好. 思念,是一种后知后觉的痛. 她说,今后做好朋友吧,说这句话的时候都没感觉.. 我想我该恨我自己,肆无忌惮的把她带进我的梦,当成了梦的主角. 梦醒之后总是 ...

随机推荐

  1. MySQL创建用户与授权方法

    最近在弄个mysql兼职项目,记录一下: 一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username ...

  2. 使用Python Shapefile Library创建和编辑Shapefile文件

    介绍 shapefile是GIS中非常重要的一种数据类型,在ArcGIS中被称为要素类(Feature Classes),主要包括点(point).线(polyline)和多边形(polygon).P ...

  3. Hibernate缓存和状态

    缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.   缓存的介质一般是内存,所以读写速度很快.但如果缓存中存放的数据量非常大时,也会用硬盘 ...

  4. BeanShell断言(一)

    在beanShell中直接可以调用的变量,无需加前缀. 1.log 打印日志 log.info(“在控制台打印日志”); 2.SampleResult 获取SampleResult对象,可以通过这个对 ...

  5. springBoot系列教程08:拦截器(Interceptor)的使用

    拦截器intercprot  和 过滤器 Filter 其实作用类似 在最开始接触java 使用struts2的时候,里面都是filter 后来springmvc时就用interceptor 没太在意 ...

  6. K:常见的正则表达式

    @装载自:http://zxin.cnblogs.com/ 平时对字符串进行校验和处理的时候难免会用到正则表达式,通常采用的方式是去网上寻找相关的正则表达式,之后copy下来进行修改,以使其满足自己的 ...

  7. zookeeper部署到linux操作步骤

    安装zookeeper1. 上传上传zookeeper,在\01课前资料\02技术资料2. 移动[root@itcast-01 ~]# mv zookeeper-3.4.6.tar.gz /usr/l ...

  8. Java笔记:开发环境

    Java开发环境 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正 ...

  9. 浏览器根对象window之screen

    1. screen 1.1 availHeight/Width screen.availWidth返回浏览器窗口可占用的水平宽度(单位:像素). screen.availHeight返回浏览器窗口在屏 ...

  10. 安卓电量优化之WakeLock锁机制全面解析

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.WakeLock概述 wakelock是一种锁的机制,只要有应用拿着这个锁,CPU就无法进入休眠状态,一直处于工作状态.比如,手机屏幕在屏幕关闭 ...