1. 程式人生 > >hdu 2732 Leapin' Lizards (經典網路流)

hdu 2732 Leapin' Lizards (經典網路流)

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=2732

Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room's floor suddenly disappears! Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below... Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties.
The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety... but there's a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.

 

 

Input

The input file will begin with a line containing a single integer representing the number of test cases, which is at most 25. Each test case will begin with a line containing a single positive integer n representing the number of rows in the map, followed by a single non-negative integer d representing the maximum leaping distance for the lizards. Two maps will follow, each as a map of characters with one row per line. The first map will contain a digit (0-3) in each position representing the number of jumps the pillar in that position will sustain before collapsing (0 means there is no pillar there). The second map will follow, with an 'L' for every position where a lizard is on the pillar and a '.' for every empty pillar. There will never be a lizard on a position where there is no pillar.Each input map is guaranteed to be a rectangle of size n x m, where 1 ≤ n ≤ 20 and 1 ≤ m ≤ 20. The leaping distance is
always 1 ≤ d ≤ 3.

 

 

Output

For each input case, print a single line containing the number of lizards that could not escape. The format should follow the samples provided below.

 

 

Sample Input

 

4 3 1 1111 1111 1111 LLLL LLLL LLLL 3 2 00000 01110 00000 ..... .LLL. ..... 3 1 00000 01110 00000 ..... .LLL. ..... 5 2 00000000 02000000 00321100 02000000 00000000 ........ ........ ..LLLL.. ........ ........

 

 

Sample Output

 

Case #1: 2 lizards were left behind. Case #2: no lizard was left behind. Case #3: 3 lizards were left behind. Case #4: 1 lizard was left behind.

 

拆點 
1.將能夠直接跳出去的點,拆出的第一個點連到t,容量為限定的次數。 
2.不能直接跳出去的,將點1連到點2,容量為限定的次數。 
3.對於能跳到別的點的,將這個點2連到別的點的點1,容量為INF。 
4.對於柱子上有人的,從s連一條邊到該點的點1,容量為1。 
跑最大流。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
char mp1[30][30], mp2[30][30];
int level[maxn], iter[maxn];
int n, m, d;
int T;
int s, t, sum,k;
 struct edge
{
	int to, cap, rev;
};
 vector<edge> G[maxn];
void init()
{
	for (int i = 0; i <= 2 * n*m + 2; i++) G[i].clear();
	sum = 0;
}
int dis(int x1, int x2, int y1, int y2)
{
	return abs(x1 - x2) + abs(y1 - y2);
}
void addedge(int u, int v, int cap)
{
	G[u].push_back(edge{v,cap,(int)G[v].size() });
	G[v].push_back(edge{ u,0,(int)G[u].size() - 1 });
}
int to1(int i, int j)
{
	return i * m + j + 1;
}
int to2(int i, int j)
{
	return to1(i, j) + n * m;
}
void bfs(int st)
{
	memset(level, -1, sizeof(level));
	queue<int>pq;
	pq.push(st);
	level[st] = 0;
	while (!pq.empty())
	{
		int u = pq.front();
		pq.pop();
		for (int i = 0; i < G[u].size(); i++)
		{
			edge &e = G[u][i];
			if (e.cap > 0 && level[e.to] < 0)
			{
				level[e.to] = level[u] + 1;
				pq.push(e.to);
			}
		}
	}
}
int dfs(int u, int t, int f)
{
	if (u == t)
	{
		return f;
	}
	for (int &i = iter[u]; i < G[u].size(); i++)
	{
		edge& e = G[u][i];
		if (e.cap> 0 && level[e.to] > level[u]  )
		{
			int d = dfs(e.to, t, min(e.cap, f));
			if (d > 0)
			{
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}
int maxflow()
{
	int flow = 0;
	while (1)
	{
		bfs(s);
		memset(iter, 0, sizeof(iter));
		if (level[t] < 0)
		{
			return flow;
		}
		int f = 0;
		while ((f = dfs(s, t, inf)) > 0)
		{
			flow += f;
		}
	}
}
int main()
{
	//freopen("C:/input.txt", "r", stdin);
	scanf("%d", &T);
	k = 0;
	while (T--)
	{
		init();
		k++;
		scanf("%d %d", &n, &d);
		for (int i = 0; i < n; i++)
		{
			scanf("%s", mp1[i]);
		}
		m = strlen(mp1[0]);
		for (int i = 0; i < n; i++)
		{
			scanf("%s", mp2[i]);
		}
		s = 0;
		t = 2 * n * m + 1;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				if (mp1[i][j] - '0' > 0)
				{
					if (i + 1 <= d || j + 1 <= d || n - i <= d || m - j <= d)
					{
						addedge(to1(i, j), t, mp1[i][j] - '0');
						continue;
					}
					else
					{
						addedge(to1(i, j), to2(i, j), mp1[i][j] - '0');
					}
					for (int ii = -d; ii <= d; ii++)
					{
						for (int jj = -d; jj <= d; jj++)
						{
							int x = i + ii;
							int y = j + jj;
							if (x == i && j == y)
							{
								continue;
							}
							if (x < 0 || y<0 || x >= n || y >= m || dis(i, x, j, y) > d || mp1[x][y] - '0' == 0)
							{
								continue;
							}
							addedge(to2(i, j), to1(x, y), inf);
						}
					}
				}
			}
		}
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				if (mp2[i][j] == 'L')
				{
					addedge(s, to1(i, j), 1);
					sum++;
				}
			}
		}
		int flow = maxflow();
		int ans = sum - flow;
		printf("Case #%d: ", k);
		if (ans > 1) printf("%d lizards were left behind.\n", ans);
		else if (ans == 1) printf("1 lizard was left behind.\n");
		else if (ans == 0) printf("no lizard was left behind.\n");
	}
	return 0;
}