HDU 2732 Leapin' Lizards(网络流+建图)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37943488/article/details/81698356

Leapin' Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3742    Accepted Submission(s): 1535


 

Problem Description

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.

题目大意:两个n行的矩阵,第一个矩阵表示每个柱子的耐久值,第二个矩阵的'L'表示有一只蜥蜴在这个柱子上,问最后会有多少蜥蜴走不出去

拆点,如果当前柱子上有蜥蜴,就让他连着源点,容量为1,如果这个柱子的耐久值不等于0,就把它拆为两个点,连一条边,容量为耐久值,表示能跳多少次,对于那些能一下跳出边界的柱子,就把它连到汇点,对于那些不能一下跳出去的柱子,就找那些他能跳到的柱子,连一条边,容量为无穷

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxm=10010;
const int maxn=1010;
const int inf=0x3f3f3f3f;
struct Node
{
	int to;
	int capa;
	int next;
}edge[maxm];
int cnt;
int source,sink;
int head[maxn];
int dep[maxn];
bool vis[maxn];
char num[maxn][maxn];
char map[maxn][maxn];
void init()
{
	memset(head,-1,sizeof(head));
	cnt=0;
	return;
}
void add(int u,int v,int capa)
{
	edge[cnt].to=v;
	edge[cnt].capa=capa;
	edge[cnt].next=head[u];
	head[u]=cnt++;
	edge[cnt].to=u;
	edge[cnt].capa=0;
	edge[cnt].next=head[v];
	head[v]=cnt++;
	return;
}
bool bfs()
{
	queue<int> que;
	que.push(source);
	memset(dep,-1,sizeof(dep));
	dep[source]=0;
	while(!que.empty())
	{
		int node=que.front();
		que.pop();
		for(int i=head[node];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(edge[i].capa>0&&dep[v]==-1)
			{
				dep[v]=dep[node]+1;
				if(v==sink) return true;
				que.push(v);
			}
		}
	}
	return dep[sink]!=-1;
}
int dfs(int node,int minn)
{
	if(node==sink||minn==0)
	{
		return minn;
	}
	int r=0;
	for(int i=head[node];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(edge[i].capa>0&&dep[v]==dep[node]+1)
		{
			int tmp=dfs(v,min(edge[i].capa,minn));
			if(tmp>0)
			{
				edge[i].capa-=tmp;
				edge[i^1].capa+=tmp;
				r+=tmp;
				minn-=tmp;
				if(!minn) break;
			}
		}
	}
	if(!r) dep[node]=-1;
	return r;
}
int dinic()
{
	int maxflow=0;
	while(bfs())
	{
		maxflow+=dfs(source,inf);
	}
	return maxflow;
}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int test;
	scanf("%d",&test);
	for(int cas=1;cas<=test;cas++)
	{
		init();
		int n,m;
		scanf("%d%d",&n,&m);
		getchar();
		for(int i=1;i<=n;i++)
		{
			gets(num[i]+1);
		}
		for(int i=1;i<=n;i++)
		{
			gets(map[i]+1);
		}
		int len=strlen(num[1]+1);
		source=0;
		sink=n*len+n*len+10;
		int sum=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=len;j++)
			{
				int u=(i-1)*len+j;
				int v=(i-1)*len+j+n*len;
				if(map[i][j]=='L')
				{
					sum++;
					add(source,u,1);
				}
				if(num[i][j]-'0'>0)
				{
					add(u,v,num[i][j]-'0');
				}
				else continue;
				if(i+m>n||i-m<1||j+m>len||j-m<1)
				{
					add(v,sink,inf);
				}
				else
				{
					for(int a=1;a<=n;a++)
					{
						for(int b=1;b<=len;b++)
						{
							if(num[a][b]-'0'>0)
							{
								if(a!=i||b!=j)
								{
									if(abs(a-i)+abs(b-j)<=m)
									{
										int node=(a-1)*len+b;
										add(v,node,inf);
									}
								}
							}
						}
					}
				}
			}
		}
		int ans=dinic();	
		printf("Case #%d: ",cas);
		if(sum-ans==0)
		{
			puts("no lizard was left behind.");
		}
		else if(sum-ans==1)
		{
			printf("%d lizard was left behind.\n",sum-ans);
		}
		else
		{
			printf("%d lizards were left behind.\n",sum-ans);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/81698356