题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1066
简单的网络流:
构图:每个各自拆成两个点,一个入一个出,它们之间的容量是这个地方能跳的次数。然后能够互相到达的就连边,容量是inf,源点到那些一步能跳出去的格子的“入点”连边,容量是inf,那些有蜥蜴的格子的“出点”连到汇点,容量是1。因为如果有一个流量经过一个有蜥蜴的格子,那么它一定会流向汇点,这样就能互不影响,所以两个蜥蜴不能同时在一个格子上也能满足。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <cassert>
#include <set>
#include <map>
#include <cmath>
#include <ctime>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,a,b) for(int i=(a);i>=(b);--i)
#define clr(a,x) memset(a,(x),sizeof(a));
#define eps 1e-8
#define LL long long
#define mp make_pair
const int maxn=25*25*2+5;
const int inf=1e8;
struct Edge
{
int u,v,cap,flow;
Edge(int u,int v,int cap,int flow)
:u(u),v(v),cap(cap),flow(flow) { }
};
vector<Edge> edges;
vector<int> G[maxn];
void add(int u,int v,int c)
{
edges.push_back(Edge(u,v,c,0));
edges.push_back(Edge(v,u,0,0));
int m=edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
struct Dinic
{
int d[maxn],cur[maxn];
int q[maxn],front,rear;
int n,s,t;
void init(int _n) { n=_n; }
bool bfs()
{
clr(d,0x3f); clr(cur,0);
d[s]=0; front=rear=0; q[rear++]=s;
while(front<rear) {
int u=q[front++];
rep(i,0,G[u].size()) {
Edge&e=edges[G[u][i]];
if(e.cap<=e.flow||d[e.v]<=d[u]+1) continue;
d[e.v]=d[u]+1;
q[rear++]=e.v;
}
}
return d[t]<n;
}
int dfs(int u,int a)
{
if(u==t||a==0) return a;
if(d[u]>=d[t]) return 0;
int flow=0,f;
for(int&i=cur[u];i<G[u].size();++i) {
Edge&e=edges[G[u][i]];
if(d[e.v]==d[u]+1&&(f=dfs(e.v,min(a,e.cap-e.flow)))>0) {
e.flow += f;
edges[G[u][i]^1].flow-=f;
flow += f;
a -= f;
if(a==0) break;
}
}
return flow;
}
int maxflow(int s,int t)
{
this->s=s; this->t=t;
int flow=0;
while(bfs()) flow+=dfs(s,inf);
return flow;
}
}solver;
int R,C,D;
char Map[25][25];
inline int getx(int r,int c) { return r*C+c; }
inline int row(int x) { return x/C; }
inline int col(int x) { return x%C; }
inline int in(int x) { return 2*x+1; }
inline int out(int x) { return 2*x+2; }
int s,t,tot;
inline int sqr(int x) { return x*x; }
void input()
{
rep(i,0,R) scanf("%s",Map[i]);
edges.clear(); rep(i,0,maxn) G[i].clear();
s=0; t=R*C*2+1; solver.init(t+1);
rep(i,0,R) {
rep(j,0,C) {
int x=getx(i,j);
if(Map[i][j]>'0') add(in(x),out(x),Map[i][j]-'0');
if((i<D||j<D||i>=R-D||j>=C-D)&&(Map[i][j]>'0')) {
add(s,in(x),inf);
}
}
}
rep(a,0,R) rep(b,0,C) {
if(Map[a][b]=='0') continue;
int x=getx(a,b);
rep(c,0,R) rep(d,0,C)
{
if(Map[c][d]=='0') continue;
if(sqr(c-a)+sqr(d-b)<=D*D) {
int y=getx(c,d);
add(out(x),in(y),inf);
}
}
}
tot=0;
rep(i,0,R) {
scanf("%s",Map[R]);
rep(j,0,C) if(Map[R][j]=='L') {
int x=getx(i,j);
add(out(x),t,1);
++tot;
}
}
}
void solve()
{
solver.init(t+1);
printf("%d\n",tot-solver.maxflow(s,t));
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d%d",&R,&C,&D)==3) {
input();
solve();
}
}