| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
要先判断是否超范围再判断是否访问过。。。弱鸡贴个代码,BFS 47MS#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100005;
int posn,posk;
int t[maxn];
int bfs()
{
memset(t,-1,sizeof(t));
queue<int> que;
que.push(posn);
t[posn] = 0;
int temp;
while(que.size()){
temp = que.front();
que.pop();
if(temp == posk){
break;
}
int next;
next = temp - 1;
if(0 <= next && next <= maxn - 5 && t[next] == -1){
que.push(next);
t[next] = t[temp] + 1;
}
next = temp + 1;
if(0 <= next && next <= maxn - 5 && t[next] == -1){
que.push(next);
t[next] = t[temp] + 1;
}
next = temp + temp;
if(0 <= next && next <= maxn - 5 && t[next] == -1){
que.push(next);
t[next] = t[temp] + 1;
}
}
return t[temp];
}
int main()
{
while(scanf("%d %d",&posn,&posk) != EOF){
printf("%d\n",bfs());
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator