HDU 2717 Catch That Cow
居然排第一,值得庆贺。
Code
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
#define MAXSIZE 200001
bool bVisited[MAXSIZE];
int N,K;
struct Set
{
int number,time;
};
int BFS()
{
queue<Set> Q;
Set start;
start.number = K;
start.time =0;
Q.push(start);
Set t;
while(!Q.empty())
{
t = Q.front();
if(t.number == N)
break;
bVisited[t.number] = true;
Q.pop();
Set one;
one.time = t.time+1;
if(t.number%2 == 0 && t.number/2 >= N/2 && bVisited[t.number/2] == false)
{
one.number = t.number/2;
Q.push(one);
}
if(t.number - 1 >= N/2 && bVisited[t.number-1] == false)
{
one.number = t.number - 1;
Q.push(one);
}
if(t.number +1 <= K*2 && bVisited[t.number+1] == false)
{
one.number = t.number + 1;
Q.push(one);
}
}
return t.time;
}
int main()
{
int i;
while(scanf("%d %d",&N,&K)==2)
{
if(N>K)//这里一定要注意分开情况讨论
{
printf("%d\n",N-K);
continue;
}
for(i = N/2; i<=2*K; i++)
bVisited[i] = false;
printf("%d\n",BFS());
}
return 0;
}
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
#define MAXSIZE 200001
bool bVisited[MAXSIZE];
int N,K;
struct Set
{
int number,time;
};
int BFS()
{
queue<Set> Q;
Set start;
start.number = K;
start.time =0;
Q.push(start);
Set t;
while(!Q.empty())
{
t = Q.front();
if(t.number == N)
break;
bVisited[t.number] = true;
Q.pop();
Set one;
one.time = t.time+1;
if(t.number%2 == 0 && t.number/2 >= N/2 && bVisited[t.number/2] == false)
{
one.number = t.number/2;
Q.push(one);
}
if(t.number - 1 >= N/2 && bVisited[t.number-1] == false)
{
one.number = t.number - 1;
Q.push(one);
}
if(t.number +1 <= K*2 && bVisited[t.number+1] == false)
{
one.number = t.number + 1;
Q.push(one);
}
}
return t.time;
}
int main()
{
int i;
while(scanf("%d %d",&N,&K)==2)
{
if(N>K)//这里一定要注意分开情况讨论
{
printf("%d\n",N-K);
continue;
}
for(i = N/2; i<=2*K; i++)
bVisited[i] = false;
printf("%d\n",BFS());
}
return 0;
}