ZOJ1002 —— Fire net

Time Limit: 2000 ms

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个格子,每个格子是堡垒或城墙或空地。

你原来知道哪里有城墙或空地。

堡垒会向东西南北四方向发射炮弹,城墙能阻挡炮弹。

问最多能放几个堡垒。

题目思维:

1.我们遍历每个格子,能放就放,并且将放完后与它有冲突的格子进行标记,最后输出。

可惜如果这样做不到最大优化。

2.我们仍然深度优先搜索每个格子,这次如果没有打过标记,则将格子标记0或1,如果标记过则只标记0,继续向下遍历,最后填满后更新最大值。

注意没打过表记的格子,打完标记要还原。

Code:

// by kyrence
#include <bits/stdc++.h>
using namespace std; int n, ans;
bool can[][];
char c; bool valid(int x, int y) {
return x > && y > && x <= n && y <= n;
} void FW(int x, int y, int dx, int dy) {
do {
can[x][y] = ;
x += dx;
y += dy;
} while (valid(x, y) && can[x][y]);
} void dfs(int dep, int tot) {
if (dep > n * n) {
ans = max(tot, ans);
return;
}
dfs(dep + , tot);
int x = (dep + n - ) / n;
int y = dep % n;
if (!y) y = n;
if (!can[x][y]) return;
bool cam[][];
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
cam[i][j] = can[i][j];
FW(x, y, , );
FW(x, y, -, );
FW(x, y, , );
FW(x, y, , -);
dfs(dep + , tot + );
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
can[i][j] = cam[i][j];
} int main() {
while (scanf("%d", &n) == && n) {
ans = -;
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++) {
cin >> c;
can[i][j] = (c == '.');
}
dfs(, );
printf("%d\n", ans);
}
return ;
}

std

ZOJ1002 —— 深度优先搜索的更多相关文章

  1. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  2. 初涉深度优先搜索--Java学习笔记(二)

    版权声明: 本文由Faye_Zuo发布于http://www.cnblogs.com/zuofeiyi/, 本文可以被全部的转载或者部分使用,但请注明出处. 上周学习了数组和链表,有点基础了解以后,这 ...

  3. 挑战程序2.1.4 穷竭搜索>>深度优先搜索

      深度优先搜索DFS,从最开始状态出发,遍历一种状态到底,再回溯搜索第二种. 题目:POJ2386  思路:(⊙v⊙)嗯  和例题同理啊,从@开始,搜索到所有可以走到的地方,把那里改为一个值(@或者 ...

  4. 回溯 DFS 深度优先搜索[待更新]

      首先申明,本文根据微博博友 @JC向北 微博日志 整理得到,本文在这转载已经受作者授权!   1.概念   回溯算法 就是 如果这个节点不满足条件 (比如说已经被访问过了),就回到上一个节点尝试别 ...

  5. 总结A*,Dijkstra,广度优先搜索,深度优先搜索的复杂度比较

    广度优先搜索(BFS) 1.将头结点放入队列Q中 2.while Q!=空 u出队 遍历u的邻接表中的每个节点v 将v插入队列中 当使用无向图的邻接表时,复杂度为O(V^2) 当使用有向图的邻接表时, ...

  6. 深度优先搜索(DFS)

    定义: (维基百科:https://en.wikipedia.org/wiki/Depth-first_search) 深度优先搜索算法(Depth-First-Search),是搜索算法的一种.是沿 ...

  7. 图的遍历之深度优先搜索(DFS)

    深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...

  8. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. 算法总结—深度优先搜索DFS

    深度优先搜索(DFS) 往往利用递归函数实现(隐式地使用栈). 深度优先从最开始的状态出发,遍历所有可以到达的状态.由此可以对所有的状态进行操作,或列举出所有的状态. 1.poj2386 Lake C ...

随机推荐

  1. set的使用

    集合是Python的一种数据类型,集合是一个可变容器.常用于列表的去重. 什么是集合 集合是一个可变容器 集合中的数据对象都是唯一的(不可重复) 集合是无序的存储结构 集合是可迭代对象 集合内的元素是 ...

  2. android studio编译包真机安装失败解决方案记录

    Android studio升级到3.0之后,编译的APK文件无法在真机上安装,提示安装失败,最开始以为是API版本过高,与手机的版本不兼容,然后降低API,结果依然是安装失败. 然后连接手机,直接调 ...

  3. Spring作用域和BeenFactory

    1.Spring Bean实例作用域: ① singleton:   IOC容器仅创建一个Bean实例,IOC容器每次返回的是同一个Bean实例. ② prototype:   IOC容器可以创建多个 ...

  4. 2019-08-07 纪中NOIP模拟B组

    T1 [JZOJ1385] 直角三角形 题目描述 二维平面坐标系中有N个位置不同的点. 从N个点选择3个点,问有多少选法使得这3个点形成直角三角形. 数据范围 $3 \leq N \leq 1500$ ...

  5. 查询linux版本信息

    uname -a (Linux查看版本当前操作系统内核信息) cat /proc/version (Linux查看当前操作系统版本信息) cat /etc/redhat-release (Linux查 ...

  6. AcWing 1027. 方格取数

    #include<iostream> using namespace std ; ; *N][N][N]; int w[N][N]; int n; int main() { cin> ...

  7. C#中画三角形和填充三角形的简单实现

    C#中画三角形和填充三角形的简单实现: private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graph ...

  8. 数据库之一、数据库简介及SQL概要

    1.数据库简介: 数据库(Database,DB)是一个长期存储在计算机内的.有组织的.有共享的.统一管理的数据集合.简单来讲就是可以放大量数据的地方.管理数据库的计算机系统称为数据库管理系统(Dat ...

  9. 今天我解决的sql中文乱码问题

    昨天我终于把我的网站做好了,在电脑上准备就绪,经过测试一切正常,放上服务器上准备炫耀一下的时候,发现插进数据库的中文字段全都变成???了,检测了下,前台是utf-8,后台是utf-8,在插进数据库前我 ...

  10. 如何删除github中的repository

    打开个人界面->点击进入想删除的repository的界面->拉到最下面的danger zone->delete