# POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴（网络流，最大流）

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

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.

## 代码

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

//这两个分别是把二维坐标转成一维数组地址，和把一维数组地址转成坐标输出
#define pos(x,y) ((x-1)*m+y)
#define inpos(x) '['<<x<<']'<<'('<<(x/m)+(int)(!(x%m==0))<<','<<(x-1)%m+1<<')'

const int maxN=2001;
const int maxM=maxN*maxN*2;
const int inf=2147483647;

class Edge
{
public:
int u,v,flow;
};

int n,m;//即题目中的n*m矩阵
int D;//蜥蜴能跳的最长距离
int Lizards;//蜥蜴数
int cnt;
int Next[maxM];
Edge E[maxM];
int depth[maxN];
int cur[maxN];
int Q[maxN];
char str1[maxN][maxN];//存每个格子能跳过的最多蜥蜴数
char str[maxN];

bool bfs();
int dfs(int u,int flow);

int main()
{
int T;
scanf("%d",&T);
for (int ti=1;ti<=T;ti++)
{
Lizards=0;//多组数据，先初始化
cnt=-1;
scanf("%d%d",&n,&D);
cin>>str1[1];//先读入一个，得出m，方便处理
m=strlen(str1[1]);
for (int i=1;i<=n;i++)
{
for (int j=0;j<m;j++)
if (str1[i][j]!='0')
if (i!=n)
cin>>str1[i+1];
}
for (int i=1;i<=n;i++)
{
cin>>str;
for (int j=0;j<m;j++)
if (str[j]=='L')
{
Lizards++;
}
}
/*
for (int i=1;i<=n*m*2;i++)
{
if (E[j].v!=flow)
cout<<inpos(i)<<"->"<<inpos(E[j].v)<<" "<<E[j].flow<<endl;
}
//*/
for (int i=1;i<=n;i++)//连接每一个格子
for (int j=1;j<=m;j++)
if (str1[i][j-1]!='0')
{
if ((i<=D)||(j<=D)||(n-i+1<=D)||(m-j+1<=D))
{
//continue;
}
for (int x=max(i-D,1);x<=min(i+D,n);x++)//连接能够跳到的格子
for (int y=max(j-D,1);y<=min(j+D,m);y++)
{
if ((x==i)&&(j==y))
continue;
if (str1[x][y-1]=='0')
continue;
if (abs(x-i)+abs(y-j)<=D)//主语是曼哈顿距离
}
}
/*
for (int i=0;i<=n*m*2;i++)
{
if (E[j].flow!=0)
cout<<inpos(i)<<"->"<<inpos(E[j].v)<<" "<<E[j].flow<<endl;
}
//*/
int Ans=0;//求解最大流
while (bfs())
{
for (int i=0;i<=n*m*2+1;i++)
while (int di=dfs(0,inf))
Ans+=di;
}
Ans=Lizards-Ans;//因为要求的是不能跳出去的蜥蜴的个数，所以要用总数减去最大流
printf("Case #%d: ",ti);
if (Ans==0)//注意输出格式，如果是0输出no，如果是1输出单数形式，如果大于1输出复数形式
printf("no lizard was left behind.\n");
else
if (Ans==1)
printf("1 lizard was left behind.\n");
else
printf("%d lizards were left behind.\n",Ans);
//printf("%d\n",Ans);
}
return 0;
}

{
cnt++;
E[cnt].u=u;
E[cnt].v=v;
E[cnt].flow=flow;

cnt++;
E[cnt].u=v;
E[cnt].v=u;
E[cnt].flow=0;
}

bool bfs()
{
memset(depth,-1,sizeof(depth));
int h=1,t=0;
Q[1]=0;
depth[0]=1;
do
{
t++;
int u=Q[t];
{
int v=E[i].v;
if ((depth[v]==-1)&&(E[i].flow>0))
{
depth[v]=depth[u]+1;
h++;
Q[h]=v;
}
}
}
while (t!=h);
if (depth[n*m*2+1]==-1)
return 0;
return 1;
}

int dfs(int u,int flow)
{
if (u==n*m*2+1)
return flow;
{
int v=E[i].v;
if ((depth[v]==depth[u]+1)&&(E[i].flow>0))
{
int di=dfs(v,min(flow,E[i].flow));
if (di>0)
{
E[i].flow-=di;
E[i^1].flow+=di;
return di;
}
}
}
return 0;
}


posted @ 2017-08-18 17:07  SYCstudio  阅读(160)  评论(0编辑  收藏