您的位置:首页 > 其它

POJ - 3278 Catch That Cow

2016-02-24 20:25 369 查看

1.题面

http://poj.org/problem?id=3278

2.解题思路

标准的bfs,在坐标轴上有一个点,你可以选择将这个点向正方向或是负方向移动一步,或是讲他的坐标×2,使用queue进行bfs搜索,顺便使用set判重,愉快地过了

3.解题代码

/*****************************************************************
> File Name: tmp.cpp
> Author: Uncle_Sugar
> Mail: uncle_sugar@qq.com
> Created Time: 2016年02月18日 星期四 20时54分36秒
****************************************************************/
# include <cstdio>
# include <cstring>
# include <cmath>
# include <queue>
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
using namespace std;

const int debug = 1;
const int size  = 100000 + 10;
typedef long long ll;
typedef pair<int,int> pir;

char vis[size];
bool inrange(int x){
return x>=0&&x<=size;
}
int solve(int s,int e){
static queue<pir> que;
static int next[3];
while(!que.empty())	que.pop();
memset(vis,0,sizeof(vis));
if (s==e)
return 0;
que.push(pir(s,0));vis[s] = 1;
while (!que.empty()){
pir T = que.front();que.pop();
int step = T.second + 1;
next[0] = T.first + 1;
next[1] = T.first - 1;
next[2] = 2*T.first;
for (int i=0;i<3;i++){
if (inrange(next[i])&&vis[next[i]]==0){
if (next[i]==e)
return step;
que.push(pir(next[i],step));vis[next[i]] = 1;
}
}
}
return -1;
}
int main()
{
std::ios::sync_with_stdio(false);cin.tie(0);
int i,j,k;
int s,e;
while (cin >> s >> e){
cout << solve(s,e) << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: