pku 3278 Catch That Cow bfs
来源:互联网 发布:淘宝引流工具有用吗 编辑:程序博客网 时间:2024/05/17 11:35
//简单的bfs,注意不要超过 133333,否则“无效内存引用”;
//x*2 - 100000 < x - 100000, 求得 x < 1333333; 为最优的数
//其实不需要,开到200000就行。
#include<stdio.h>
#include<string.h>#include<stdlib.h>
#include<queue>
using namespace std;
int f[250000];
struct node
{
int pos,time;
};
int t,n,k;
bool vis(int a)
{
if(a>=0 && a<=133333 && f[a]==0)
return true;
return false;
}
int bfs()
{
node sta,next;
int i,j;
sta.pos=n;
sta.time=0;
f[n]=1;
queue<node>q;
q.push(sta);
while(!q.empty())
{
sta=q.front();
q.pop();
if(sta.pos == k)
return sta.time;
for(i=1;i<=3;i++)
{
switch(i)
{
case 1:next.pos =sta.pos+1;break;
case 2:next.pos =sta.pos-1;break;
case 3:next.pos =sta.pos*2;break;
}
if(vis(next.pos))
{
next.time=sta.time+1;
f[next.pos]=1;
q.push(next);
}
}
}
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
memset(f,0,sizeof(f));
scanf("%d%d",&n,&k);
int sum=bfs();
printf("%d\n",sum);
}
}
- pku 3278 Catch That Cow bfs
- pku 3278 Catch That Cow---BFS
- PKU 3278 Catch That Cow
- BFS | 3278 | Catch That Cow
- pku 3278 Catch That Cow(广搜)
- Catch That Cow(bfs)
- Catch That Cow+BFS
- Catch That Cow BFS
- Catch That Cow BFS
- Catch That Cow(BFS)
- Catch That Cow BFS
- Catch That Cow (BFS)
- Catch That Cow【bfs】
- Catch That Cow(bfs)
- Catch That Cow BFS
- bfs-catch that cow
- Catch That Cow(bfs)
- Catch That Cow bfs
- 学习笔记 一
- 谈web--部署和服务器选型
- Ubuntu下不能用sunpinyin进行翻页问题解决
- java applet 猜数字小游戏
- python中函数陷阱
- pku 3278 Catch That Cow bfs
- ini文件的读写方法
- android socket长连接问题
- 黑马程序员_Java基础(流程控制、函数、数组)
- HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面 .
- 半数集问题
- 下周放假到十教,看看母校
- I9300硬件拆解浅析
- 整合Spring3.0.5,Hibernate3.3,Struts2.3.8(中文OK)