rt,以下代码
#include <bits/stdc++.h>
#define inf INT_MAX
using namespace std;
inline long long read() {
long long x;bool f;char ch;
for(f=0;!isdigit(ch=getchar());f=ch=='-');
for(x=ch-48;isdigit(ch=getchar());x=x*10+ch-48);
return f?-x:x;
}
inline void print(long long x,char las) {
if(!x) {
putchar(48),putchar(las);
return ;
}
if(x<0) putchar('-'),x=-x;
int ls[20],k=0;
while(x) ls[++k]=x%10,x/=10;
while(k) putchar(ls[k--]+48);
putchar(las);
return ;
}
struct edge {
int to,name;long long lim;
edge *next;
};
struct graph {
int rs;edge rd[160800],*head[402];
inline void add(int u,int v,long long lim) {
rd[rs].to=v;rd[rs].lim=lim;rd[rs].name=rs;rd[rs].next=head[u];head[u]=&rd[rs++];
}
}g1,g2;
int dist[402],cnt[402];
int r=read(),c=read(),d=read(),s=0,t,n=1;
inline void st() {
queue<int>que;que.push(t);
for(int i=0;i<=r*c;i++) dist[i]=-1;
cnt[0]=1;
while(!que.empty()) {
int now=que.front();que.pop();
for(edge *i=g2.head[now];i;i=i->next) {
int nex=i->to;
if(dist[nex]==-1) dist[nex]=dist[now]+1,que.push(nex),cnt[dist[nex]]++;
}
}
return ;
}
long long ans=0;
inline long long ISAP(int x,long long lim) {
if(x==t) {
ans+=lim;
return lim;
}
long long used=0;
for(edge *i=g1.head[x];i;i=i->next) {
int nex=i->to;
if(i->lim && dist[x]==dist[nex]+1) {
long long cost=ISAP(nex,min(i->lim,lim-used));
if(cost) {
i->lim-=cost;
g2.rd[i->name].lim+=cost;
used+=cost;
if(used==lim) return used;
}
}
}
for(edge *i=g2.head[x];i;i=i->next) {
int nex=i->to;
if(i->lim && dist[x]==dist[nex]+1) {
long long cost=ISAP(nex,min(i->lim,lim-used));
if(cost) {
i->lim-=cost;
g1.rd[i->name].lim+=cost;
used+=cost;
if(used==lim) return used;
}
}
}
cnt[dist[x]]--;
if(!cnt[dist[x]]) dist[s]=n+1;
dist[x]++;
cnt[dist[x]]++;
return used;
}
char stone[21][21];
bitset<21>out[21];int cont;
int main() {
n=r*c+2;
t=r*c+1;
int dd=d*d;
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++) {
cin>>stone[i][j];
if(stone[i][j]!='0') if(i<=d || j<=d || r-i<d || c-j<d) {
g1.add(c*i-c+j,t,stone[i][j]-'0');g2.add(t,c*i-c+j,0);
out[i][j]=true;
continue;
}
}
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++) {
if(stone[i][j]!='0' && !out[i][j]) {
for(int ii=max(1,i-d);ii<=min(r,i+d);ii++) {
for(int jj=max(1,j-d);jj<=min(c,j+d);jj++) {
if(dd<abs(i-ii)*abs(j-jj)) continue;
if(out[ii][jj]) g1.add(c*i-c+j,c*ii-c+jj,stone[i][j]-'0');g2.add(c*ii-c+jj,c*i-c+j,0);
}
}
}
}
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++) {
char k;
cin>>k;
if(k=='L') {
cont++;
g1.add(0,i*c-c+j,1);g2.add(i*c-c+j,0,0);
}
}
st();
while(dist[s]<n) ISAP(s,LLONG_MAX);
print(cont-ans,'\n');
return 0;
}