将每个不是障碍的格子标号,设三只狼的位置分别为$A,B,C$,羊的位置在$D$。合法状态中强行限制$A<B<C$,这样状态数只有$\frac{n^8}{6}\approx 1.6\times 10^7$。

设$f[S],g[S]$表示目前局面是$S$,狼/羊正在决策,最优情况下羊会不会被围住。

  • 若$f[S]$的某个后继$g[T]$是真,那么$f[S]$也是真。
  • 若$g[S]$的所有后继$f[T]$都是真,那么$g[S]$也是真。

先认为所有$f[S]$和$g[S]$都是假,并记录每个$g[S]$的后继个数$deg[S]$。

对于羊被围住的状态,将它们的$g$设为真,加入队列。

每次从队列中取出一个状态:

  • 若是$g[S]$,则枚举$g[S]$的所有前驱$f[T]$,将它们更新为真,并加入队列。
  • 若是$f[S]$,则枚举$f[S]$的所有前驱$g[T]$,将$deg[T]$减一,若$deg[T]$变为了$0$则将$g[T]$更新为真,并加入队列。

时间复杂度$O(n^8)$。

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=10,M=161701;
int n,m,all,tot,i,j,k,x,y,z,A,B,S;bool can[N*N],safe[N*N];char a[N][N+5];
int id[N*N][N*N][N*N],loc[M][3];
int tr[N*N][4];
bool f[M][N*N],g[M][N*N];
char deg[M][N*N];
int q[M*N*N*2],head,tail;
inline int getid(int x,int y){return x*m+y;}
inline void extf(int A,int B,int C,int y){
if(A>B)swap(A,B);
if(B>C)swap(B,C);
if(A>B)swap(A,B);
int x=id[A][B][C];
if(f[x][y])return;
f[x][y]=1;
q[++tail]=(x<<8)|(y<<1);
}
inline void extg(int x,int y){
g[x][y]=1;
q[++tail]=(x<<8)|(y<<1)|1;
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(i=0;i<n;i++)scanf("%s",a[i]);
all=n*m;
for(i=0;i<all;i++)can[i]=safe[i]=0;
for(i=0;i<n;i++)for(j=0;j<m;j++){
if(a[i][j]!='X')can[getid(i,j)]=1;
if(i==0||i==n-1||j==0||j==m-1)safe[getid(i,j)]=1;
}
for(i=0;i<n;i++)for(j=0;j<m;j++)for(k=0;k<4;k++){
tr[getid(i,j)][k]=-1;
int nx=i,ny=j;
if(k==0)nx--;
if(k==1)nx++;
if(k==2)ny--;
if(k==3)ny++;
if(nx<0||nx>=n||ny<0||ny>=m)continue;
if(a[i][j]!='X'&&a[nx][ny]!='X')tr[getid(i,j)][k]=getid(nx,ny);
}
tot=0;
for(i=0;i<all;i++)if(can[i])for(j=i+1;j<all;j++)if(can[j])for(k=j+1;k<all;k++)if(can[k]){
id[i][j][k]=tot;
loc[tot][0]=i;
loc[tot][1]=j;
loc[tot][2]=k;
tot++;
}
for(i=0;i<tot;i++)for(j=0;j<all;j++)f[i][j]=g[i][j]=deg[i][j]=0;
head=1,tail=0;
for(i=0;i<tot;i++){
x=loc[i][0],y=loc[i][1],z=loc[i][2];
for(j=0;j<all;j++)if(can[j]){
if(j==x||j==y||j==z)continue;
if(safe[j]){deg[i][j]++;continue;}
for(k=0;k<4;k++){
int o=tr[j][k];
if(o<0)continue;
if(o==x||o==y||o==z)continue;
deg[i][j]++;
}
if(!deg[i][j])extg(i,j);
}
}
while(head<=tail){
S=q[head++];
A=S>>8,B=S>>1&127;
x=loc[A][0],y=loc[A][1],z=loc[A][2];
if(S&1){
bool flag=0;
for(k=0;k<4;k++){
int o=tr[x][k];
if(o<0)continue;
if(o==y||o==z||o==B)continue;
flag=1;
extf(o,y,z,B);
}
for(k=0;k<4;k++){
int o=tr[y][k];
if(o<0)continue;
if(o==x||o==z||o==B)continue;
flag=1;
extf(x,o,z,B);
}
for(k=0;k<4;k++){
int o=tr[z][k];
if(o<0)continue;
if(o==x||o==y||o==B)continue;
flag=1;
extf(x,y,o,B);
}
if(!flag)extf(x,y,z,B);
}else{
for(k=0;k<4;k++){
int o=tr[B][k];
if(o<0)continue;
if(safe[o])continue;
if(o==x||o==y||o==z)continue;
if(!(--deg[A][o]))extg(A,o);
}
}
}
x=y=z=-1;
for(i=0;i<n;i++)for(j=0;j<m;j++){
if(a[i][j]=='S')B=getid(i,j);
if(a[i][j]=='W'){
if(x<0)x=getid(i,j);
else if(y<0)y=getid(i,j);
else z=getid(i,j);
}
}
if(x>y)swap(x,y);
if(y>z)swap(y,z);
if(x>y)swap(x,y);
puts(f[id[x][y][z]][B]?"danger":"safe");
}
return 0;
}

  

BZOJ1991 : Pku2422 The Wolves and the Sheep的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. [CF] 948A Protect Sheep

    A. Protect Sheep time limit per test1 second memory limit per test256 megabytes inputstandard input ...

  3. Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)A. Protect Sheep

    http://codeforces.com/contest/948/problem/A   A. Protect Sheep Bob is a farmer. He has a large pastu ...

  4. Codeforces Round #470 (Div. 2) A Protect Sheep (基础)输入输出的警示、边界处理

    Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to w ...

  5. 2001. Counting Sheep

      After a long night of coding, Charles Pearson Peterson is having trouble sleeping. This is not onl ...

  6. hdu 3046 Pleasant sheep and big big wolf 最小割

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046 In ZJNU, there is a well-known prairie. And it a ...

  7. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  8. Counting sheep...

    Counting sheep... Description: Consider an array of sheep where some sheep may be missing from their ...

  9. HDU-2952 Counting Sheep (DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

随机推荐

  1. Intellij IDEA 修改jsp 不能实时更新

    Intellij IDEA 修改jsp 不能实时更新 1. 首先,output要指定到项目的webapp下,这样应该就可以实时更新了 2. 我的问题是这样设置之后,也不可以,原来是可以的,重装系统之后 ...

  2. iptables系列

    详情请参考:http://www.zsythink.net/archives/tag/iptables/page/2/

  3. Django --- Django下载和APP创建 ORM (大概步骤)

    1,下载: 命令行: pip install django == 1.11.15 pip install -i或 源 django == 1.11.15 pycharm settings 解释器 点 ...

  4. MUI之App开发

    一般开发APP分为两种:1.原生ios和android语言开发.2.混合开发,里边穿插h5的东西. 3.第三种:现在因为前端用hbuilder工具开发的情况越来越多,这家公司又提供了更多的选择,所以近 ...

  5. MUI底部导航切换子页面

    1.登陆页面进入之后,进入到main页面,main页面只有一个底部导航,然后引入子页面进行渲染. <nav class="mui-bar mui-bar-tab" id=&q ...

  6. SP283 NAPTIME - Naptime

    SP283 NAPTIME - Naptime 题意: 在某个星球上,一天由N小时构成.我们称0-1点为第一个小时,1-2点为第二个小时,以此类推.在第i个小时睡觉能恢复Ui点体力.在这座星球上住着一 ...

  7. OpenStack--ntp组件时间同步服务

    作用:ntp主要是用于对计算机的时间同步管理操作 环境: 服务端: 192.168.245.172 客户端: 192.168.245.171 时间是对服务器来说是很重要的,一般很多网站都需要读取服务器 ...

  8. MYSQL 单表一对多查询,将多条记录合并成一条记录

    一.描述: 在MySQL 5.6环境下,应工作需求:将一个表中多条某个相同字段的其他字段合并(不太会表达,有点绕,直接上图) 想要达到的效果: 实现SQL语句: SELECT a.books, GRO ...

  9. Jenkins job之间依赖关系配置(联动构建)

    使用场景: 想要在某APP打新包之后,立即执行自动化测试的job来验证该新包.比如Job A 执行完执行Job B ,如下图所示,如何建立依赖呢? 主要有两种方法: 1.配置上游依赖: 2.配置下游依 ...

  10. 日期时间选择器、Bootstrap日期和时间表单组件。bootstrap-datetimepicker实现年月日,时分秒的选择。

    参考链接:http://www.bootcss.com/p/bootstrap-datetimepicker/ 1.官网以及很详细的说明了如何使用,这里结合一下自己的使用来写下. 下载解压缩包以后,可 ...