【HDU - 1045】Fire Net (dfs 或二分图)

题干:

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. 

Input

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.

Output

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

解题报告:

      这题水啊,数据范围4*4,比赛的时候直接dfs一发。找准dfs定义的状态的含义,不难写,有点类似八皇后的递归思路。

     二分图显然要把原始图分别按行和列缩点。建图:横竖分区。同一列相连的空地同时看成一个点,显然这样的区域不能够同时放两个点。这些点作为二分图的X部。同理在对所有的行用相同的方法缩点,作为Y部。

     连边的条件是两个区域有相交部分(即'.'的地方)。最后求最大匹配就是答案。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,ans;
char maze[10][10];
bool bk[10][10];
bool ok(int x,int y) {for(int i = x; i>=1; i--) {if(maze[i][y] == 'X') break;if(bk[i][y] == 1) return 0;}for(int i = y; i>=1; i--) {if(maze[x][i] == 'X') break;if(bk[x][i] == 1) return 0;}return 1;
}
void dfs(int x,int y,int cur) {if(x == n && y == n) {if(maze[x][y] == '.' &&ok(x,y)) cur++;ans = max(ans,cur);return ;}if(y == n) {if(maze[x][y] == '.' && ok(x,y)) {bk[x][y]=1;
//			make(x,y);dfs(x+1,1,cur+1);bk[x][y]=0;}dfs(x+1,1,cur);}else {if(maze[x][y] == '.' && ok(x,y)) {bk[x][y]=1;dfs(x,y+1,cur+1);bk[x][y]=0;}dfs(x,y+1,cur);}}
int main() 
{while(~scanf("%d",&n) ) {if(n == 0) break;memset(bk,0,sizeof bk);ans=0;for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}dfs(1,1,0);printf("%d\n",ans);		}return 0 ;
}

AC代码2:(二分图)

#include<bits/stdc++.h>using namespace std;
char maze[55][55];
int visx[55][55],visy[55][55];
int n;
int totx,toty;
bool line[55][55];
int nxt[55];
bool used[55];
bool find(int x) {for(int i = 1; i<=toty; i++) {if(line[x][i] && used[i]==0) {used[i]=1;if(nxt[i] == -1 || find(nxt[i])) {nxt[i] = x;return 1;}}}return 0;
} 
int match() {int sum = 0;memset(nxt,-1,sizeof nxt);for(int i = 1; i<=totx; i++) {memset(used,0,sizeof used);if(find(i)) sum++;}return sum;
}
int main()
{while(~scanf("%d",&n)) {if(n == 0) break;memset(visx,0,sizeof visx);memset(visy,0,sizeof visy);memset(line,0,sizeof line);totx=toty=0;for(int i = 1; i<=n; i++) scanf("%s",maze[i]+1);for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j] == '.') {if(visx[i][j-1]!=0) visx[i][j] = visx[i][j-1];else visx[i][j] = ++totx;if(visy[i-1][j]!=0) visy[i][j] = visy[i-1][j];else visy[i][j] = ++toty;}}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j] == '.') {line[visx[i][j]][visy[i][j]] = 1;
//					printf("%d %d\n",visx[i][j],visy[i][j]);}}}printf("%d\n",match());} return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441464.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html 按钮 按下 状态_第一次按下是启动,第二次按下是停止,俵哥分享2种接线方法...

朋友们大家好我是大俵哥&#xff0c;今天我们来说一下单按钮启停电路。这个电路虽然应用的不多&#xff0c;但是非常的经典&#xff0c;新手朋友们可以拿来练手。今天我们讲2种控制方法&#xff0c;一种用中间继电器控制一种用时间继电器控制&#xff0c;在看电路之前&#xff…

【CodeForces - 633D】Fibonacci-ish (离散化,暴力枚举+STPmap,fib数列收敛性质)

题干&#xff1a; Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elementsf0 and f1 are arbitraryfn  2  fn  1  fn for all n ≥ 0. You …

sql server 迁移 mysql_【转】sql server迁移到mysql

【1】MSSQL2SQLSQL Server转换为MySQL工具&#xff0c;用了一下 感觉蛮不错的。分享上来&#xff0c;同时也以便记录下来以后自用。工具名称&#xff1a;Mss2sql来个操作流程:下载后打开压缩包运行mss2sql默认就是Move to MysQL server directly,选择下一步继续下一步,稍等片刻…

【51Nod - 1416】两点 (dfs 或 并查集+dfs)

题干&#xff1a; 福克斯在玩一款手机解迷游戏&#xff0c;这个游戏叫做”两点”。基础级别的时候是在一个nm单元上玩的。像这样&#xff1a; 每一个单元有包含一个有色点。我们将用不同的大写字母来表示不同的颜色。 这个游戏的关键是要找出一个包含同一颜色的环。看上图中4…

linux 源码安装mysql5.7_linux安装mysql5.7.27

一、卸载mysql安装有三种方式&#xff0c;包括二进制包安装(Using Generic Binaries)、RPM包安装、源码安装。一般是前两种比较多二、安装建议路径设置按照写的来将下载的压缩包复制到linux服务器/usr/local/路径下(下载地址https://dev.mysql.com/downloads/mysql/,进去下载默…

c语言可以将负数强制转换成正数吗_C语言笔记(一、概述)

1&#xff0e; C语言的特点 ①语言简洁、紧凑&#xff0c;使用方便、灵活。共有&#xff13;&#xff12;个关键字(也称保留字)&#xff0c;&#xff19;种控制语句。 ②运算符丰富&#xff0c;共有&#xff13;&#xff14;种运算符。 ③数据结构丰富&#xff0c;数据类型有&a…

mysql varchar java_关于MySQL varchar类型最大值,原来一直都理解错了

写在前面关于MySQL varchar字段类型的最大值计算&#xff0c;也许我们一直都理解错误了&#xff0c;本文从问题出发&#xff0c;经实践验证得出一些实用经验&#xff0c;希望对大家的开发工作有些帮助~背景描述最近同事在做技术方案设计时候&#xff0c;考虑到一个表设计时希望…

【CodeForces - 1027C】Minimum Value Rectangle (数学,公式化简,思维,卡常卡memset)

题干&#xff1a; You have nn sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can …

mysql数据库业务逻辑_Mysql业务设计(逻辑设计)

逻辑设计数据库设计三大范式数据库设计第一大范式数据库表中所有的字段都只具有单一属性单一属性的列是由基本数据类型所构成设计出来的表都是简单的二维表数据库设计的第二大范式要求表中只有一个业务主键&#xff0c;也就是说符合第二范式的表不能存在非主键列&#xff0c;只…

lua进入压缩包_使用lua语言制作贪吃蛇游戏(love2d)(一)开发环境的搭建

本教程教大家使用lua制作一个贪吃蛇&#xff0c;游戏引擎使用love2d&#xff0c;因为它开源轻巧而且跨平台。1.开发环境搭建&#xff1a;windows系统&#xff1a;在windows系统下&#xff0c;首先我们进入官网www.love2d.org。love2d官网进入官网可以看到Download选项&#xff…

mysql rand() 子查询_MySQL ------ 子查询(十三)

查询&#xff08;query&#xff09;:任何SQL 都是查询&#xff0c;但此术语一般指select 语句子查询&#xff08;subquery&#xff09;:嵌套在查询中的查询&#xff0c;MySQL4.1 引入对子查询的支持。接下来得就比较有意思了&#xff0c;需要你对于表与表之间的关系有所了解&am…

centos 6.5 apache mysql php_CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境

简单点的&#xff1a;1.关闭SELINUX&#xff1a;setenfo 0 暂时关闭2.安装Apache&#xff1a;yum install httpd3.安装MySQL&#xff1a;yum install mysql mysql-server4.安装PHP&#xff1a;yum install php5.相关的配置&#xff1a;PHP关联MySQL&#xff1b;httpd出错信息…

mysql 如何调用函数结果_MySQL自定义函数调用不出结果

自定义函数的代码&#xff1a;DROP FUNCTION IF EXISTS fn_HrStaffBase_GetNameFromidCarddelimiter //CREATE FUNCTION fn_HrStaffBase_GetNameFromidCard (a VARCHAR(30))RETURNS VARCHAR(50)beginreturn (SELECT staff_name FROM hr_staff_base where idCard a);END //--…

【CodeForces - 987C 】Three displays (dp,最长上升子序列类问题,三元组问题)

题干&#xff1a; It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem. There are nn displays placed along a road, and the ii-th of th…

git object 很大_这才是真正的Git——Git内部原理

本文以一个具体例子结合动图介绍了Git的内部原理&#xff0c;包括Git是什么储存我们的代码和变更历史的、更改一个文件时&#xff0c;Git内部是怎么变化的、Git这样实现的好处等等。TL;DR本文以一个具体例子结合动图介绍了Git的内部原理&#xff0c;包括Git是什么储存我们的代码…

【CodeForces - 195D】Analyzing Polyline (思维,卡精度的处理方式)

题干&#xff1a; As Valeric and Valerko were watching one of the last Euro Championship games in a sports bar, they broke a mug. Of course, the guys paid for it but the barman said that he will let them watch football in his bar only if they help his son …

【CodeForces - 985D】Sand Fortress (二分,贪心,思维构造,技巧,有坑)

题干&#xff1a; You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbe…

scrapy 分布式 mysql_Scrapy基于scrapy_redis实现分布式爬虫部署的示例

准备工作1.安装scrapy_redis包,打开cmd工具,执行命令pip install scrapy_redis2.准备好一个没有BUG,没有报错的爬虫项目3.准备好redis主服务器还有跟程序相关的mysql数据库前提mysql数据库要打开允许远程连接,因为mysql安装后root用户默认只允许本地连接,详情请看此文章部署过程…

(精)DEVC++的几个实用小技巧

依赖 DEV C 5.11 最新版 下载安装DEV C后&#xff0c;使用DEV C打开一个随便的cpp文件&#xff0c;你看到的应该是这样的界面。&#xff08;为了节约读者的流量&#xff0c;图片进行了有损压缩&#xff0c;但是字看得清楚&#xff09; 重点是确认工具栏有AStyle选项。 相信…

win10一按右键就闪屏_升级Win10正式版后屏幕一直闪烁正确的解决办法

Win10正式版屏幕一直闪烁怎么办呢&#xff1f;升级到Win10正式版并进入Windows桌面后&#xff0c;发现屏幕一直不断的闪烁&#xff0c;此时无法执行任务操作。小编最近在升级到Win10正式版后才遇到了这个问题&#xff0c;后台经过反复思考和探索&#xff0c;终于解决了问题&…