博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdoj1045--Fire Net(二分图 转化 )
阅读量:7038 次
发布时间:2019-06-28

本文共 5450 字,大约阅读时间需要 18 分钟。

Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8620    Accepted Submission(s): 4976

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

 

Source
 

 

Recommend
We have carefully selected several similar problems for you:            
 
二分图行列匹配, 主要是处理过程;
#include 
#include
using namespace std;char Ap[5][5];int Gra[5][5], vis[5], dis[5];int dx[5], dy[5], left[5], right[5];int n;struct Node{ int x, y;}a[5][5];void Printf(){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ printf("%c", Ap[i][j]); } printf("\n"); }}int x, y;bool Search(int a){ for(int i = 1; i <= y; i++){ if(Gra[a][i] && !vis[i]){ vis[i] = 1; if(!dis[i] || Search(dis[i])){ dis[i] = a; return true; } } } return false;}int main(){ while(scanf("%d", &n) != EOF && n){ for(int i = 0; i < n; i++) { getchar(); for(int j = 0; j < n; j++) scanf("%c", &Ap[i][j]); } x = y = 0; for(int i = 0; i < n; i++) //行列匹配; for(int j = 0; j < n; j++) { if(Ap[i][j] == '.') { if(j == 0 || Ap[i][j-1] == 'X'){ x++; /***********************/ } a[i][j].x = x; } if(Ap[j][i] == '.') { if(j == 0 || Ap[j-1][i] == 'X'){ y++; /***********************/ } a[j][i].y = y; } } memset(Gra, 0, sizeof(Gra)); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++){ if(Ap[i][j] == '.'){ int u = a[i][j].x; int v = a[i][j].y; Gra[u][v] = 1; } } int ans = 0; memset(dis, 0, sizeof(dis)); for(int i = 1; i <= x; i++) { memset(vis, 0, sizeof(vis)); if(Search(i)) ans++; } printf("%d\n", ans); } return 0;}

 

暴搜;

#include 
int n, cnt;char map[5][5];bool Build(int row, int col){ int i, j; for(int i = row; i >= 0; i--){ if(map[i][col] == 'O') return false; if(map[i][col] == 'X') break; } for(int i = col; i >= 0; i--){ if(map[row][i] == 'O') return false; if(map[row][i] == 'X') break; } return true ;} void Dfs(int i, int num){ if(i == n*n){ // printf("%d %d\n", i, num); if(num > cnt) cnt = num; return; } else{ int row = i / n; int col = i % n; if(map[row][col] == '.' && Build(row, col)){ map[row][col] = 'O'; Dfs(i+1, num+1); map[row][col] = '.'; } Dfs(i+1, num); }}int main(){ while(scanf("%d", &n), n){ cnt = 0; for(int i = 0; i < n; i++) scanf("%s", map[i]); Dfs(0, 0); printf("%d\n", cnt); } return 0;}

 

转载于:https://www.cnblogs.com/soTired/p/5063031.html

你可能感兴趣的文章
Linux HA Cluster的实例演示(2)
查看>>
Javascript Closure
查看>>
Delphi之word报表
查看>>
重要博客
查看>>
解析C#开发过程常见的编程模式
查看>>
java单例模式Singleton
查看>>
JsonUtils工具整理
查看>>
Python操作Redis
查看>>
【C++ Primer】第六章(分支语句和逻辑操作符)
查看>>
centsos7修改主机名 [root@st152 ~]# cat /etc/hostname
查看>>
软件工程(2018)团体第五次作业
查看>>
windows phone 7 系列教程索引
查看>>
委托的异步编程和同步编程的使用( Invoke 和BeginInvoke)
查看>>
转载 iphone 获取iPhone用户手机号
查看>>
简单springmvc在Eclipse的Tomcat上部署404error,直接在Tomcat上部署可以访问
查看>>
17.文件上传、下载
查看>>
微信推出网页开发调试工具,方便广大微信开发工程师上线调试
查看>>
前端会遇到的算法
查看>>
apue第16章笔记
查看>>
4、android xml中drawableTop(drawableBoottom、drawableLeft、drawableRight)在java代码中的动态配置...
查看>>