0140 - Bus Line

#include <iostream>
#include <vector>
using namespace std;

void Slove(int n)	{
	const int place[25]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
	int a, b;

	for (int i=0; i<n; ++i)	{
		cin >> a >> b;
		int start=0;
		while (place[start] != a)
			++start;
		vector<int> first;
		first.clear();
		for (int j=start; j<25; ++j)	{
			first.push_back(place[j]);
			if (place[j] == b)	break;
		}
		if (1<=a && a<=5 && a>b)	{
			vector<int> second;
			second.clear();
			for (int j=start; j>=0; --j)	{
				second.push_back(place[j]);
				if (place[j] == b)	break;
			}
			if ( first.size() > second.size() )	first=second;
		}
		for (int j=0; j<first.size(); ++j)	{
			cout << first[j];
			(j!=first.size()-1)? cout << " " : cout << endl;
		}
	}
}

int main(void)	{
	int n;

	cin >> n;
	Slove(n);
	return 0;
}

問題文の記述の通り停留所の定義。
1~5は双方向で通過可能、最短を表示。
~了~