您的位置:首页 > 其它

poj 3278 catch that cow

2012-03-29 20:33 330 查看
bfs,RE多次之后学习大牛的代码,不要惊讶怎么限定在100000范围内!

更好的解法:http://www.cnblogs.com/longzhiri/articles/1555344.html

# include <stdio.h>
# include <string.h>
# include <queue>

using namespace std;

int n, k, x;
int d[100002];
queue<int>Q;

int main()
{
while (~scanf("%d%d", &n, &k))
{
memset(d, 0, sizeof(d));
while (!Q.empty()) Q.pop();
Q.push(n);
while (!Q.empty())
{
x = Q.front();
Q.pop();
if (x == k) break;
if (x > 0  && 0==d[x-1]) {Q.push(x-1); d[x-1] = d[x]+1;}
if (x < 100001 && 0==d[x+1]) {Q.push(x+1); d[x+1] = d[x]+1;}
if ((x<<1) < 100001 && 0==d[x<<1]) {Q.push(x<<1); d[x<<1] = d[x]+1;}
}
printf("%d\n", d[k]);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: