您的位置:首页 > 其它

hdu 1045 Fire Net(最大流)

2015-08-03 19:31 288 查看

hdu 1045 Fire Net

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




题目大意:炮台不能同行同列,除非中间有隔墙。

解题思路:可以用DFS来做,但最近在学最大流,所以用最大流来做。做法与hdu 5093 相同。

先用X把第一行第一列填满,然后遍历一遍把所有的 X 拆成两个点a和b设置一个超级源点连向所有的a,设置一个超级汇点,使所有的b连向它。拆完点之后遍历找 .,当找到一个 . 之后,算出他的左边第一个XA, 上边第一个XB,然后使A的a点连向B的b点,边的容量都为1。建完图后直接求最大流。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int PO = 10;
const int N = 100;
const int M = 500;
const int INF = 0x3f3f3f3f;
typedef long long ll;
char map[PO][PO];
int n, s, t, OF;

int ec, head
, first
, que
, lev
;  
int Next[M], to[M], v[M];  
  
void init() {  
    ec = 0;  
    memset(first, -1, sizeof(first));  
    memset(map, 0, sizeof(map));  
}  
  
void addEdge(int a,int b,int c) {  
    to[ec] = b;  
    v[ec] = c;  
    Next[ec] = first[a];  
    first[a] = ec++;  
  
    to[ec] = a;  
    v[ec] = 0;  
    Next[ec] = first[b];  
    first[b] = ec++;  
}  
  
int BFS() {  
    int kid, now, f = 0, r = 1, i;  
    memset(lev, 0, sizeof(lev));  
    que[0] = s, lev[s] = 1;  
    while (f < r) {  
        now = que[f++];  
        for (i = first[now]; i != -1; i = Next[i]) {  
            kid = to[i];      
            if (!lev[kid] && v[i]) {  
                lev[kid] = lev[now] + 1;      
                if (kid == t) return 1;  
                que[r++] = kid;  
            }  
        }  
    }  
    return 0;  
}  
  
int DFS(int now, int sum) {  
    int kid, flow, rt = 0;  
    if (now == t) return sum;  
    for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) {  
        head[now] = i;    
        kid = to[i];  
        if (lev[kid] == lev[now] + 1 && v[i]) {  
            flow = DFS(kid, min(sum - rt, v[i]));  
            if (flow) {  
                v[i] -= flow;  
                v[i^1] += flow;  
                rt += flow;  
            } else lev[kid] = -1;     
        }             
    }  
    return rt;  
}  
  
int dinic() {  
    int ans = 0;  
    while (BFS()) {  
        for (int i = 0; i <= t; i++) {  
            head[i] = first[i];  
        }             
        ans += DFS(s, INF);  
    }  
    return ans;  
}     

int findUp(int x, int y) {
	for (int i = x - 1; i >= 0; i--) {
		if (map[i][y] == 'X') return i * (n + 1) + y + 1;	
	}
}
int findLeft(int x, int y) {
	for (int i = y - 1; i >= 0; i--) {
		if (map[x][i] == 'X') return x * (n + 1) + i + 1;	
	}
}

void input() {
	OF = (n + 1) * (n + 1);
	s = 0, t = OF * 2 + 1;
	for (int i = 1; i <= n; i++) {
		scanf("%s", map[i] + 1);	
	}	
	for (int i = 0; i <= n; i++) {
		map[i][0] = 'X';	
		map[0][i] = 'X';
	}
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= n; j++) {
			if (map[i][j] == 'X') {
				int id = i * (n + 1) + j + 1;	
				addEdge(s, id, 1);
				addEdge(id + OF, t, 1);
			}		
		}	
	}
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= n; j++) {
			if (map[i][j] == '.') {
				int id1 = findUp(i, j);	
				int id2 = findLeft(i, j);
				addEdge(id2, id1 + OF, 1);
			}	
		}		
	}
}
int main() {
	while (scanf("%d\n", &n) != EOF, n) {
		init();
		input();	
		printf("%d\n", dinic());
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: