Problem 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.
 
题意:给你一个网格,网格上的一些位置上有一只蜥蜴,所有蜥蜴的最大跳跃距离是d
如果一只蜥蜴能跳出网格边缘,那么它就安全了.且每个网格有一个最大跳出次数x
即最多有x只蜥蜴从这个网格跳出,这个网格就再也不能有蜥蜴进来了.问你最少有多少只蜥蜴跳不出网格.

建图:

源点S编号0,网格的每个格子分成两个点i和i+n*m(n和m为网格的行和列数,其实i编号点是表示蜥蜴进来,而i+n*m编号的点是表示蜥蜴出去).汇点t编号n*m*2+1.

如果格子i上有蜥蜴,那么从s到i有边(s,i,1).

如果格子i能承受x次跳出,那么有边(i,i+n*m,x)

如果从格子i能直接跳出网格边界,那么有边(i+n*m,t,INF)

如果从格子i不能直接跳出网格,那么从i到离i距离<=d的网格j有边(i+n*m,j,INF). 注意这里的距离是abs(行号之差)+abs(列号之差)

最终我们求出的最大流就是能跳出网格的蜥蜴数.

 
网络流的拆点操作是为了解决有些题目要求每个点的选择次数是有限制的.
比如 id 这个点的被选择次数最大是3,那么我们把它拆成id1 跟 id2两个点分别来表示入id和出id
再在id1和id2之间建一条容量为3的边,这样每次进入id的时候我们就可以让它其实进入id1然后原地跳一下跳到id2这样就控制了某个点的选择的次数
 #include <bits/stdc++.h>

 using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
char s[][];
char ss[][];
struct Edge
{
int from,to,cap,flow;
Edge (){}
Edge (int f,int t,int c,int fl){from=f,to=t,cap=c,flow=fl;}
};
struct Dinic
{
int n,m,s,t;
vector <Edge> edges;
vector <int> G[maxn];
int cur[maxn];
int dep[maxn];
bool vis[maxn];
void init (int n,int s,int t)
{
this->n=n;this->s=s;this->t=t;
edges.clear();
for (int i=;i<n;++i)
G[i].clear();
}
void addedge (int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool bfs ()
{
queue <int> q;
while (!q.empty()) q.pop();
memset(vis,false,sizeof vis);
vis[s] = true;
dep[s] = ;
q.push(s);
while (!q.empty()){
int u = q.front();
q.pop();
for (int i=;i<G[u].size();++i){
Edge e = edges[G[u][i]];
int v = e.to;
if(!vis[v]&&e.cap>e.flow){
vis[v] = true;
dep[v] = dep[u] + ;
q.push(v);
}
}
}
return vis[t];
}
int dfs (int x,int mi){
if (x==t||mi==) return mi;
int flow = ,f;
for (int &i=cur[x];i<G[x].size();++i){
Edge &e = edges[G[x][i]];
int y = e.to;
if (dep[y]==dep[x]+&&(f=dfs(y,min(mi,e.cap-e.flow)))>){
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
mi-=f;
if (mi==) break;
}
}
return flow;
}
int max_flow ()
{
int ans = ;
while (bfs()){
memset(cur,,sizeof cur);
ans+=dfs(s,inf);
}
return ans;
}
}dinic;
int full_flow;
bool check (int x,int y,int i,int j,int d)
{
if (abs(x-i)+abs(y-j)<=d) return true;
else return false;
}
bool out (int x,int y,int d)
{
if (x-d) return true;
else return false;
}
int main()
{
//freopen("de.txt","r",stdin);
int casee = ;
int T;
scanf("%d",&T);
while (T--){
int n,m,src,dst,d;
full_flow = ;
scanf("%d%d",&n,&d);
for (int i=;i<=n;++i)
scanf("%s",s[i]+);
int len = strlen(s[]+);
m = len;
src = ; dst = *n*m+;
dinic.init(*n*m+,src,dst);
for (int i=;i<=n;++i){
for (int j=;j<=len;++j){
if (s[i][j]-''>){
int id = (i-)*m+j;
dinic.addedge(id,id+n*m,s[i][j]-'');
if (i<=d || i+d>n || j<=d || j+d>m){//这个点能直接跳出去
dinic.addedge(id+n*m,dst,inf);
}
else{
for (int x=;x<=n;++x){
for (int y=;y<=m;++y){
if (x==i&&y==j) continue;
if (check(x,y,i,j,d)){
int id2 = (x-)*m+y;
dinic.addedge(id+n*m,id2,inf);//这个点的出连向能到达点的入
//dinic.addedge(id2+n*m,id,inf);
}
}
}
}
} }
}
for (int i=;i<=n;++i){
scanf("%s",ss[i]+);
for (int j=;j<=len;++j){
if (ss[i][j]=='L'){
full_flow++;
int id = (i-)*m+j;
dinic.addedge(src,id,);
}
}
}
int ans = full_flow-dinic.max_flow();
if(ans==) printf("Case #%d: no lizard was left behind.\n",++casee);
else if(ans==) printf("Case #%d: 1 lizard was left behind.\n",++casee);
else printf("Case #%d: %d lizards were left behind.\n",++casee,ans);
}
return ;
}
 

最新文章

  1. C# 索引器,实现IEnumerable接口的GetEnumerator()方法
  2. slf4j的简单介绍
  3. angularjs 表单验证(不完整版)
  4. X200s,Debian 8(Jessie) 安装流水帐
  5. 【spring】 &lt;tx:annotation-driven /&gt; 的理解 【转载的】
  6. 2600: [Ioi2011]ricehubh
  7. PostgreSQL应用相关问题解决
  8. [转]Asp.net三种事务处理
  9. Swift 本地推送通知UILocalNotification
  10. Linux命令:tail命令详解
  11. OSA-MAC: A MAC Protocol for Opportunistic Spectrum Access in Cognitive Radio Networks
  12. EntityFramework Core饥饿加载忽略导航属性问题
  13. 代码之间-论文修改助手v1.0版本发布
  14. C++一个类对象的大小计算
  15. RIDE创建工程和测试套件和用例--书本介绍的入门方法,自己整理实践下
  16. 【谈谈IO】BIO、NIO和AIO
  17. 怎么让table中的&lt;td&gt;内容向上对齐
  18. maven中修改可用的版本
  19. 设置Eclipse具有字母自动联想
  20. 防雪崩利器:熔断器 Hystrix 的原理与使用

热门文章

  1. 建站手册-template
  2. Integer自动装箱和拆箱
  3. linux crontab 执行任务(7秒执行)
  4. linux + eclipse + cdt 报错undefined reference......好麻烦的,这位大牛给出的方法可行,特此MARK!!!!
  5. oracle三大范式
  6. Linux下查看日志文件
  7. 如何写一个简单的基于 Qt 框架的 HttpServer ?
  8. 6个常用Java 源代码 保护工具(混淆、加密、底层)
  9. 如何在linux命令行无界面下使用selenium
  10. centos7配置sudo免密