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 |
大家来找茬(这题真折腾人啊)ac代码的: #include<iostream> #include<queue> using namespace std; typedef long long LL; const int M= 100000+10; int ans[M],vis[M]; int main() { int n,m; cin>>n>>m; int x; queue<int>que; que.push(n); ans[n] = 0; while(!que.empty()) { x = que.front(); que.pop(); vis[x] = 1; if (x==m) { cout<<ans[x]<<endl; break; } if (x-1>=0 && vis[x-1]==0) { que.push(x-1); ans[x-1] = ans[x] + 1; vis[x-1] = 1; } if (x+1<100001 && vis[x+1]==0) { que.push(x+1); ans[x+1] = ans[x] + 1; vis[x+1] = 1; } if (x*2<100001 && vis[x*2]==0) { que.push(x*2); ans[x*2] = ans[x] + 1; vis[x*2] = 1; } } return 0; } Re的代码: int main() { int n,m; cin>>n>>m; int x; queue<int>que; que.push(n); ans[n] = 0; while(!que.empty()) { x = que.front(); que.pop(); vis[x] = 1; if (x==m) { cout<<ans[x]<<endl; break; } if (vis[x-1]==0&&x-1>=0) { que.push(x-1); ans[x-1] = ans[x] + 1; vis[x-1] = 1; } if (vis[x+1]==0&&x+1<100001 ) { que.push(x+1); ans[x+1] = ans[x] + 1; vis[x+1] = 1; } if (vis[x*2]==0&&x*2<100001 ) { que.push(x*2); ans[x*2] = ans[x] + 1; vis[x*2] = 1; } } return 0; } 就是把这几个 if (vis[x*2]==0&&x*2<100001 )里面的&&两边换下顺序就对了。。。 Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator