首页 > 其他分享 >HDU 2732 Leapin' Lizards(最大流)

HDU 2732 Leapin' Lizards(最大流)

时间:2023-06-12 14:38:35浏览次数:40  
标签:HDU Lizards Leapin pillar flow 网格 int lizard include


题意:给你一个网格,网格上的一些位置上有一只蜥蜴,所有蜥蜴的最大跳跃距离是d,如果一只蜥蜴能跳出网格边缘,那么它就安全了.且每个网格有一个最大跳出次数x,即最多有x只蜥蜴从这个网格跳出,这个网格就再也不能有蜥蜴进来了.问你最少有多少只蜥蜴跳不出网格.

思路:和POJ3498差不多...关键是能看懂题目..看题目看的心累..

         建图:

         源点编号0,网格的每个格子拆成两个点i和i+n*m(n和m为网格的行和列数,其实i编号点是表示蜥蜴进来,而i+n*m编号的点是表示蜥蜴出去).汇点t编号n*m*2+1.

         如果格子i上有蜥蜴,那么从s到i有边(s,i,1).

         如果格子i能承受x次跳出,那么有边(i,i+n*m,x)

         如果从格子i能直接跳出网格边界,那么有边(i+n*m,t,INF)

         如果从格子i不能直接跳出网格,那么从i到离i距离<=d的网格j有边(i+n*m,j,INF). 注意这里的距离是abs(行号之差)+abs(列号之差)

         最终我们求出的最大流就是能跳出网格的蜥蜴数.



#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 1200
#define INF 1<<29
#define LL long long
int cas=1,T;
struct Edge
{
	int from,to,cap,flow;
	Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
int n,m;
struct Dinic
{
//	int n,m;
    int s,t;
	vector<Edge>edges;        //边数的两倍
	vector<int> G[maxn];      //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
	bool vis[maxn];           //BFS使用
	int d[maxn];              //从起点到i的距离
	int cur[maxn];            //当前弧下标
	void init()
	{
	   for (int i=0;i<=n*m*2+2;i++)
		   G[i].clear();
	   edges.clear();
	}
	void AddEdge(int from,int to,int cap)
	{
		edges.push_back(Edge(from,to,cap,0));
		edges.push_back(Edge(to,from,0,0));        //反向弧
		int mm=edges.size();
		G[from].push_back(mm-2);
		G[to].push_back(mm-1);
	}
	bool BFS()
	{
		memset(vis,0,sizeof(vis));
		queue<int>q;
		q.push(s);
		d[s]=0;
		vis[s]=1;
		while (!q.empty())
		{
			int x = q.front();q.pop();
			for (int i = 0;i<G[x].size();i++)
			{
				Edge &e = edges[G[x][i]];
				if (!vis[e.to] && e.cap > e.flow)
				{
					vis[e.to]=1;
					d[e.to] = d[x]+1;
					q.push(e.to);
				}
			}
		}
		return vis[t];
	}

	int DFS(int x,int a)
	{
		if (x==t || a==0)
			return a;
		int flow = 0,f;
		for(int &i=cur[x];i<G[x].size();i++)
		{
			Edge &e = edges[G[x][i]];
			if (d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)
			{
				e.flow+=f;
				edges[G[x][i]^1].flow-=f;
				flow+=f;
				a-=f;
				if (a==0)
					break;
			}
		}
		return flow;
	}

	int Maxflow(int s,int t)
	{
		this->s=s;
		this->t=t;
		int flow = 0;
		while (BFS())
		{
			memset(cur,0,sizeof(cur));
			flow+=DFS(s,INF);
		}
		return flow;
	}
}dc;


int main()
{
	scanf("%d",&T);
	while (T--)
	{
		int d;
		int sum = 0;
		scanf("%d%d",&n,&d);
		
		for (int i = 1;i<=n;i++)
		{
			string s;
			cin >> s;
			if (i==1)
			{
				m=s.size();
				dc.init();
			}
			for (int j = 0;j<s.size();j++)
			{
				if (s[j]-'0'>0)
				{
					int id = (i-1)*m+j+1;
					dc.AddEdge(id,id+n*m,s[j]-'0');
					if (i<=d || i+d>n || j<d || j+d>=m)
					{
						dc.AddEdge(id+n*m,2*n*m+1,INF);
					}
					else
					{
						for (int k = 1;k<=n;k++)
							for (int h = 0;h<m;h++)
							{
								int id1=(k-1)*m+h+1;
								if (id == id1)
									continue;
								if (abs(i-k)+abs(j-h) <=d)
									dc.AddEdge(id+n*m,id1,INF);
							}
					}
				}
			}
		}
		for (int i =1;i<=n;i++)
		{
			string s;
			cin >> s;
			for (int j = 0;j<s.size();j++)
			{
				int id = (i-1)*m+j+1;
				if (s[j]=='L')
				{
					++sum;
					dc.AddEdge(0,id,1);
				}
			}
		}
		int ans = sum-dc.Maxflow(0,2*m*n+1);
		if (!ans)
			printf("Case #%d: no lizard was left behind.\n",cas++);
		else if (ans==1)
			printf("Case #%d: 1 lizard was left behind.\n",cas++);
		else
			printf("Case #%d: %d lizards were left behind.\n",cas++,ans);

	}
}




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.



 







标签:HDU,Lizards,Leapin,pillar,flow,网格,int,lizard,include
From: https://blog.51cto.com/u_16156555/6462527

相关文章

  • hdu2068 RPG的错排(错排)
    思路:我们定义f(n)为n个人抽到的情况总数。对于第n个人,他要不抽中自己,即要抽中其他n-1个人,有n-1种可能,接下来讨论下,如果第n个人它抽中的人也抽中了第n个人,那么有f(n-2)种情况,如果第n个人抽中的人没有抽中第n个人,那么有f(n-1)可能,所以f(n)=(n-1)*(f(n-1)+f(n-2))。      ......
  • hdu2079选课时间(背包)
    思路:相当于一个裸的多重背包#include<iostream>#include<cstdio>usingnamespacestd;inta[20],num[20],dp[50];intmain(){ intT; scanf("%d",&T); while(T--) { intn,m; scanf("%d%d",&n,&m); for(inti=1;i<=m;i......
  • hdu2066 一个人的旅行(最短路)
    思路:简单最短路#include<stdio.h>#include<string.h>#include<algorithm>usingnamespacestd;constintinf=1<<30;intT,S,D,n;intmap[1111][1111];intvis[1111],cast[1111];ints[1111],e[1111];voidDijkstra(){inti,j,minn,p......
  • HDU 2838 Cow Sorting(树状数组)
    题意:首先每次只能交换相邻的两头牛,并且最后要求升序排列,所以最后整个序列的逆序是0,每次交换只可以消除1个逆序。(令a[i]的逆序是从1到i-1比它大的数的个数。)思路:对于某个数,要把它变成有序的,那么很容易可以推算出公式就是它自身的逆序数乘自身的值再加上它的逆序数的和,自己算算看看。......
  • HDU 1394 Minimum Inversion Number(树状数组)
    题意:有一个n个整数的排列,这n个整数就是0,1,2,3...n-1这n个数(但不一定按这个顺序给出)。现在先计算一下初始排列的逆序数,然后把第一个元素a1放到an后面去,形成新排列a2a3a4...ana1,然后再求这个排列的逆序数。继续执行类似操作(一共要执行n-1次)直到产生排列ana1a2...an-1为止。......
  • HDU 3743 Frosh Week(树状数组+离散化)
    题意和思路:和POJ2299几乎一样...离散化+树状数组#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<cstdlib>#include<algorithm>#include<vector>#include<map>#include<string>#includ......
  • POJ 2352 HDU1541 Stars(树状数组)
    题意:二维平面给定n个点的坐标,然后要你输出每个点的“等级“。每个点的等级是它的左下放的点个数(包括正下放和正左方的点)。即要你输出对于每个点(x,y)来说,有多少点的坐标(xi,yi)满足xi<=x且yi<=y。思路:题目给出的坐标中已经是按y升序排列,那么其实只用考虑x轴,那么显然就是在前面的......
  • HDU 3277 Marriage Match III(并查集+二分+最大流)
    题意:和HDU3081一样的题意,只不过多了一个条件,每个女孩除了能选自己喜欢的男生之外,还能选不超过K个自己不喜欢的男生,问游戏最多能进行几轮思路:除了选喜欢的,还能选任意K个不喜欢的,怎么建图呢?一开始我想每个女孩连喜欢的男孩,而且选K个不喜欢的男孩也连边,可是这K个要怎么确定呢?这种显然......
  • HDU 3081 Marriage Match II(二分+并查集+最大流)
    题意:有N个女孩要与N个男孩玩配对游戏.每个女孩有一个可选男孩的集合(即该女孩可以选自己集合中的任意一个男孩作为该轮的搭档).然后从第一轮开始,每个女孩都要和一个不同的男孩配对.如果第一轮N个女孩都配对成功,那么就开始第二轮配对,女孩依然从自己的备选男孩集合中选择,但是不能......
  • HDU 1556 Color the ball(树状数组区间更新)
    题意:中文题思路:一道区间更新,单点查询的裸题,用线段树做更好,因为还没看到所以这里用树状数组做。树状数组标记区间的方法很特别,比如给区间[a,b]内的气球涂颜色时,我们add(a,1),add(b+1,-1),单点查询的时候sum(x)就是x这个气球被涂色的总次数。建议先在纸上自己试一下看看,有点抽象,可以这......