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

贴份c++代码,还是要注意代码的严谨性

Posted by a280920481 at 2018-11-20 00:27:48 on Problem 3278
#include <iostream>
using namespace std;

struct Situ
{
	int n, s;
}si[400005];//实现队列

bool bfs[400005];// bfs 数组

int main()
{
	int N, K, ans = 1 << 30, qBegin = 0, qEnd = 0;
	Situ m;

	cin >> N >> K;

	m.n = N;
	m.s = 0;
	si[qEnd++] = m;

	while (qEnd > qBegin)
	{
		m = si[qBegin++];

		if (m.n == K)
		{
			ans = m.s;
			break;
		}
		else
		{
			if (m.n)//加入对 n == 0 的判定
			{
				if ((!bfs[m.n << 1]) && (m.n < K))//加入对出界的判定
				{
					si[qEnd].n = (m.n << 1);
					si[qEnd++].s = m.s + 1;
					bfs[m.n << 1] = true;
				}
				if (!bfs[m.n - 1])
				{
					si[qEnd].n = (m.n - 1);
					si[qEnd++].s = m.s + 1;
					bfs[m.n - 1] = true;
				}
			}

			if ((!bfs[m.n + 1]) && (m.n < K))//加入对出界的判定
			{
				si[qEnd].n = m.n + 1;
				si[qEnd++].s = m.s + 1;
				bfs[m.n + 1] = true;
			}
		}

	}

	cout << ans << '\n';

	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