您的位置:首页 > 其它

hdu 2717 Catch That Cow

2015-03-30 23:08 267 查看
暴力搜索(广搜),注意要先判断再加入队列,否则会超内存

#include<iostream>
#include<queue>
#include<cmath>
#include<cstring>
#define maxn 100000+5
using namespace std;
int dir[2]={1,-1};
int vis[maxn]={0};
int n,m,re;
int maxx;
struct stu
{
int x,s;
};
void bfs(int n)
{
memset(vis,0,sizeof(vis));
stu xx,yy;
xx.x=n;xx.s=0;
queue<stu>root;
root.push(xx);
while(root.size())
{
xx=root.front();
root.pop();
if(xx.x==m) {re=xx.s;return;}
for(int i=0;i<2;i++)
{
yy.x=xx.x+dir[i];
yy.s=xx.s+1;
if(yy.x>=0&&yy.x<=maxn)
{
if(!vis[yy.x]) vis[yy.x]=1,root.push(yy);
}
}
yy.x=xx.x*2;
yy.s=xx.s+1;
if(yy.x>=0&&yy.x<=maxn)
{
if(!vis[yy.x])
{
vis[yy.x]=1;
root.push(yy);
}
}
}
}
int main()
{
while(cin>>n>>m)
{
bfs(n);
cout<<re<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: