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 ...
随机推荐
- Poj/OpenJudge 1094 Sorting It All Out
1.链接地址: http://poj.org/problem?id=1094 http://bailian.openjudge.cn/practice/1094 2.题目: Sorting It Al ...
- HTML5之地理信息应用 获取自己的位置
上代码: window.onload = function() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosit ...
- PHOTOSHOP 半透明方格
1.新建60*60的透明文档,在左方和上方用直线工具画白边,存储为图案(编辑/定义图案) 2.新建图层,用油漆桶填充图案 3. 选择若干小方格,填充白色后设置不透明度50%
- 纯原生js移动端城市选择插件
接着上一篇纯js移动端日期选择插件,话说今天同事又来咨询省市县联动的效果在移动端中如何实现,还是老样子,百度上一搜,诶~又全是基于jquery.zepto的,更加可恨的是大多数都是PC版的,三个sel ...
- js实现瀑布流的一种简单方法实例分享
现在说瀑布流式布局似乎有点晚了,但是每一项技术都是向着“精”和“简”的方向在不断发展,在发展到极致之前,需要一个相当漫长的过程,因此,从这个角度来说,当瀑布流被应用得越来越多的时候,反而更应该讨论它, ...
- nuget的使用总结
使用NuGet发布自己的类库包(Library Package) from:http://blog.csdn.net/gulijiang2008/article/details/41724927 使用 ...
- configure: error: zlib library and headers are required
configure: error: zlib library and headers are required (1)直接看是zlib没安装导致的,yum list |grep zlib* 看到的是全 ...
- MJRefreshFooterView
实例化header和footer _header = [MJRefreshHeaderView header]; _header.scrollView = _tableView; 设置header和f ...
- hbase 0.96 java 示例
import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; impo ...
- csdn搜索技巧
由于CSDN下载资源高级搜索,只能显示10页分页,所以我们换种方法去搜索csdn的资源. 第10页以后没了. 在百度框里面输入如下: jquery site:download.csdn.net 图: