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

不过现在我们优先蛇的头的话,要用一个巧妙的哈希来把蛇的身体表达出来,那么就可以用一个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. JDK1.8 HashMap中put源码分析

    一.存储结构      在JDK1.8之前,HashMap采用桶+链表实现,本质就是采用数组+单向链表组合型的数据结构.它之所以有相当快的查询速度主要是因为它是通过计算散列码来决定存储的位置.Hash ...

  2. Json操作

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. BootstrapDialog.show函数底层简化

    平台用的全部都是BootStrapDialog的弹窗,然后美工设计了一个统一的样式,每次写的时候,都要对其进行样式重写:写吐了快,所以对BootStrap.底层做了修改: 也就是说,只要你要写的界面包 ...

  4. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  5. vmware RHEL6.x 开启FTP和TELNET服务--root权限

    //vmware RHEL6.x默认未安装ftp工具,需自己安装--root权限 第一部分:ftp //检查ftp是否安装 # rpm -qa | grep -i vsftpd //找到ftp的rpm ...

  6. 【1】Bootstrap入门引言

    Bootstrap学习者要具备的一些要求: [1]xhtml常用标签的基础知识 [2]xhtml+css布局的基础知识 [3]html5+css3的基础知识 ===================== ...

  7. (转载)Delphi TStringList的用法

    Delphi TStringList的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. TStringList 常用方法与属性: var List: TStringL ...

  8. android实现视频图片取缩略图

    取缩略图不等同于缩放图片. 缩放图片是保持不失真的情况下缩放处理,并进行平滑处理. 缩略图则不然,允许失真,目的只是取出图片的轮廓. 保存Bitmap图片 private void saveBitma ...

  9. 今年的IT大趋势是虚拟现实

    从今年下半年开始,陆陆续续出现了一些基于虚拟现实技术的创业公司,先是从IT新闻中的一篇创业故事中了解到这个方向,后来再是身边一个以前的朋友也发布了类似的产品. 从他们的产品来看,基本都是围绕教育行业开 ...

  10. 黄聪:如何使用CodeSmith批量生成代码(转:http://www.cnblogs.com/huangcong/archive/2010/06/14/1758201.html)

    先看看CodeSmith的工作原理: 简单的说:CodeSmith首先会去数据库获取数据库的结构,如各个表的名称,表的字段,表间的关系等等,之后再根据用户自定义好的模板文件,用数据库结构中的关键字替代 ...