【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 - 1060B 】Maximum Sum of Digits(思维,构造)

题干&#xff1a; You are given a positive integer nn. Let S(x)S(x) be sum of digits in base 10 representation of xx, for example, S(123)1236S(123)1236, S(0)0S(0)0. Your task is to find two integers a,ba,b, such that 0≤a,b≤n0≤a,b≤n, abnabn and S(a)S(…

mysql 异步_MySQL -- 异步I/O

linux上&#xff0c;innodb使用异步IO子系统(native AIO)来对数据文件页进行预读和写请求。行为受到参数innodb_use_native_aio控制。默认是开启的&#xff0c;且只是适用于linux平台&#xff0c;需要libaio库。在其他的类unix平台上&#xff0c;innodb使用的是同步I/O。由于历…

【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 - 1268】和为K的组合 (背包 或 dfs)

题干&#xff1a; 给出N个正整数组成的数组A&#xff0c;求能否从中选出若干个&#xff0c;使他们的和为K。如果可以&#xff0c;输出&#xff1a;"Yes"&#xff0c;否则输出"No"。 Input 第1行&#xff1a;2个数N, K, N为数组的长度, K为需要判断的和(…

centos7 mysql tar_CentOS7中mysql-5.7.21-el7-x86_64.tar.gz版MySQL的安装与配置

一、准备阶段通常情况下&#xff0c;MySQL在CentOS下主要使用glibc、rpm、yum等方式进行安装&#xff0c;使用mysql-5.7.21-el7-x86_64.tar.gz包进行安装的很少见&#xff0c;网上资料也较少。通过一上午的摸索&#xff0c;总结出如下安装方法。下载安装包&#xff1a;[rootGee…

【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/,进去下载默…

【51Nod - 1010 】只包含因子2 3 5的数 (打表,有坑越界)

题干&#xff1a; K的因子中只包含2 3 5。满足条件的前10个数是&#xff1a;2,3,4,5,6,8,9,10,12,15。 所有这样的K组成了一个序列S&#xff0c;现在给出一个数n&#xff0c;求S中 > 给定数的最小的数。 例如&#xff1a;n 13&#xff0c;S中 > 13的最小的数是15&…

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

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

mysql or全表_mysql or条件可以使用索引而避免全表

在某些情况下&#xff0c;or条件可以避免全表扫描的。1 .where 语句里面如果带有or条件, myisam表能用到索引&#xff0c; innodb不行。1)myisam表&#xff1a;CREATE TABLE IF NOT EXISTS a (id int(1) NOT NULL AUTO_INCREMENT,uid int(11) NOT NULL,aNum char(20) DEFAULT N…

【51Nod - 1163】最高的奖励 (贪心+优先队列 或 妙用并查集)

题干&#xff1a; 有N个任务&#xff0c;每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务&#xff0c;就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的&#xff0c;因为时间上可能会有冲突&#xff0…

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;只…

【CodeForces - 632B】Alice, Bob, Two Teams (预处理,思维,前缀和后缀和)

题干&#xff1a; Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: First, Alice will split …

mysql一张表1亿天数据_1亿条数据在PHP中实现Mysql数据库分表100张

转&#xff1a;1亿条数据在PHP中实现Mysql数据库分表100张http://php-z.com/thread-2115-1-1.html(出处: PHP-Z)当数据量猛增的时候&#xff0c;大家都会选择库表散列等等方式去优化数据读写速度。笔者做了一个简单的尝试&#xff0c;1亿条数据&#xff0c;分100张表。具体实现…

【POJ - 3273 】Monthly Expense (二分,最小最大值)

题干&#xff1a; Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over t…

Mysql8秒级加字段_Mysql8.0秒级加字段

Mysql 8.0版本合并了腾讯互娱数据库团队的Patch&#xff0c;可以实现秒级添加字段&#xff0c;这个功能可谓是mysql数据库攻城狮的福音&#xff0c;解决了之前5.6&#xff0c;5.7版本添加字段很高的运维成本。下面是验证mysql8.0版本秒级添加字段的过程首先用sysbench模拟一张1…