其实就是我们经常玩的贪吃蛇。

不过现在我们优先蛇的头的话,要用一个巧妙的哈希来把蛇的身体表达出来,那么就可以用一个4进制的数字来表示,蛇的身体长度最多不超过8,所以最多是2^7种状态。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
int const N = ;
int const M = ;
int const State = ;
struct Snake
{
int bx[N],by[N];
int step;
}cur,nt;
Snake q[];
int head,tail;
int n,m,l,x,y,ans,s;
bool map[M][M],vis[M][M][State];
int dirx[]={,,-,};
int diry[]={-,,,};
bool judge(int x,int y)
{
return x<||x>n||y<||y>m||map[x][y];
}
int check(int x1,int y1,int x2,int y2)
{
if(x1==x2)
{
if(y1>y2)
return ;
else
return ;
}
else
{
if(x1>x2)
return ;
else
return ;
}
}
int bfs()
{
cur.step=;
int state=;
for(int j=;j<l-;j++)
state=state*+check(cur.bx[j],cur.by[j],cur.bx[j+],cur.by[j+]);
vis[cur.bx[]][cur.by[]][state]=true;
head=tail=;
q[tail++]=cur;
while(head<tail)
{
cur=q[head++];
if(cur.bx[]==&&cur.by[]==)return cur.step;
for(int i=;i<;i++)
{
nt.bx[]=cur.bx[]+dirx[i];
nt.by[]=cur.by[]+diry[i];
nt.step=cur.step+;
if(judge(nt.bx[],nt.by[]))continue;
int f=;
for(int j=;j<l;j++)
if(nt.bx[]==cur.bx[j]&&nt.by[]==cur.by[j])
{
f=;break;
}
if(f)continue;
for(int j=l-;j>;j--)
nt.bx[j]=cur.bx[j-],nt.by[j]=cur.by[j-];
int state=;
for(int j=;j<l-;j++)
state=state*+check(nt.bx[j],nt.by[j],nt.bx[j+],nt.by[j+]);
if(!vis[nt.bx[]][nt.by[]][state])
{
vis[nt.bx[]][nt.by[]][state]=true;
q[tail++]=nt;
}
}
}
return -;
}
int main()
{
int T=;
while(~scanf("%d %d %d",&n,&m,&l)&&(n+m+l)!=)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
map[i][j]=false;
for(int k=;k<=(<<(*(l-)));k++)
vis[i][j][k]=false;
}
for(int i=;i<l;i++)
scanf("%d %d",&cur.bx[i],&cur.by[i]);
scanf("%d",&s);
for(int i=;i<s;i++)
{
scanf("%d %d",&x,&y);
map[x][y]=true;
}
printf("Case %d: %d\n",++T,bfs());
}
return ;
}

poj 1324 状态广搜的更多相关文章

  1. poj 1324 状态压缩+bfs

    http://poj.org/problem?id=1324 Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submis ...

  2. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  3. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  4. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  5. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  6. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  7. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  8. (poj)3414 Pots (输出路径的广搜)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  9. UVA 10047 The Monocycle (状态记录广搜)

    Problem A: The Monocycle  A monocycle is a cycle that runs on one wheel and the one we will be consi ...

随机推荐

  1. Java如何获得平台相关的行分隔符、路径分隔符?

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3550194.html ,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体 ...

  2. OpenJudge 2754 八皇后

    1.链接地址: http://bailian.openjudge.cn/practice/2754 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 会下国际象棋的人都很清楚: ...

  3. 设置Session的超时时间

    设置Session的超时时间 IIS 里面有个设置 站点属性->主目录->应用程序配置->选项->启用会话状态->会话超时,可以设置. 在web.config中,设置: ...

  4. 没用调用flush导致的数据保存丢失

    在将字符串保存到文件时,我们采有下面的写法,大部分情况下,都可以直接将数据保存到文件中, using (var fs = System.IO.File.Create(path)) { var sw = ...

  5. js中的referrer返回上一页使用介绍

    js中的referrer的用法举例. js完整代码:  <script language="javascript">  var refer=document.refer ...

  6. [PHP]MemCached高级缓存

    Memcache Win32 的安装下载:Memcache Win32 [www.php100.com]   [www.jehiah.cz/projects/memcached-win32/] 1.解 ...

  7. GCC交叉编译链命名

    命名格式: arch[-vendor][-os]-abi arch:CPU的架构 vendor:工具链的供应商 os: 目标上运行的操作系统,不同的操作系统对应着不同的C库,例如 newlib.gli ...

  8. 【Base64】JDK里面实现Base64的API

    原文出处: 成熟的毛毛虫的博客 BASE64 编码是一种常用的字符编码,在很多地方都会用到.但base64不是安全领域下的加密解密算法.能起到安全作用的效果很差,而且很容易破解,他核心作用应该是传输数 ...

  9. C# dynamic

    [TestMethod] public void DynamicTest() { dynamic Customer = new ExpandoObject(); Customer.Name = &qu ...

  10. ms-grid layout

    <!DOCTYPE html> <html> <head> <title></title> <script src="js/ ...