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.
题意:在网格内有若干只蜥蜴,他们每次可以跳k长度的网格,每个格子在被一只蜥蜴踩过之后就会崩塌,问有多少蜥蜴不能离开网格。
每个格子向它能跳到的格子建边,外圈格子向超级汇点建边,通过将网格拆成入点和出点限制通过的蜥蜴数量,直接跑网络流就可以了。
1 #include<stdio.h>
2 #include<string.h>
3 #include<vector>
4 #include<queue>
5 #include<algorithm>
6 using namespace std;
7 const int maxm=1000;
8 const int INF=0x7fffffff;
9
10 struct edge{
11 int from,to,f;
12 edge(int a,int b,int c):from(a),to(b),f(c){}
13 };
14
15 struct dinic{
16 int s,t,m;
17 vector<edge>e;
18 vector<int>g[maxm];
19 bool vis[maxm];
20 int cur[maxm],d[maxm];
21
22 void init(int n){
23 for(int i=1;i<=n;i++)g[i].clear();
24 e.clear();
25 }
26
27 void add(int a,int b,int c){
28 e.push_back(edge(a,b,c));
29 e.push_back(edge(b,a,0));
30 m=e.size();
31 g[a].push_back(m-2);
32 g[b].push_back(m-1);
33 }
34
35 bool bfs(){
36 memset(vis,0,sizeof(vis));
37 queue<int>q;
38 q.push(s);
39 vis[s]=1;
40 d[s]=0;
41 while(!q.empty()){
42 int u=q.front();
43 q.pop();
44 for(int i=0;i<g[u].size();i++){
45 edge tmp=e[g[u][i]];
46 if(!vis[tmp.to]&&tmp.f>0){
47 d[tmp.to]=d[u]+1;
48 vis[tmp.to]=1;
49 q.push(tmp.to);
50 }
51 }
52 }
53 return vis[t];
54 }
55
56 int dfs(int x,int a){
57 if(x==t||a==0)return a;
58 int flow=0,f;
59 for(int& i=cur[x];i<g[x].size();i++){
60 edge& tmp=e[g[x][i]];
61 if(d[tmp.to]==d[x]+1&&tmp.f>0){
62 f=dfs(tmp.to,min(a,tmp.f));
63 tmp.f-=f;
64 e[g[x][i]^1].f+=f;
65 flow+=f;
66 a-=f;
67 if(a==0)break;
68 }
69 }
70 if(flow==0)d[x]=-1;
71 return flow;
72 }
73
74 int mf(int s,int t){
75 this->s=s;
76 this->t=t;
77 int flow=0;
78 while(bfs()){
79 memset(cur,0,sizeof(cur));
80 flow+=dfs(s,INF);
81 }
82 return flow;
83 }
84 };
85
86 char a[25][25],b[25][25];
87 struct point{
88 int x,y,n;
89 }p[500];
90
91 int aabs(int a){
92 return a>=0?a:-a;
93 }
94
95 int main(){
96 int T;
97 scanf("%d",&T);
98 for(int q=1;q<=T;q++){
99 int i,j;
100 int n,m,k;
101 scanf("%d%d",&n,&k);
102 for(i=1;i<=n;i++){
103 scanf("%s",a[i]+1);
104 }
105 for(i=1;i<=n;i++){
106 scanf("%s",b[i]+1);
107 }
108 m=strlen(a[1]+1);
109 dinic d;
110 d.init(n*m*2+10);
111 int cnt=0,l,c=0;
112 for(i=1;i<=n;i++){
113 for(j=1;j<=m;j++){
114 if(a[i][j]!='0'){
115 int tmp=(i-1)*m+j;
116 d.add(tmp,tmp+m*n,a[i][j]-'0');
117 for(l=1;l<=cnt;l++){
118 if(aabs(i-p[l].x)+aabs(j-p[l].y)<=k){
119 d.add(tmp+m*n,p[l].n,INF);
120 d.add(p[l].n+n*m,tmp,INF);
121 }
122 }
123 cnt++;
124 p[cnt].x=i;
125 p[cnt].y=j;
126 p[cnt].n=tmp;
127 if(b[i][j]=='L'){
128 c++;
129 d.add(0,tmp,1);
130 }
131 if(i<=k||j<=k||n-i+1<=k||m-j+1<=k)d.add(tmp+n*m,n*m*2+1,INF);
132 }
133 }
134 }
135 int ans=c-d.mf(0,2*n*m+1);
136 printf("Case #%d: ",q);
137 if(ans)printf("%d",ans);
138 else printf("no");
139 if(ans<=1)printf(" lizard was left behind.
");
140 else printf(" lizards were left behind.
");
141 }
142 return 0;
143 }