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 |
哪个大佬帮我看看怎么就错了 枯了#include <iostream> #include <string> #include <queue> #include <algorithm> #include <cstring> using namespace std; typedef long long ll; const ll maxn = 100000; ll N,M; bool vis[maxn]; struct node { ll x; ll step; }; void bfs(int n,int k) { node v,t,p; queue<node> q; p.x = n; p.step = k; q.push(p); while(!q.empty()) { t = q.front(); q.pop(); if(t.x == M) { cout<<t.step<<endl; return; } if(t.x - 1 >= 0 && !vis[t.x-1]) { v.x = t.x-1; v.step = t.step+1; q.push(v); vis[v.x] = 1; } if(t.x + 1 <= maxn && !vis[t.x+1]) { v.x = t.x+1; v.step = t.step+1; q.push(v); vis[v.x] = 1; } if(t.x * 2 <= maxn && !vis[t.x*2]) { v.x = t.x*2; v.step = t.step+1; q.push(v); vis[v.x] = 1; } } } int main() { cin >> N >> M; memset(vis,0,sizeof(vis)); vis[N] = 1; bfs(N,0); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator