首页 > ACM题库 > HDU-杭电 > hdu 2732 Leapin’ Lizards-DFS-[解题报告]C++
2014
02-14

hdu 2732 Leapin’ Lizards-DFS-[解题报告]C++

Leapin’ Lizards

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

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.

非常好的一道拆点题目,终于学会isap了,递归的,这几天要把非递归实现的敲出来,,

题意:给出蜥蜴可以跳的最大距离,从一根柱子跳向可以到达的任意柱子,每个柱子有高度,蜥蜴没跳一次柱子会下降1米,问有多少蜥蜴跳不出所给的图。

建图:源点与每个蜥蜴所在的柱子建边流量为1,应为柱子间可以互相达到的,所以每个柱子应该拆成两个点,每根柱子相当于一条边,流量为柱子的高度,没两个可以互相到达的柱子间建边(一根柱子的终点到另一根柱子的起点)流量为无穷大,然后每根柱子的终点跟汇点建边流量为柱子的高度。







#include<stdio.h>
#include<string.h>
#define N 1000
#define inf 0x3fffffff
int dis[N],gap[N],head[N],num,n,m,D,ans;
int G[25][25],start=801,end=802;
struct edge
{
	int st,ed,flow,next;
}E[N*20];
void addedge(int x,int y,int w)
{
	E[num].st=x;E[num].ed=y;E[num].flow=w;E[num].next=head[x];head[x]=num++;
	E[num].st=y;E[num].ed=x;E[num].flow=0;E[num].next=head[y];head[y]=num++;
}
int abs(int a)
{
	if(a>0)return a;
	return -a;
}
bool judge(int x, int y)
{
    if (x < 0 || x >= n || y < 0 || y >= m)
	{
        return false;
    }
    else if (!G[x][y])
	{
        return false;
    }
    return true;
}
bool out(int x,int y)//判断该柱子上的蜥蜴是否能跳出去
{
	if(x-D<0||x+D>=n||y-D<0||y+D>=m)
		return true;
	return false;
}
void makemap(int x,int y)
{
	int nn=x*m+y;
	addedge(nn,nn+400,G[x][y]);//柱子拆点
	if(out(x,y))//如果可以跳出就不跳到别的柱子上了
	{
		addedge(nn+400,end,G[x][y]);
		return ;
	}
	for (int i = -D; i <= D; ++i) 
		for (int j = -(D-abs(i)); j <= (D-abs(i)); ++j)
		{
			int xx = x + i, yy = y + j;
			if (judge(xx, yy) && !(x == xx && y == yy))
				addedge(nn+400, xx*m+yy, inf);
		}
}
int dfs(int u,int minflow)
{
	if(u==end)return minflow;
	int i,v,f,min_dis=ans-1,flow=0;
	for(i=head[u];i!=-1;i=E[i].next)
	{
		if(E[i].flow)
		{
			v=E[i].ed;
			if(dis[u]==dis[v]+1)
			{
				f=dfs(v,E[i].flow>minflow-flow?minflow-flow:E[i].flow);
				E[i].flow-=f;
				E[i^1].flow+=f;
				flow+=f;
				if(flow==minflow)break;
				if(dis[start]>=ans)
					return flow;
			}
			min_dis=min_dis>dis[v]?dis[v]:min_dis;
		}
	}
	if(flow==0)
	{
		if(--gap[dis[u]]==0)
			dis[start]=ans;
		dis[u]=min_dis+1;
		gap[dis[u]]++;
	}
	return flow;
}
int isap()
{
	int maxflow=0;
	memset(dis,0,sizeof(dis));
	memset(gap,0,sizeof(gap));
	gap[0]=ans;//点的个数
	while(dis[start]<ans)
       maxflow+=dfs(start,inf);
	return maxflow;
}
int main()
{
	int i,j,sum,t,op=1;
	char str[25];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&D);
		memset(head,-1,sizeof(head));
		num=0;ans=2;sum=0;
		for(i=0;i<n;i++)
		{
			scanf("%s",str);
			for(j=0;str[j];j++)
				G[i][j]=str[j]-'0';
		}
		m=strlen(str);
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				if(G[i][j]>0)
				{
					makemap(i,j);
					ans+=2;
				}
			}
			for(i=0;i<n;i++)
			{
				scanf("%s",str);
				for(j=0;j<m;j++)
					if(str[j]=='L')
					{
						sum++;
						addedge(start,i*m+j,1);
					}
			}
			sum-=isap();
			printf("Case #%d: ",op++);
			if(sum==0)//输出还得考虑单复数问题,
				puts("no lizard was left behind.");
			else if(sum==1)
				printf("%d lizard was left behind.\n", sum);
			else
				printf("%d lizards were left behind.\n", sum);
	}
	return 0;
}








解题转自:http://blog.csdn.net/aixiaoling1314/article/details/9882185


  1. 5.1处,反了;“上一个操作符的优先级比操作符ch的优先级大,或栈是空的就入栈。”如代码所述,应为“上一个操作符的优先级比操作符ch的优先级小,或栈是空的就入栈。”

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。