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

用STL队列AC了,之前一直WA 都想吐血了

Posted by lyz963254 at 2014-08-07 16:29:46 on Problem 3278
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
const int Max = 100000 + 10;
int s[Max],vis[Max];
int main()
{
	int n,m;
	cin>>n>>m;
	int x;
	queue<int>q;
	q.push(n);
	s[n] = 0;
	while (!q.empty())
	{
		x = q.front();
		q.pop();
		vis[x] = 1;
		if (x==m) break;
		if (x-1>=0 && vis[x-1]==0)
		{
			q.push(x-1);
			s[x-1] = s[x] + 1;
			vis[x-1] = 1;
		}
		if (x+1<100001 && vis[x+1]==0)
		{
			q.push(x+1);
			s[x+1] = s[x] + 1;
			vis[x+1] = 1;
		}
		if (x*2<100001 && vis[x*2]==0)
		{
			q.push(x*2);
			s[x*2] = s[x] + 1;
			vis[x*2] = 1;
		}
	}
	cout<<s[m]<<endl;
	return 0;
}

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