BZOJ1991 : Pku2422 The Wolves and the Sheep
将每个不是障碍的格子标号,设三只狼的位置分别为$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的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- [CF] 948A Protect Sheep
A. Protect Sheep time limit per test1 second memory limit per test256 megabytes inputstandard input ...
- 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 ...
- 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 ...
- 2001. Counting Sheep
After a long night of coding, Charles Pearson Peterson is having trouble sleeping. This is not onl ...
- 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 ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- Counting sheep...
Counting sheep... Description: Consider an array of sheep where some sheep may be missing from their ...
- HDU-2952 Counting Sheep (DFS)
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
随机推荐
- springmvc 开发流程图
- 2018-2019-2 20165221 【网络对抗技术】-- Exp6 信息搜集与漏洞扫描
2018-2019-2 20165221 [网络对抗技术]-- Exp6 信息搜集与漏洞扫描 目录 1. 实践目标 2. 实践内容 3. 各种搜索技巧的应用 a. 搜索网址的目录结构 b.使用IP路由 ...
- linux 如何截取一段时间内log日志
截取一段时间内的log日志可以使用sed命令对log文件进行抽取操作: 1,sed查看某时间段到现在的系统日志:sed -n '/May 20 17/,$p' /var/log/messages | ...
- 使用scrapy爬虫,爬取17k小说网的案例-方法一
无意间看到17小说网里面有一些小说小故事,于是决定用爬虫爬取下来自己看着玩,下图这个页面就是要爬取的来源. a 这个页面一共有125个标题,每个标题里面对应一个内容,如下图所示 下面直接看最核心spi ...
- 【C++11】unoedered_map和map(部分转载)
1.结论 新版的hash_map都是unordered_map了,这里只说unordered_map和map. 运行效率:unordered_map最高,而map效率较低但提供了稳定效率和有序的序列. ...
- Oracle建立全文索引详解
Oracle建立全文索引详解1.全文检索和普通检索的区别 不使用Oracle text功能,当然也有很多方法可以在Oracle数据库中搜索文本,比如INSTR函数和LIKE操作: SELECT *FR ...
- java实现一个简单的Web服务器
注:本段内容来源于<JAVA 实现 简单的 HTTP服务器> 1. HTTP所有状态码 状态码 状态码英文名称 中文描述 100 Continue 继续.客户端应继续其请求 101 Swi ...
- eclipse下properties文件中文乱码的解决方案
今天在工程下编辑.properties文件时输入了中文然后就保存出错,弄了好久才搞定!大家瞄瞄 在中文操作系统下,Eclipse中的Java类型文件的编码的默认设置是GBK,但是对Properties ...
- 网络流24题——数字梯形问题 luogu 4013
题目描述:这里 极其裸的一道费用流问题 首先分析第一问,由于要求一个点只能经过一次,所以我们将梯形中的每一个点拆成两个点(记为入点和出点,顾名思义,入点用来承接上一行向这一行的边,出点用来向下一行连边 ...
- 设计模式 — 单例模式(Singleton)
在一个软件系统中,经常有有些特殊的对象就需要一个实例,如果有多个的话,就比较浪费服务器资源,最典型的就是 整个系统的配置文件对象. 普通方式读取配置文件 // 配置文件 SingletonApp.pr ...