【算法】算法之美—Fire Net

1045 Fire Net 炮台问题(类似八皇后问题)深搜算法DFS Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9880    Accepted Submission(s): 5739 Problem Description Suppose that we h 阅读详情

题目概述:Fire Net


  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.

  he  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(最大为4*4)的矩形表格中,由你指定在哪些表格不空(用"X"表示,代表wall),哪些表格是空的(用"."表示,可以建blockhouses),现在要在空的(".")表格中写O(建blockhouses),要求就是在水平或者竖直方向上不能有两个O直接或间接相邻,问最多可以写几个O(建blockhouses)?



题目分析

  1、不空的表格由自己决定,即为输入的一部分

  2、在水平或者竖直方向上不能有两个O直接或间接相邻,意味着需要作遍历判断

  3、最多可以写几个O(建blockhouses),意味着需要对所有表格进行分析

  下面贴出源代码,其中我对最主要的代码都作了详细的注释



解题算法

 
  1. #include < stdio.h>

  2. char map[4][4];

  3. int best,n;

  4. int CanPut(int row, int col)

  5. /*

  6. *检测与前行或者与前列是否存在冲突,即原文中的

  7. *no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them

  8. *如果bullets cannot run through,则返回1

  9. *否则bullets can run through,返回0

  10. */

  11. {

  12. int i;

  13. for (i = row - 1; i >= 0; i--)

  14.   {

  15. if (map[i][col] == 'O') return 0;

  16. if (map[i][col] == 'X') break;

  17.   }

  18. for (i = col - 1; i >= 0; i--)

  19.   {

  20. if (map[row][i] == 'O') return 0;

  21. if (map[row][i] == 'X') break;

  22.   }

  23. return 1;

  24. }

  25. void solve(int k,int tot)

  26. /*

  27. *calculates the maximum number of blockhouses that can be placed in the city in a legal configuration

  28. *k表示被检测的map单元个数

  29. *tot表示可以放置blockhouses的个数

  30. */

  31. {

  32. int x,y;

  33. if(k==n*n)//保证整个地图都被检测过

  34.   {

  35. if(tot>best)  

  36.     {

  37.        best=tot;    

  38. return;  

  39.     }

  40.   }

  41. else

  42.   {

  43.     x=k/n; //先逐行进行检测

  44.     y=k%n; //逐列进行检测

  45. if((map[x][y]=='.') && (CanPut(x,y) ) )//是open space,并且 bullets cannot run through

  46.     {

  47.       map[x][y]='O';//'0'表示已经检测过并且可放置blockhouses,即将tot+1

  48.       solve(k+1,tot+1);//map[x][y]可以放置blockhouses,则从map[(k+1)/n][(k+1)%n]开始继续检测,即逐行进行检测,并且tot+1

  49.       map[x][y]='.';//在恢复堆栈的时候,还原map原来的数据

  50.     }

  51.     solve(k+1,tot);//若map[k/n][k%n]存在bullets can run through,则继续从map[(k+1)/n][(k+1)%n]开始逐行检测

  52.   }

  53. }

  54. int main()

  55. {

  56. int i,j;

  57.   scanf("%d",&n);

  58. while(n>0)

  59.   {

  60. for(i=0;i< n;i++)

  61.     {

  62. for(j=0;j< n;j++)

  63.        {

  64.            scanf("%1s",&map[i][j]);//输入单个字符并且忽略空白

  65.        }

  66.     }

  67.     best=0;

  68.     solve(0,0);

  69.     printf("%d\n",best);

  70.     n=0;//预防scanf失败,reset n

  71.     scanf("%d",&n);

  72.   }

  73. return 0;

  74. }




本文出自 “成鹏致远” 博客,请务必保留此出处http://infohacker.blog.51cto.com/6751239/1194261

【代码超详解】HDU 1045 Fire Net(建图 + 二分图匹配) 一、题目描述 样例输入: Sample Input 4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0 样例输出: 5 1 5 2 4 二、算法分析说明与代码编写指导 本题的难点在于如何建立正确的图论模型。 一行、一列同时只能有一个炮,除非被墙挡住。炮弹向上下左右发射后,直到被... 阅读详情

相关推荐

UDP_千兆光通信(四)Tri Mode Ethernet MAC ip核

其中 Transmit Engine 从 AXI-Stream TX 接口接收数据,并添加前导码 Preamble、帧起始界定符 SFD、帧校验序列 FSC 等,并在必要时(长度小于最小 MAC 帧长度)填充数据,因此在使用 TEMAC 时,MAC 帧的这几个部分就不需要我们自己添加了(不过其他的 MAC 帧部分,可选的帧过滤器 Frame Filter 用于过滤与一组可配置过滤器匹配的帧,一般用于过滤掉目的 MAC 地址与本设备不一致的帧,TEMAC 默认开启帧过滤器。默认值为使用64位宽的计数器。

lzr232的博客 1786

【Acm】算法之美—Fire Net

题目概述:Fire Net     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  block...

weixin_33995481的博客 194

解锁笔记智能对话:手把手配置 Obsidian Copilot 插件与硅基流动模型

本文详细介绍了如何为Obsidian笔记软件配置Copilot插件,并接入硅基流动AI模型,从而为个人知识库添加智能对话能力。通过手把手教程,指导用户完成插件安装、API Key获取、模型配置及索引构建,最终实现基于个人笔记内容的深度语义问答,提升知识管理效率。

corn8的博客 618

[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 t...

weixin_33777877的博客 196

oh-my-zsh详细安装与主题插件配置

使用Linux的小伙伴都知道默认shell是bash,但是对于喜欢折腾,喜欢酷炫的我们bash怎能够用。zsh的作为一种shell,功能更强大,但是配置复杂。后来,有大佬创建了一个名为oh-my-zsh的开源项目,使用方便,功能强大。 下面就去oh-my-zsh的配置。 ①安装zsh 想要安装oh-my-zsh,首先得安装zsh,输入命令 sudo apt-get install zsh 界面如下 ②查看是否安装成功 输入命令 zsh --version 可以查看安装zsh的版本..

惜洛-Jankin的博客 2万+

Fortran环境配置:VS2022+Intel oneAPI安装全流程指南

Fortran环境配置:VS2022+Intel oneAPI安装全流程指南

shuyisuisuinian的博客 2万+

ACM-Fire Net

题目描述:Fire Net 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 blockh...

weixin_30810583的博客 189

告别有线:用PVE直通无线网卡,打造一台真正的‘无线’Win10软路由/测试机

本文详细介绍了如何通过PVE直通无线网卡技术,构建全无线Win10软路由或测试机。从硬件选型、PVE环境配置到PCIe和USB双模直通技术,逐步指导实现Wi-Fi AP和蓝牙连接功能,为家庭实验室和小型办公环境提供灵活高效的网络解决方案。

weixin_28683689的博客 595

杭电ACM1045——Fire Net

Fire Net Problem Description 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. ...

BarisGuo的博客 483

【易语言】大漠插件使用

首先下载大漠插件:大漠插件 下载完成后把Dm.dll和DmRed.dll放到同目录文件夹下,然后新建一个易语言程序。PS:DmReg.dll是大漠免注册。(不是账号免注册) 然后使用解压包中的工具,生成一个类文件。 打开类文件,全部复制。 去易程序上新添加一个类。 把数据复制进去,并且改类名为 dmsoft 给一个私有对象 Obj。 然后新建一个全局变量其数据类型就是 dmsoft 这个类。 好了到这里就可以使用这个变量使用dmsoft 类的方法了。 大漠免注册要使用DmReg.dll文件

weixin_45423515的博客 4万+

UCIE Specification详解(二)

UCIe架构概述。

Crazzy_M的博客 3347

基于YOLOv5和U-NET的火灾检测与分割

1.文章信息本次介绍的文章是发表在EUSIPCO 2021的一篇基于计算机视觉的火灾检测文章。2.摘要当今世界面临的环境危机是对人类的真正挑战。对人类和自然的一个显著危害是森林火灾的数量不断增加。由于传感器和技术以及计算机视觉算法的快速发展,提出了新的火灾探测方法。然而,这些方法面临着一些需要精确解决的限制,如类火灾物体的存在、高误报率、小尺寸火灾物体的检测和高推断时间。...

zuiyishihefang的博客 9251

Unity-RetargetPro3-动画插件试用

一个名气大于实战的插件。

大话程序员 718

Fire net之贪心解题报告

Fire Net Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4531    Accepted Submission(s): 2577 Problem Description Suppose that we have a

才子年华 1489

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Fire Net problem Python

最近发现一个联系编程语言的好方法 去做ACM的题目,用自己想学的语言去编 因为一直想学python,所以就用python了 Fire Net answer: ''' Created on 2013-1-21 @author: Administrator ''' class point: x=0 y=0 def __init__(self,x,y):

Json_Nie的专栏 932

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

ZOJ 1002 Fire Net 深度优先搜索算法

这道里原来我在看别人代码之前, 也写过。 洋洋洒洒的写了一个main函数四个嵌套for循环, 第一次的大手笔。运行结果没有问题, 但就是提交不上去, 还不收呢?搁置了好几天后, 没办法放弃。 后来看了一下别人的代码, 写完后提交编译错误, 都是因为跟系统函数重名缘故。  这道题的名头是,DFS(Depth-First-Search)深度优先搜索算法 #include "iostrea

fofu33的专栏 1157

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

浙大ACM 1002 Fire Net

先看看自己以前的题,找回点自己以前的思想,这题是我大一刚刚学C时写的,当时在练这题时,他们都说用搜索,但我用的是贪心,贪心一般是说找最多,这里我找的是最少,一个一个找下来,就OK了,你先找到这个点能被几个点打到,然后你就能求出最少要用几个点了。当时的代码写的是笨了点,但想法还是挺不错的。 Fire Net Time Limit: 2 Seconds      Memory Limit:

蔡斌松的专栏 962

Fire Net

题目:Fire Net 总结:构图+二分图匹配(匈牙利算法) #include <stdio.h> #include <vector> #include <string.h> using namespace std; const int N = 105; vector<int>G[N]; int n; char s[N][N]; int a[N][...

多一些不为什么的坚持 852

(dfs算法) Fire Net

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 castl

ices_的博客 404
上一篇: 【算法】数据结构与算法之美,解剖艺术
下一篇: 【算法】算法之美—Crashing Balloon
成鹏致远
博客等级 码龄14年 50粉丝 132原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址