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 |
DFS作法#include<iostream> #include<cmath> using namespace std; int n,k,ans; void dfs(int now,int d){ if(d>=ans)return; ans=min(ans,d+abs(now-n)); if(now==0)return; if(now%2){ dfs((now+1)/2,d+2); dfs((now-1)/2,d+2); }else{ dfs(now/2,d+1); } } int main(){ ans=1e9; cin>>n>>k; dfs(k,0); cout<<ans<<endl; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator