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

高手指点哪里出错了

Posted by onlystruggle at 2008-08-08 15:06:02 on Problem 3278
#include <iostream>
#include <queue>

using namespace std;

#define MAXN 1000000

struct state { int x; int y; };

bool mark[2*MAXN+2];

int bfs(int n, int k);

int main()
{
	int N, K;
	scanf("%d%d", &N, &K);
	if(N>=K) printf("%d\n", N-K);
	else printf("%d\n", bfs(N, K));
	system("pause");
}

int bfs(int n, int k)
{
		state inistate, s, t;
		inistate.x = n;
		inistate.y = 0;
		queue<state> q;
		q.push(inistate);
		mark[n] = true;
		while(true)
		{
			s = q.front();
			q.pop();
			t.x = s.x+1;
			t.y = s.y+1;
			if(t.x==k) return t.y;
			else if(t.x<k && t.x==false)
			{
				q.push(t);
				mark[t.x] = true;
			}
			t.x = s.x-1;
			t.y = s.y+1;
			if(t.x==k) return t.y;
			else if(t.x>=1 && mark[t.x]==false)
			{
				q.push(t);
				mark[t.x] = true;
			}
			t.x = s.x*2;
			t.y = s.y+1;
			if(t.x==k) return t.y;
			else if(t.x<k && mark[t.x]==false)
			{
				q.push(t);
				mark[t.x] = true;
			}
		}
}

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