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 ...
随机推荐
- [leetcode] 401. Binary Watch
https://leetcode.com/contest/5/problems/binary-watch/ 这个题应该是这次最水的,这个题目就是二进制,然后是10位,最大1024,然后遍历,找二进制里 ...
- ImageButton如何让图片按比例缩放不被拉伸
了解 在安卓的界面XML中,ImageButton有这样一个属性android:scaleType,他干嘛的? ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例 ...
- GCC编译器入门
GCC(GNU Compiler Collection,GNU编译器套件),是由 GNU 开发的编程语言编译器.它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分.GCC原本作为GNU操作 ...
- PHP生成压缩文件开发实例
大概需求: 每一个订单都有多个文件附件,在下载的时候希望对当前订单的文件自动打包成一个压缩包下载 细节需求:当前订单号_年月日+时间.zip 例如: 1.生成压缩文件,压缩文件名格式: 2.压缩文件 ...
- oracle学习总结
set linesize 120 set pagesize 20 column file_name format a8 v$nls_parameters 数据字典中的一个表 关于null的注意: 1: ...
- Vijos P1061 迎春舞会之三人组舞 DP
题目链接:https://vijos.org/p/1061 n个人选出3*m人,排成m组,每组3人. 站的队形——较矮的2个人站两侧,最高的站中间. 从对称学角度来欣赏,左右两个人的身高越接近,则这一 ...
- Quartz Scheduler 开发指南(1)
Quartz Scheduler 开发指南(1) 原文地址:http://www.quartz-scheduler.org/generated/2.2.2/html/qtz-all/ 实例化调度程序( ...
- Spring MVC 注解和XML的区别
注解与XML配置的区别 注解:是一种分散式的元数据,与源代码紧绑定. xml:是一种集中式的元数据,与源代码无绑定. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码绑定/无绑定. ...
- Spring AOP 性能监控器
spring,真是一个好东西:性能,真是个让人头疼又不得不面对的问题.如何排查出项目中性能瓶颈?如何迅速定位系统的慢查询?在这我就不说spring自带的性能监控器了,实在是有些简陋.下面就说说我自己写 ...
- [转载]MongoDB学习 (五):查询操作符(Query Operators).1st
本文地址:http://www.cnblogs.com/egger/archive/2013/05/04/3059374.html 欢迎转载 ,请保留此链接๑•́ ₃•̀๑! 查询操作符(Quer ...