Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

DFS作法

Posted by misclicked at 2018-10-12 16:04:10 on Problem 3278
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator