2014
02-14

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

#include<stdio.h>
#include<string.h>
#define N 1000
#define inf 0x3fffffff
int G[25][25],start=801,end=802;
struct edge
{
int st,ed,flow,next;
}E[N*20];
{
}
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;
if(out(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))
}
}
int dfs(int u,int minflow)
{
if(u==end)return minflow;
int i,v,f,min_dis=ans-1,flow=0;
{
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);
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++;
}
}
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;
}

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