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. linux系统下phpstudy里的mysql使用方法

    linux作为一个优秀的服务器端管理系统,其实linux的桌面系统也用起来十分的nice.好吧,如何你在做开发的时候在linux下安装了lmap或者phpstudy,那么在第一次使用其自带的mysql ...

  2. Xamarin.Android AlertDialog中的EditText打上去字为什么不显示?也没有光标闪烁

    AlertDialog.Builder builder = new AlertDialog.Builder(this);            builder.SetTitle("请您输入管 ...

  3. Runtime那些事

    Runtime 前言 从字面意思看,就是运行时.但是这个运行时究竟什么意思?可以把它理解成:不是在编译期也不是在链接期,而是在运行时.那究竟在运行期间做了什么呢?按照苹果官方的说法,就是把一些决策(方 ...

  4. CSS3 Media Queries 特性的妙用

    第一招: 在网页中,pixel与point比值称为 device-pixel-ratio,普通设备都是1,iPhone 4是2,有些Android机型是1.5. 那么-webkit-min-devic ...

  5. Spring Boot实战:Restful API的构建

    上一篇文章讲解了通过Spring boot与JdbcTemplate.JPA和MyBatis的集成,实现对数据库的访问.今天主要给大家分享一下如何通过Spring boot向前端返回数据. 在现在的开 ...

  6. python基础(六)dict字典和文件操作open

    字典dict 使用key来标注value的数据类型,key和value是一一对应的.在字典中key是唯一的,所以字典也是无序的. #定义一个字典 dict = { 'name' : 'sylar', ...

  7. mysql数据库-------基础

    一 数据库是什么 把文件存放于一台机器,然后将多台机器通过网络去访问这台机器上的文件,即共享这台机器上的文件,共享则意味着竞争,会发生数据不安全,需要加锁处理,为了远程访问并处理这台共享机器上的文件, ...

  8. linux系统编辑神器 -vim用法大全

    vim编辑器 文本编辑器,字处理器ASCII nano, sed vi: Visual Interfacevim: VI iMproved 全屏编辑器,模式化编辑器 vim模式:编辑模式(命令模式)输 ...

  9. MySQL innodb_table_monitor 解析

    背景:       用innodb_table_monitor来查看表内部的存储信息和索引结构是一个好的办法.再之前的MySQL 字符串主键和整型主键分析中提到了一些内容,但没有细讲,现在来好好的分析 ...

  10. 在ASP.NET Core 2.0中使用MemoryCache

    说到内存缓存大家可能立马想到了HttpRuntime.Cache,它位于System.Web命名空间下,但是在ASP.NET Core中System.Web已经不复存在.今儿个就简单的聊聊如何在ASP ...