将每个不是障碍的格子标号,设三只狼的位置分别为$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)$。

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. const int N=10,M=161701;
  5. 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];
  6. int id[N*N][N*N][N*N],loc[M][3];
  7. int tr[N*N][4];
  8. bool f[M][N*N],g[M][N*N];
  9. char deg[M][N*N];
  10. int q[M*N*N*2],head,tail;
  11. inline int getid(int x,int y){return x*m+y;}
  12. inline void extf(int A,int B,int C,int y){
  13. if(A>B)swap(A,B);
  14. if(B>C)swap(B,C);
  15. if(A>B)swap(A,B);
  16. int x=id[A][B][C];
  17. if(f[x][y])return;
  18. f[x][y]=1;
  19. q[++tail]=(x<<8)|(y<<1);
  20. }
  21. inline void extg(int x,int y){
  22. g[x][y]=1;
  23. q[++tail]=(x<<8)|(y<<1)|1;
  24. }
  25. int main(){
  26. while(~scanf("%d%d",&n,&m)){
  27. for(i=0;i<n;i++)scanf("%s",a[i]);
  28. all=n*m;
  29. for(i=0;i<all;i++)can[i]=safe[i]=0;
  30. for(i=0;i<n;i++)for(j=0;j<m;j++){
  31. if(a[i][j]!='X')can[getid(i,j)]=1;
  32. if(i==0||i==n-1||j==0||j==m-1)safe[getid(i,j)]=1;
  33. }
  34. for(i=0;i<n;i++)for(j=0;j<m;j++)for(k=0;k<4;k++){
  35. tr[getid(i,j)][k]=-1;
  36. int nx=i,ny=j;
  37. if(k==0)nx--;
  38. if(k==1)nx++;
  39. if(k==2)ny--;
  40. if(k==3)ny++;
  41. if(nx<0||nx>=n||ny<0||ny>=m)continue;
  42. if(a[i][j]!='X'&&a[nx][ny]!='X')tr[getid(i,j)][k]=getid(nx,ny);
  43. }
  44. tot=0;
  45. 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]){
  46. id[i][j][k]=tot;
  47. loc[tot][0]=i;
  48. loc[tot][1]=j;
  49. loc[tot][2]=k;
  50. tot++;
  51. }
  52. for(i=0;i<tot;i++)for(j=0;j<all;j++)f[i][j]=g[i][j]=deg[i][j]=0;
  53. head=1,tail=0;
  54. for(i=0;i<tot;i++){
  55. x=loc[i][0],y=loc[i][1],z=loc[i][2];
  56. for(j=0;j<all;j++)if(can[j]){
  57. if(j==x||j==y||j==z)continue;
  58. if(safe[j]){deg[i][j]++;continue;}
  59. for(k=0;k<4;k++){
  60. int o=tr[j][k];
  61. if(o<0)continue;
  62. if(o==x||o==y||o==z)continue;
  63. deg[i][j]++;
  64. }
  65. if(!deg[i][j])extg(i,j);
  66. }
  67. }
  68. while(head<=tail){
  69. S=q[head++];
  70. A=S>>8,B=S>>1&127;
  71. x=loc[A][0],y=loc[A][1],z=loc[A][2];
  72. if(S&1){
  73. bool flag=0;
  74. for(k=0;k<4;k++){
  75. int o=tr[x][k];
  76. if(o<0)continue;
  77. if(o==y||o==z||o==B)continue;
  78. flag=1;
  79. extf(o,y,z,B);
  80. }
  81. for(k=0;k<4;k++){
  82. int o=tr[y][k];
  83. if(o<0)continue;
  84. if(o==x||o==z||o==B)continue;
  85. flag=1;
  86. extf(x,o,z,B);
  87. }
  88. for(k=0;k<4;k++){
  89. int o=tr[z][k];
  90. if(o<0)continue;
  91. if(o==x||o==y||o==B)continue;
  92. flag=1;
  93. extf(x,y,o,B);
  94. }
  95. if(!flag)extf(x,y,z,B);
  96. }else{
  97. for(k=0;k<4;k++){
  98. int o=tr[B][k];
  99. if(o<0)continue;
  100. if(safe[o])continue;
  101. if(o==x||o==y||o==z)continue;
  102. if(!(--deg[A][o]))extg(A,o);
  103. }
  104. }
  105. }
  106. x=y=z=-1;
  107. for(i=0;i<n;i++)for(j=0;j<m;j++){
  108. if(a[i][j]=='S')B=getid(i,j);
  109. if(a[i][j]=='W'){
  110. if(x<0)x=getid(i,j);
  111. else if(y<0)y=getid(i,j);
  112. else z=getid(i,j);
  113. }
  114. }
  115. if(x>y)swap(x,y);
  116. if(y>z)swap(y,z);
  117. if(x>y)swap(x,y);
  118. puts(f[id[x][y][z]][B]?"danger":"safe");
  119. }
  120. return 0;
  121. }

  

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. R语言数据集的技术

    特征值选择技术要点 特征值选择技术要点(特征值分解) 作者:王立敏 文章来源:xiahouzuoxin 一.特征值分解 1.特征值分解 线性代数中,特征分解(Eigendecomposition),又 ...

  2. python中元组/列表/字典/集合

    转自:https://blog.csdn.net/lobo_seeworld/article/details/79404566

  3. 通配符的匹配很全面, 但无法找到元素 'mvc:annotation-driven' 的声明

    通配符的匹配很全面, 但无法找到元素 'mvc:annotation-driven' 的声明 错误原因是springmvc中的约束信息不对 <beans xmlns="http://w ...

  4. wireshark 抓包过滤器使用

    目录 wireshark 抓包过滤器 一.抓包过滤器 二.显示过滤器 整理自陈鑫杰老师的wireshark教程课 wireshark 抓包过滤器 过滤器分为抓包过滤器和显示过滤器,抓包过滤器会将不满足 ...

  5. reuters-多分类问题

    from keras.datasets import reuters import numpy as np from keras.utils.np_utils import to_categorica ...

  6. 记录一个使用HttpClient过程中的一个bug

    最近用HttpClient进行链接请求,开了多线程之后发现经常有线程hang住,查看线程dump java.lang.Thread.State: RUNNABLE at java.net.Socket ...

  7. php获取脚本执行的参数

    在看PHP文档到预定义变量时碰到了$argc和$argv,顺手记录下 getopt()从命令行参数列表中获取选项 $arg = getopt('d:n:'); //只接收d n之后的参数 $num = ...

  8. 5. SpringBoot —— Actuator简介

    Actuator是SpringBoot提供的用来帮助我们在将应用程序推向生产环境时对其进行监视和管理的工具集.使用Actuator最简单的方式,就是在pom文件中添加如下依赖: <depende ...

  9. nyoj 633 幂

    幂 nyoj 633 应用数学 幂 时间限制:3000 ms  |  内存限制:65535 KB   描述 在学习循环的时候,我们都练习过利用循环计算a的k次方.现在给定整数k和一个整数m,请你求出对 ...

  10. Node 环境变量 process.env.NODE_ENV 之webpack应用

    转载来源:https://github.com/wfzong/NODE_ENV_TEST,这里还有源码可以学习,谢谢原作者的分享! 对于process.env.NODE_ENV困惑起因为在配置webp ...