Escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 16    Accepted Submission(s): 12

Problem Description
Given a maze of size n×m. The rows are numbered 1, 2, · · · , n from top to bottom while the columns are numbered 1, 2, · · · , m from left to right, which means that (1, 1) is the top-left corner and that (n, m) is the bottom-right corner. And for each cell of size 1 × 1, it is either blank or blocked.
There are a robots above the maze. For i-th robot, it is initially positioned exactly above the cell (1, pi), which can be described as (0, pi). And the initial moving direction of the robots are all downward, which can be written as (1, 0) in the vector form.
Also, there are b exits below the maze. For i-th exit, it is positioned exactly below the cell (n, ei), which can be described as (n + 1, ei).
Now, you want to let the robots escape from the maze by reaching one of the exits. However, the robots are only able to go straight along their moving directions and can’t make a turn. So you should set some turning devices on some blank cells in the maze to help the robots make turns.
There are 4 types of turning devices:

  • “NE-devices” : make the robots coming from above go rightward, and make the robots coming from right go upward. Coming from left or below is illegal.
  • “NW-devices” : make the robots coming from above go leftward, and make the robots coming from left go upward. Coming from right or below is illegal.
  • “SE-devices” : make the robots coming from below go rightward, and make the robots coming from right go downward. Coming from left or above is illegal.
  • “SW-devices” : make the robots coming from below go leftward, and make the robots coming from left go downward. Coming from right or above is illegal.

For each cell, the number of turning devices on it can not exceed 1. And collisions between the robots are ignored, which allows multiple robots to visit one same cell even at the same time.
You want to know if there exists some schemes to set turning devices so that all the a robots can reach one of the b exits after making a finite number of moves without passing a blocked cell or passing a turning device illegally or going out of boundary(except the initial position and the exit).
If the answer is yes, print “Yes” in a single line, or print “No” if the answer is no.

 
Input
The first line contains one positive integer T (1 ≤ T ≤ 10), denoting the number of test cases.
For each test case:
The first line contains four positive integers n, m, a, b (1 ≤ n, m ≤ 100, 1 ≤ a, b ≤ m), denoting the number of rows and the number of columns in the maze, the number of robots and the number of exits respectively.
Next n lines each contains a string of length m containing only “0” or “1”, denoting the initial maze, where cell (i, j) is blank if the j-th character in i-th string is “0”, while cell (i, j) is blocked if the j-th character in i-th string is “1”.
The next line contains a integers pi (1 ≤ pi ≤ m), denoting the initial positions (0, pi) of the robots.
The next line contains b integers ei (1 ≤ ei ≤ m), denoting the positions (n + 1, ei) of the exits.
It is guaranteed that all pis are pairwise distinct and that all eis are also pairwise distinct.
 
Output
Output T lines each contains a string “Yes” or “No”, denoting the answer to corresponding test case.
 
Sample Input
2
3 4 2 2
0000
0011
0000
1 4
2 4
3 4 2 2
0000
0011
0000
3 4
2 4
 
Sample Output
Yes
No

Hint

 
Source

题解:

每个格子的水平方向和竖直方向都只能被使用一次,因为两个机器人的路径不可能合并,也不可能迎面相撞。如果一个格子没有放转弯装置,则可以被水平穿过一次,竖直穿过一次。如果一个格子放了转弯装置,则这个格子只能被一个机器人经过一次。所以对于所有非障碍格子,可以拆成水平点和竖直点,每个点限流 1,上下相邻的格子连竖直点(竖直直行),左右相邻的格子连水平点(水平直行),格子内部的水平点和竖直点互相相连(转弯),源连向起点的竖直点,出口的竖直点连向汇,跑最大流,如果最大流 = 机器人个数,则输出 Yes,否则输出 No。
 
