您的位置:首页 > 其它

POJ3278——Catch That Cow

2013-03-21 23:04 344 查看

题目大意:给你两个整数,n和k,n有三种方式移动,n+1,n-1,n*2, 最快让n==k

解题方法:宽度搜索,剪枝,三入口搜索

技巧:

■ 数组要开的大

■ 注意剪枝,不然 RE

Source Code
Problem: 3278		User: Smile_7
Memory: 1324K		Time: 32MS
Language: C++		Result: Accepted

#include <iostream>
#include <cstring>

using namespace std;

const int MAX = 200030;

typedef class
{
public:
int x;
int step;
}point;

int n , k ;
bool vist[MAX];
point queue[MAX];

void bfs()
{
int head , tail;
queue[head = tail = 0].x = n;
queue[tail++].step = 0;

vist
= true;

while(head < tail)
{
point w = queue[head++];

if(w.x == k)
{
cout<<w.step<<endl;
break;
}

if(w.x-1 >=  0 && !vist[w.x-1])
{
vist[w.x-1] = true;
queue[tail].x = w.x-1;
queue[tail++].step = w.step+1;
}
if(w.x <= k && !vist[w.x+1])
{
vist[w.x+1] = true;
queue[tail].x = w.x+1;
queue[tail++].step = w.step+1;
}
if(w.x <= k && !vist[2*w.x])
{
vist[2*w.x] = true;
queue[tail].x = 2*w.x;
queue[tail++].step = w.step+1;
}
}

return;
}

int main()
{
while(cin>>n>>k)
{
memset(vist,false,sizeof(vist));
bfs();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: