Leapin' Lizards
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 1166 |
|
Accepted: 447 |
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.
Hint
Brute force methods examining every path will likely exceed the allotted time limit.
Source
Mid-Central USA 2005
/* 诡异的输出要求。。。。 诡异的数据越界,D>=4 还有就是意外在HDU上刷到了第一。。。。 */ #include<cstdio> #include<cstring> const int N=1010; const int M=100010; const int inf=0x7fffffff; int head[N]; int map[25][25]; bool mat[25][25]; int cou[25][25]; char str[30]; int len; int dx[]={0,0,-1,1,0,0,-1,-1,1,1,-2,2,0,0,-1,-1,-2,-2,-3,1,1,2,2,3, 0,0,-1,-1,-2,-2,-3,-3,-4,1,1,2,2,3,3,4};//0->3 4->11,12->23 int dy[]={1,-1,0,0,-2,2,-1,1,-1,1,0,0,-3,3,-2,2,-1,1,0,-2,2,-1,1,0, -4,4,-3,3,-2,2,-1,1,0,3,-3,2,-2,1,-1,0}; struct Edge { int v,next,w; } edge[M]; int cnt,n,s,t; void addedge(int u,int v,int w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++; } int sap() { int pre[N],cur[N],dis[N],gap[N]; int flow=0,aug=inf,u; bool flag; for(int i=0; i<n; i++) { cur[i]=head[i]; gap[i]=dis[i]=0; } gap[s]=n; u=pre[s]=s; while(dis[s]<n) { flag=0; for(int &j=cur[u]; j!=-1; j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[u]==dis[v]+1) { flag=1; if(edge[j].w<aug) aug=edge[j].w; pre[v]=u; u=v; if(u==t) { flow+=aug; while(u!=s) { u=pre[u]; edge[cur[u]].w-=aug; edge[cur[u]^1].w+=aug; } aug=inf; } break; } } if(flag) continue; int mindis=n; for(int j=head[u]; j!=-1; j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[v]<mindis) { mindis=dis[v]; cur[u]=j; } } if((--gap[dis[u]])==0) break; gap[dis[u]=mindis+1]++; u=pre[u]; } return flow; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T,d; scanf("%d",&T); int cas=1; while(T--) { cnt=0; s=0; memset(head,-1,sizeof(head)); memset(mat,0,sizeof(mat));//no map memset(cou,0,sizeof(cou)); scanf("%d%d",&n,&d); for(int i=0;i<n;i++) { scanf("%s",str); len=strlen(str); for(int j=0;j<len;j++) map[i][j]=str[j]-'0'; } for(int i=0;i<n;i++) { scanf("%s",str); for(int j=0;j<len;j++) if(str[j]=='L') mat[i][j]=1; } int num=0; for(int i=0;i<n;i++) for(int j=0;j<len;j++) if(map[i][j]!=0) cou[i][j]=++num;//对有柱子的才编号 int sum=0; t=2*num+1; for(int i=0;i<n;i++) for(int j=0;j<len;j++) if(map[i][j]!=0) { if(mat[i][j]==1) { sum++;//记录L总数 addedge(s,cou[i][j],1);//有L的与源点连边 } if(i<d) addedge(cou[i][j]+num,t,inf);//能跳出去的与汇点相连 else if(n-1-i<d) addedge(cou[i][j]+num,t,inf); else if(j<d) addedge(cou[i][j]+num,t,inf); else if(len-1-j<d) addedge(cou[i][j]+num,t,inf); } for(int i=0;i<n;i++) for(int j=0;j<len;j++) if(map[i][j]!=0) { addedge(cou[i][j],cou[i][j]+num,map[i][j]); } int end; if(d==1) end=3; else if(d==2) end=11; else if(d==3) end=23; else end=39; for(int i=0;i<n;i++) for(int j=0;j<len;j++) if(map[i][j]!=0) { int x,y; for(int k=0;k<=end;k++) { x=i+dx[k]; y=j+dy[k]; if(x>=0&&x<n&&y>=0&&y<len&&map[x][y]!=0) { addedge(cou[i][j]+num,cou[x][y],inf); } } } n=t+1; int t=sum-sap(); if(t>1)//输出太阴了 printf("Case #%d: %d lizards were left behind./n",cas++,t); else if(t==0) printf("Case #%d: no lizard was left behind./n",cas++); else printf("Case #%d: 1 lizard was left behind./n",cas++); } return 0; }