参考代码:
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=,M=;
int T,n,m,a,b,h[N],s,t,base;
char g[][];
int head[N],nex[M],w[M],to[M],tot;
inline void ade(int a,int b,int c)
{
to[++tot]=b;
nex[tot]=head[a];
w[tot]=c;
head[a]=tot;
}
inline void add(int a,int b,int c)
{
ade(a,b,c);
ade(b,a,);
}
inline int id(int x,int y){return m*x+y;}
inline int bfs()
{
memset(h,,sizeof h);
h[s]=;
queue<int> q; q.push(s);
while(q.size())
{
int u=q.front(); q.pop();
for(int i=head[u];i;i=nex[i])
{
if(!h[to[i]]&&w[i])
{
h[to[i]]=h[u]+;
q.push(to[i]);
}
}
}
return h[t];
}
int dfs(int x,int f)
{
if(x==t) return f;
int fl=;
for(int i=head[x];i&&f;i=nex[i])
{
if(h[to[i]]==h[x]+&&w[i])
{
int mi=dfs(to[i],min(w[i],f));
w[i]-=mi; w[i^]+=mi; fl+=mi; f-=mi;
}
}
if(!fl) h[x]=-;
return fl;
}
int dinic()
{
int res=;
while(bfs()) res+=dfs(s,inf);
return res;
}
signed main()
{
cin>>T;
while(T--)
{
tot=;
memset(head,,sizeof head);
cin>>n>>m>>a>>b;
base=(n+)*m; t=base*;
for(int i=;i<=n;i++) scanf("%s",g[i]+);
for(int i=;i<=a;i++)
{
int x; scanf("%d",&x); g[][x]='';
add(s,id(,x),); add(id(,x),id(,x),);
}
for(int i=;i<=b;i++)
{
int x; scanf("%d",&x);
g[n+][x]='';
add(id(n+,x),t,inf);
add(id(n,x),id(n+,x),inf);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(g[i][j]=='') continue;
if(i>) add(id(i,j),id(i-,j),);
if(i<n) add(id(i,j),id(i+,j),);
if(j>) add(id(i,j)+base,id(i,j-)+base,);
if(j<m) add(id(i,j)+base,id(i,j+)+base,);
add(id(i,j),id(i,j)+base,);add(id(i,j)+base,id(i,j),);
}
}
puts(dinic()==a?"Yes":"No");
}
return ;
}

最新文章

  1. SQL分组多列统计(GROUP BY后按条件分列统计)
  2. 日本超人气洛比(Robi)声控机器人
  3. 跟随标准与Webkit源码探究DOM -- 获取元素之getElementsByTagName
  4. MySQL中的FEDERATED引擎
  5. Silverlight自定义控件开发:仪表盘
  6. PTF 安装及简单测试 Packet Testing Framework
  7. bind: address already in use
  8. 5 echo展开
  9. SQL2008-功能设置
  10. OpenJDK 8 on Windows
  11. (转)java多线程的一篇好文
  12. 采用Java语言如何实现高速文件复制?
  13. Qt之Windows开发移植问题汇总
  14. Angular - - $location 和 $window
  15. 如何设置secureCRT的鼠标右键为弹出文本操作菜单功能
  16. node.js平台下的mysql数据库配置及连接
  17. Java Applet 与Servlet之间的通信
  18. vue input输入框长度限制
  19. 用归并排序或树状数组求逆序对数量 poj2299
  20. MySQL学习----索引的使用

热门文章

  1. golang 服务诡异499、504网络故障排查
  2. 监听器以及在监听类里面获得bean的方法
  3. 2019CSP day1t1 格雷码
  4. [LC]747题 Largest Number At Least Twice of Others (至少是其他数字两倍的最大数)
  5. 有关html的标签以及css属性(border、div)
  6. 力扣(LeetCode)两整数之和 个人题解
  7. Google Chrome浏览器的编码格式的修改步骤
  8. Java关于Resource leak: &#39;s&#39; is never closed的问题
  9. 像黑客一样写博客–Pelican快速搭建静态博客
  10. SpringBoot学习(七)—— springboot快速整合Redis