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 |
贴份c++代码,还是要注意代码的严谨性#include <iostream> using namespace std; struct Situ { int n, s; }si[400005];//实现队列 bool bfs[400005];// bfs 数组 int main() { int N, K, ans = 1 << 30, qBegin = 0, qEnd = 0; Situ m; cin >> N >> K; m.n = N; m.s = 0; si[qEnd++] = m; while (qEnd > qBegin) { m = si[qBegin++]; if (m.n == K) { ans = m.s; break; } else { if (m.n)//加入对 n == 0 的判定 { if ((!bfs[m.n << 1]) && (m.n < K))//加入对出界的判定 { si[qEnd].n = (m.n << 1); si[qEnd++].s = m.s + 1; bfs[m.n << 1] = true; } if (!bfs[m.n - 1]) { si[qEnd].n = (m.n - 1); si[qEnd++].s = m.s + 1; bfs[m.n - 1] = true; } } if ((!bfs[m.n + 1]) && (m.n < K))//加入对出界的判定 { si[qEnd].n = m.n + 1; si[qEnd++].s = m.s + 1; bfs[m.n + 1] = true; } } } cout << ans << '\n'; return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator