poj 1324 状态广搜
其实就是我们经常玩的贪吃蛇。
不过现在我们优先蛇的头的话,要用一个巧妙的哈希来把蛇的身体表达出来,那么就可以用一个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 状态广搜的更多相关文章
- poj 1324 状态压缩+bfs
http://poj.org/problem?id=1324 Holedox Moving Time Limit: 5000MS Memory Limit: 65536K Total Submis ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- (poj)3414 Pots (输出路径的广搜)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- 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 ...
随机推荐
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
- Mysql多实例 安装以及配置
MySQL多实例 1.什么是MySQL多实例 简单地说,Mysql多实例就是在一台服务器上同时开启多个不同的服务端口(3306.3307),同时运行多个Mysql服务进程,这些服务进程通过不同的soc ...
- Windows10中的IIS10安装php manager和IIS URL Rewrite 2.0组件的方法
Windows10中自带的Server:Microsoft-IIS/10.0,然后这个10却让原本支持组件无法安装了,php manager组件安装时提示“必须安装IIS7以上才可以安装”.那是不是真 ...
- PHP扩展开发(1):入门
有关PHP扩展开发的文章.博客已经很多了,比较经典的有: TIPI项目(http://www.php-internals.com/,强烈推荐) <Extending and Embedding ...
- ICBC中的路由设置
才去的中国工商银行,用身份证叫了A0076的号,前边还有26个人,闲来无聊果断拿出手机收取wifi.有两个ssid为ICBC的路由,信道分别是1号和6号,还好不需要密码,不过一会就连接上了. 那我先上 ...
- css3太极图效果+自动旋转
主要使用border-radius属性实现圆,半圆,定位坐标覆盖部分模块. 半圆: width: 50%; height: 100%; border-radius:100% 0 0 100% /50% ...
- 电脑升级完Xcode8后 注释快捷键无效的问题
1.部分电脑升级完Xcode8 后直接重启电脑就可以使用Command +/ 快捷键注释代码, 2.如果上述方法没有效果,可以在终端输入sudo /usr/libexec/xpccachectl 然 ...
- 整理sed实战修改多行配置技巧
老男孩老师有关sed实战技巧分享,来自课堂教学内容实战1.在指定行前插入两行内容,分别为oldboy和oldgirl.提示:被修改的文件内容必须要大于等于2行 1 sed -i '2 ioldboy\ ...
- centos6.5 mysql配置整理
安装 // 安装mysql yum -y install mysql-server //设置开机启动 chkconfig mysqld on //启动MySql服务 service mysqld st ...
- Python OptionParser学习
from optparse import OptionParser import sys def main(): p = OptionParser() p.add_option('-n','--nam ...