Problem:


バス路線

図 1のようなバス路線があります。停留所は 10 箇所で、それぞれ 0〜9 の番号がついています。バスは停留所 0 では折り返しますが、反対側は循環路線になっており、図のように 5→6→7→8→9→5 の順で循環します。

  

このバス路線について、乗車する停留所と降車する停留所を入力とし、乗車から降車までに通る停留所の番号を出力するプログラムを作成してください。

ただし、1〜5 の停留所では 2 方向のバスに乗車できますが、より短い経路で降車停留所に到着するバスに乗車することとします。例えば、停留所 4 から停留所 2 へ行く場合、左方向に進むバスに乗り「4→3→2」の経路を通ります。また、一度バスに乗車したら途中下車はしないものとします。同じ停留所を乗車停留所、降車停留所に指定することはありません。

Input

複数のデータセットが与えられます。一行目にデータセット数 n (n ≤ 20 ) が与えられます。各データセットとして、乗車する停留所番号と降車する停留所番号が空白区切りで１行に与えられます。

Output

各データセットに対して、通過する停留所番号の並びを空白区切りで１行に出力してください。

Sample Input

2
2 4
4 2

Output for the Sample Input

2 3 4
4 3 2



Sample input:
2
2 4
4 2


Sample output:
2 3 4
4 3 2

Submission status: Wrong Answer

Solution code:
w = [list(range(i, 6)) for i in range(5)] + [[]] + [[6, 5], [7, 6, 5], [8, 9, 5], [9, 5]]
def unique(l):
    nl = []
    for x in l:
        if x not in nl:
            nl.append(x)
    return nl

n = input()
s = []
for i in range(n):
    a, b = map(int, raw_input().split())
    if a < 5 and b < 5:
        s = list(range(a, b + 1) if a < b else range(b, a + 1))
    elif a < 5 or b < 5:
        s = w[a] + list(reversed(w[b]))
    else:
        if max(a, b) - min(a, b) < min(a, b) - max(a, b) + 5:
            x = 1 if a < b else -1
            s = list(range(a, b + x, x))
        else:
            s = w[a] + list(reversed(w[b]))
    print(' '.join(map(str, unique(s))))

Submission status: Wrong Answer

Solution code:
w = [list(range(i, 6)) for i in range(5)] + [[]] + [[6, 5], [7, 6, 5], [8, 9, 5], [9, 5]]
def unique(l):
    nl = []
    for x in l:
        if x not in nl:
            nl.append(x)
    return nl

n = input()
s = []
for i in range(n):
    a, b = map(int, raw_input().split())
    if a < 5 and b < 5:
        x = 1 if a < b else -1
        s = list(range(a, b + x, x))
    elif a < 5 or b < 5:
        s = w[a] + list(reversed(w[b]))
    else:
        if max(a, b) - min(a, b) < min(a, b) - max(a, b) + 5:
            x = 1 if a < b else -1
            s = list(range(a, b + x, x))
        else:
            s = w[a] + list(reversed(w[b]))
    print(' '.join(map(str, unique(s))))

Submission status: Wrong Answer

Solution code:
for _ in [0]*input():
  A = '012345678954321'
  a, b = raw_input().split()
  n = 14
  for _ in [0]*A.count(a):
    I = A.index(a)
    A = A[I:]+A[:I]
    I = A.index(b)
    if n>I:
      n = I
      x = A[:I+1]
    A = A[1:]+A[0]
  for e in x:
    print e,
  print

Submission status: Wrong Answer

Solution code:
for _ in [0]*input():
  A = "012345678954321"
  A += A+A
  a, b = raw_input().split()
  i1 = A.index(a)
  i2 = A.index(b,i1)
  i3 = A.index(a,i2)
  i4 = A.index(b,i3)
  if i2-i1<i4-i3: print " ".join(A[i1:i2+1])
  else: print " ".join(A[i3:i4+1])

Submission status: Wrong Answer

Solution code:
for i in range(input()):
    s, g = map(int, raw_input().split())
    if s < g:
        res = range(s, g+1)
    elif 0 <= s <= 5 and 0 <= g <= 5:
        res = range(s, g-1, -1)
    else:
        res = range(s, 10)
        res.extend(range(5, g-1, -1))
    print ' '.join(map(str, res))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
import os

N = int(input())

for i in range(N):
    a, b = map(int, input().split())
    if a < b:
        lst = [j for j in range(a, b+1)]
        print(*lst)

    # a > b
    else:
        # easy
        if a <= 5:
            lst = [j for j in range(a, b - 1, -1)]
        else:
            lst = [j for j in range(a, 9+1)] + [j for j in range(5, b - 1, -1)]
        print(*lst)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
import os

N = int(input())

for i in range(N):
    a, b = map(int, input().split())
    if a < b:
        lst = [j for j in range(a, b+1)]
        print(*lst)

    # a > b
    else:
        # easy
        if a <= 5:
            lst = [j for j in range(a, b - 1, -1)]
        elif b <= 5:
            lst = [j for j in range(a, 9+1)] + [j for j in range(5, b - 1, -1)]

        # i.e. 7 -> 6
        else:
            lst = [j for j in range(a, 9 + 1)] + [j for j in range(5, b - 1, -1)] + list(range(0, b+1))
        print(*lst)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0140

"""
import sys
from sys import stdin
from enum import Enum
input = stdin.readline


class Graph(object):
    """ single source shortest path """
    class Status(Enum):
        """ ?????????????¨??????¶??? """
        white = 1  # ????¨????
        gray = 2  # ?¨???????
        black = 3  #?¨???????

    def __init__(self, data):
        num_of_nodes = len(data)
        self.color = [Graph.Status.white] * num_of_nodes  # ????????????????¨??????¶???
        self.M = [[float('inf')] * num_of_nodes for _ in range(num_of_nodes)]
        self._make_matrix(data)  # data????????????????????£??\??????(?????\?¶???¨???????????????????????§????????????)
        self.d = [float('inf')] * num_of_nodes  # ?§???????????????????(?????????)
        self.p = [-1] * num_of_nodes  # ????????????????????????????¨?????????????????????????

    def _make_matrix(self, data):
        for f, t, c in data:
            self.M[f][t] = c


    def dijkstra(self, start):
        self.d[start] = 0
        self.p[start] = -1

        while True:
            mincost = float('inf')
            # ??\??????????????§??????????????¨?????????????????????u???????????????
            for i in range(len(self.d)):
                if self.color[i] != Graph.Status.black and self.d[i] < mincost:  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????
                    mincost = self.d[i]
                    u = i  # u??????????????????ID

            if mincost == float('inf'):
                break

            self.color[u] = Graph.Status.black  # ?????????u???S????±???????????????´???

            for v in range(len(self.d)):
                if self.color[v] != Graph.Status.black and self.M[u][v] != float('inf'):
                    # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????
                    if self.d[u] + self.M[u][v] < self.d[v]:
                        self.d[v] = self.d[u] + self.M[u][v]
                        self.p[v] = u
                        self.color[v] = Graph.Status.gray


def main(args):
    data = []
    # from, to, cost
    data.append([0, 1, 10])
    data.append([1, 2, 10])
    data.append([2, 3, 10])
    data.append([3, 4, 10])
    data.append([4, 5, 10])
    data.append([5, 6, 10])
    data.append([6, 7, 10])
    data.append([7, 8, 10])
    data.append([8, 9, 10])
    data.append([9, 5, 10])
    data.append([5, 4, 10])
    data.append([4, 3, 10])
    data.append([3, 2, 10])
    data.append([2, 1, 10])
    data.append([1, 0, 10])

    n = int(input())
    for _ in range(n):
        start, goal = map(int, input().split())
        g = Graph(data)

        g.dijkstra(start)

        path = [goal]
        u = goal
        while g.p[u] != start:
            path.append(g.p[u])
            u = g.p[u]
        path.append(start)
        path.reverse()

        print(' '.join(map(str, path)))


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
RIGHT_DIRECT = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
LEFT_DIRECT = [6, 7, 8, 9, 5, 4, 3, 2, 1, 0]
ROTATE_DIRECT = [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]

for _ in range(int(input())):

    begin_number, end_number = [int(item) for item in input().split(" ")]

    output = []

    if 5 <= begin_number <= 9 and 5 <= end_number <= 9:

        index = ROTATE_DIRECT.index(begin_number)

        while ROTATE_DIRECT[index] != end_number:
            output.append(ROTATE_DIRECT[index])
            index += 1
        output.append(ROTATE_DIRECT[index])

    elif begin_number < end_number:

        begin_index = RIGHT_DIRECT.index(begin_number)
        end_index = RIGHT_DIRECT.index(end_number)

        for item in RIGHT_DIRECT[begin_index:end_index + 1]:
            output.append(item)

    else:

        begin_index = LEFT_DIRECT.index(begin_number)
        end_index = LEFT_DIRECT.index(end_number)

        for item in LEFT_DIRECT[begin_index:end_index + 1]:
            output.append(item)

    output = [str(item) for item in output]
    print(" ".join(output))

Submission status: Wrong Answer

Solution code:
for i in range(input()):
    s,e=map(int,raw_input().split())
    if 0<=s<=5 and 0<=e<=5:
        if s>e:
            L=range(s,e-1,-1)
        else:
            L=range(s,e+1)
    elif s<=5:
        L=range(s,e+1)
    elif s>=5:
        L=range(s,max(9,e)+1)+range(5,e-1,-1)
    print " ".join(map(str,L))

Submission status: Wrong Answer

Solution code:
for i in range(input()):
    s,e=map(int,raw_input().split())
    if 0<=s<=5 and 0<=e<=5:
        if s>e:
            L=range(s,e-1,-1)
        else:
            L=range(s,e+1)
    else:
        L=range(s,max(9,e)+1)+range(5,e-1,-1)
    print " ".join(map(str,L))

Submission status: Wrong Answer

Solution code:
for i in range(input()):
    s,e=map(int,raw_input().split())
    if 0<=s<=5 and 0<=e<=5:
        if s>e:
            L=range(s,e-1,-1)
        else:
            L=range(s,e+1)
    elif 5<=e<s:
        L=range(s,10)+range(5,e+1)
    else:
        L=range(s,max(9,e)+1)+range(5,e-1,-1)
    print " ".join(map(str,L))

Submission status: Wrong Answer

Solution code:
for i in range(input()):
    s,e=map(int,raw_input().split())
    if 0<=s<=5 and 0<=e<=5:
        if s>e:
            L=range(s,e-1,-1)
        else:
            L=range(s,e+1)
    elif s<e:
        L=range(s,e+1)
    elif 5<=e<s:
        L=range(s,10)+range(5,e+1)
    else:
        L=range(s,max(9,e)+1)+range(5,e-1,-1)
    print " ".join(map(str,L))

Submission status: Wrong Answer

Solution code:
from itertools import product
d = [[[e] if s+1 == e or (s-1 == e and s < 6) else range(10)for e in xrange(10)]
      for s in xrange(10)]
d[9][5] = [5]
for k,i,j in product(xrange(10),repeat=3):
    d[i][j] = min(d[i][j],d[i][k]+d[k][j],key=lambda x:len(x))
for _ in xrange(input()):
    s,e = map(int,raw_input().split())
    print "{} {}".format(str(s)," ".join(map(str,d[s][e])))

Submission status: Wrong Answer

Solution code:
from itertools import product
d = [[[e] if s+1 == e or (s-1 == e and s < 6) else range(11) for e in xrange(10)]
      for s in xrange(10)]
d[9][5] = [5]
for k,i,j in product(xrange(10),repeat=3):
    d[i][j] = min(d[i][j],d[i][k]+d[k][j],key=lambda x:len(x))
for _ in xrange(input()):
    s,e = map(int,raw_input().split())
    print "{} {}".format(str(s)," ".join(map(str,d[s][e])))

Submission status: Wrong Answer

Solution code:
stop="0123456789543210"
for i in range(input()):
	sp,ep=raw_input().split()
	if sp<ep:
		print " ".join(list(stop[stop.find(sp):stop.find(ep)+1]))
	else:
		print " ".join(list(stop[stop.rfind(sp):stop.rfind(ep)+1]))

Submission status: Wrong Answer

Solution code:
p="0123456789543210"
for i in range(input()):
	sp,ep=raw_input().split()
	if sp<ep:
		print " ".join(list(p[p.find(sp) :p.find(ep)+1] ))
	else:
		print " ".join(list(p[p.rfind(sp):p.rfind(ep)+1]))

Submission status: Wrong Answer

Solution code:
p="0123456789543210"
for i in range(input()):
	sp,ep=raw_input().split()
	if sp<ep:
		sp=p.find(sp)
		ep=p.find(ep)+1
	else:
		sp=p.rfind(sp)
		ep=p.rfind(ep)+1
		if ep<=sp:
			print " ".join(list(p[:ep]+p[sp:]))
			continue
	print " ".join(list(p[sp:ep]))

Submission status: Wrong Answer

Solution code:
bus = "0123456789543210123456789"
for i in xrange(input()):
    s, t = raw_input().split()
    left = bus.find(s, 0)
    right = bus.find(t, left)
    print " ".join(bus[left:right+1])

Submission status: Wrong Answer

Solution code:
bus = "0123456789543210123456789"
rbus = "543210"
for i in xrange(input()):
    s, t = raw_input().split()
    if s<=t:
        left = bus.find(s, 0)
        right = bus.find(t, left)
        print " ".join(bus[left:right+1])
    else:
        left = rbus.find(s, 0)
        right = rbus.find(t, left)
        print " ".join(rbus[left:right+1])

Submission status: Accepted

Solution code:
for _ in [0]*input():
  A = '012345678954321'
  a, b = raw_input().split()
  n = 15
  for _ in [0]*A.count(a):
    I = A.index(a)
    A = A[I:]+A[:I]
    I = A.index(b)
    if n>I:
      n = I
      x = A[:I+1]
    A = A[1:]+A[0]
  for e in x:
    print e,
  print

Submission status: Accepted

Solution code:
for _ in [0]*input():
  A = "012345678954321"
  a, b = raw_input().split()
  n = 15
  for _ in [0]*A.count(a):
    I = A.index(a)
    A = A[I:]+A[:I]
    I = A.index(b)
    if n>I:
      n = I
      x = A[:I+1]
    A = A[1:]+A[0]
  print " ".join(x)

Submission status: Accepted

Solution code:
for _ in [0]*input():
  A = "012345678954321"
  A += A
  a, b = raw_input().split()
  i1 = A.index(a)
  i2 = A.index(a,i1+1)
  i3 = A.index(b,i1)
  if i2<i3: i1=i2
  print " ".join(A[i1:i3+1])

Submission status: Accepted

Solution code:
for i in range(input()):
    s, g = map(int, raw_input().split())
    if s < g:
        res = range(s, g+1)
    elif 0 <= s <= 5 and 0 <= g <= 5:
        res = range(s, g-1, -1)
    else:
        res = range(s, 10)
        if 0 <= g <= 5:
            res.extend(range(5, g-1, -1))
        else:
            res.extend(range(5, -1, -1))
            res.extend(range(1, g+1))
    print ' '.join(map(str, res))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os

N = int(input())

for i in range(N):
    a, b = map(int, input().split())
    if a < b:
        lst = [j for j in range(a, b+1)]
        print(*lst)

    # a > b
    else:
        # easy
        if a <= 5:
            lst = list(range(a, b - 1, -1))
        elif b <= 5:
            lst = list(range(a, 10)) + list(range(5, b - 1, -1))

        # i.e. 7 -> 6
        else:
            lst = [j for j in range(a, 9 + 1)] + [j for j in range(5, 0, -1)] + list(range(0, b+1))
        print(*lst)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0140

"""
import sys
from sys import stdin
input = stdin.readline



def solve(start, goal):
    path = []                   #  ?????????n???????????????????????????????????????
    # ????????????????????¨????????´?????¨????????????????????¨????????´????????°??????
    path.append([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], []])
    path.append([[1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [1, 0]])
    path.append([[2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [2, 1, 0]])
    path.append([[3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [3, 2, 1, 0]])
    path.append([[4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [4, 3, 2, 1, 0]])
    path.append([[5, 6, 7, 8, 9, 5, 4, 3, 2], [5, 4, 3, 2, 1, 0]])
    path.append([[6, 7, 8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5], []])
    path.append([[7, 8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6], []])
    path.append([[8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7], []])
    path.append([[9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8], []])

    path_a = [0]*100            #  ???????????????????????????
    path_b = [0]*100
    try:
        path_a = path[start][0][:path[start][0].index(goal)+1]
    except ValueError:
        pass
    try:
        path_b = path[start][1][:path[start][1].index(goal)+1]
    except ValueError:
        pass

    if len(path_a) < len(path_b):
        return ' '.join(map(str, path_a))
    else:
        return ' '.join(map(str, path_b))


def main(args):
    n = int(input())
    for _ in range(n):
        start, goal = map(int, input().split())
        result = solve(start, goal)
        print(result)






if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0140

"""
import sys
from sys import stdin
input = stdin.readline



def solve(path, start, goal):
    path_a = [0]*100            #  ???????????????????????????
    path_b = [0]*100

    # ?????????????????¨??????????????????????????????????????°?????§??????????????¢???
    try:
        path_a = path[start][0][:path[start][0].index(goal)+1]
    except ValueError:
        pass
    try:
        path_b = path[start][1][:path[start][1].index(goal)+1]
    except ValueError:
        pass

    # ??????????????????????±???????????????????????????§???start??????goal?????§????????????????????§1??????????????????
    if len(path_a) < len(path_b):
        return ' '.join(map(str, path_a))
    else:
        return ' '.join(map(str, path_b))


def main(args):
    path = []                   #  ?????????n???????????????????????????????????????
    # ?????????[n]???????????????????????????????????????????????¨????????´?????¨??????????????????????????¨????????´????????°??????
    path.append([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], []])                #  0????????????
    path.append([[1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [1, 0]])   #  1
    path.append([[2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [2, 1, 0]])   #  2
    path.append([[3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [3, 2, 1, 0]])   #  3
    path.append([[4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [4, 3, 2, 1, 0]])   #  4
    path.append([[5, 6, 7, 8, 9, 5, 4, 3, 2], [5, 4, 3, 2, 1, 0]])   #  5
    path.append([[6, 7, 8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5], []]) #  6
    path.append([[7, 8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6], []]) #  7
    path.append([[8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7], []]) #  8
    path.append([[9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8], []]) #  9

    n = int(input())
    for _ in range(n):
        start, goal = map(int, input().split())
        result = solve(path, start, goal)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# Aizu Problem 00140: Bus Line
#
import sys, math, os, copy

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def bus_line(a, b):
    line = []
    if max(a, b) <= 5:
        if a < b:
            return list(range(a, b + 1))
        else:
            return list(range(a, b - 1, -1))
    if a <= 5:
        # stop must be in circle:
        return list(range(a, b + 1))
    if b <= 5:
        # start must be in circle:
        return list(range(a, 10)) + list(range(5, b - 1, -1))
    # at this point both start and stop are in circle:
    if a < b:
        return list(range(a, b + 1))
    else:
        return list(range(a, 10)) + [5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5] + \
               list(range(6, b + 1))

    
n = int(input())
for __ in range(n):
    a, b = [int(_) for _ in input().split()]
    print(' '.join([str(_) for _ in bus_line(a, b)]))

Submission status: Accepted

Solution code:
def printAns(ans):
    for i in range(len(ans)):
        if i == 0:
            print(ans[i], end="")
        else:
            print(" ", end="")
            print(ans[i], end="")
    print("")

N = int(input())

for l in range(N):
    a,b = [int(i) for i in input().split()]

    ans = []
    if a < b:
        for i in range(a, b+1):
            ans.append(i)
    else: # a > b
        if a <= 5:
            for i in range(a, b-1, -1):
                ans.append(i)
        else:
            if b <= 5:
                for i in range(a, 10):
                    ans.append(i)
                for i in range(5, b-1, -1):
                    ans.append(i)
            else:
                for i in range(a, 10):
                    ans.append(i)
                for i in range(5, 0, -1):
                    ans.append(i)
                for i in range(b+1):
                    ans.append(i)

    printAns(ans)

Submission status: Accepted

Solution code:
dic = {}
for i in range(10):
  for j in range(10):
    if i < j:
      dic[(i, j)] = list(range(i, j + 1))
    elif i > j:
      if i <= 5:
        dic[(i, j)] = list(range(i, j - 1, -1))
      elif j <= 5:
        dic[(i, j)] = list(range(i, 10)) + list(range(5, j - 1, -1))
      else:
        dic[(i, j)] = list(range(i, 10)) + list(range(5, -1, -1)) + list(range(1, j + 1))


n = int(input())
for _ in range(n):
  s, g = map(int, input().split())
  print(*dic[(s, g)])

Submission status: Accepted

Solution code:
# AOJ 0140 Bus Line
# Python3 2018.6.23 bal4u

for i in range(int(input())):
	s, e = map(int, input().split())
	a = []
	if s <= 5:
		if s > e: [a.append(i) for i in range(s, e-1, -1)]
		else: [a.append(i) for i in range(s, e+1)]
	elif s < e: [a.append(i) for i in range(s, e+1)]
	else:
		[a.append(i) for i in range(s, 10)]
		if e <= 5: [a.append(i) for i in range(5, e-1, -1)]
		else:
			[a.append(i) for i in range(5, 0, -1)]
			[a.append(i) for i in range(0, e+1)]
	print(*a)

Submission status: Accepted

Solution code:
L="".join(map(str,range(6,10)+range(5,0,-1)+range(10)))
for i in range(input()):
    s,e=raw_input().split()
    p1=L.index(s)
    p3=L.rindex(s)
    p2=L.index(e,p1+1)
    if p3>p2:p3=p1
    print " ".join(map(str,L[p3:p2+1]))

Submission status: Accepted

Solution code:
from itertools import product
d = [[[e] if s+1 == e or (s-1 == e and s < 6) else range(11) for e in xrange(10)]
      for s in xrange(10)]
d[9][5] = [5]
for k,i,j in product(xrange(10),repeat=3):
    d[i][j] = min(d[i][j],d[i][k]+d[k][j],key=lambda x:len(x))
for s in xrange(7,10):
    for e in xrange(6,s):
        d[s][e] = d[s][0] + range(1,e+1)
for _ in xrange(input()):
    s,e = map(int,raw_input().split())
    print "{} {}".format(str(s)," ".join(map(str,d[s][e])))

Submission status: Accepted

Solution code:
p="0123456789543210"
for i in range(input()):
	sp,ep=raw_input().split()
	if sp<ep:
		sp=p.find(sp)
		ep=p.find(ep)+1
	else:
		sp=p.rfind(sp)
		ep=p.rfind(ep)+1
	print " ".join(list(p[sp:ep])) if sp<ep else " ".join(list(p[sp:]+p[1:ep]))

Submission status: Accepted

Solution code:
p="0123456789543210"
for i in range(input()):
	sp,ep=raw_input().split()
	(sp,ep)=(p.find(sp),p.find(ep)+1) if sp<ep else (p.rfind(sp),p.rfind(ep)+1)
	print " ".join(list(p[sp:ep])) if sp<ep else " ".join(list(p[sp:]+p[1:ep]))

Submission status: Accepted

Solution code:
p="0123456789543210"
for i in range(input()):
	sp,ep=raw_input().split()
	(sp,ep)=(p.find(sp),p.find(ep)+1) if sp<ep else (p.rfind(sp),p.rfind(ep)+1)
	print " ".join(p[sp:ep]) if sp<ep else " ".join(p[sp:]+p[1:ep])

Submission status: Accepted

Solution code:
bus = [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]
for i in xrange(input()):
    s, t = map(int, raw_input().split())
    if s>5:
        print " ".join(map(str, bus[s:bus.index(t, s)+1]))
    else:
        if s<t:
            print " ".join(map(str, xrange(s, t+1)))
        else:
            print " ".join(map(str, xrange(s, t-1, -1)))

Submission status: Accepted

Solution code:
bus = "0123456789543210123456789"
rbus = "543210"
for i in xrange(input()):
    s, t = raw_input().split()
    if t<s<'6':
        left = rbus.find(s, 0)
        right = rbus.find(t, left)
        print " ".join(rbus[left:right+1])
    else:
        left = bus.find(s, 0)
        right = bus.find(t, left)
        print " ".join(bus[left:right+1])

Submission status: Accepted

Solution code:
def line(i,j):
    retval=[]
    if i<=5 and j<=5:
        if i<j:
            for k in range(i,j+1):
                retval.append(k)
        else:
            for k in range(i,j-1,-1):
                retval.append(k)

    elif i<=5 and j>5:
        for k in range(i,j+1):
            retval.append(k)

    elif i>5 and j>5:
        if i<j:
            for k in range(i,j+1):
                retval.append(k)
        else:
            for k in range(i,10):
                retval.append(k)
            retval+=[5,4,3,2,1,0,1,2,3,4,5]
            for k in range(6,j+1):
                retval.append(k)
    else:
        for k in range(i,10):
            retval.append(k)
        for k in range(5,j-1,-1):
            retval.append(k)


    return " ".join([str(i) for i in retval])

N=int(input())
for i in range(N):
    print(line(*[int(j) for j in input().split(" ")]))

Submission status: Accepted

Solution code:
for i in range(int(input())):
    s, e = map(int, input().split())
    a = []
    if s <= 5:
        if s > e:
            [a.append(i) for i in range(s, e - 1, -1)]
        else:
            [a.append(i) for i in range(s, e + 1)]
    elif s < e:
        [a.append(i) for i in range(s, e + 1)]
    else:
        [a.append(i) for i in range(s, 10)]
        if e <= 5:
            [a.append(i) for i in range(5, e - 1, -1)]
        else:
            [a.append(i) for i in range(5, 0, -1)]
            [a.append(i) for i in range(0, e + 1)]
    print(*a)

Submission status: Accepted

Solution code:
# pythonっぽくなくてダサい
stops=[0,1,2,3,4,5,6,7,8,9,5,4,3,2,1]
istops=[[0],[1,14],[2,13],[3,12],[4,11],[5,10],[6],[7],[8],[9]]

def asloop(cur, end):
	result=[]
	for i in range(cur, cur+len(stops)):
		x=stops[i%len(stops)]
		result.append(x)
		if x==end:
			return	result

def asrecurse(cur, end, result=None):
	if result is None:
		result=[]
	x=stops[cur]
	result.append(x)
	next=(cur+1)%len(stops)
	if x==end:
		return	result
	return asrecurse(next, end, result)

def loop(start, end):
	result=[i for i in range(len(stops)+1)]
	for i in istops[start]:
		a=asloop(i, end)
		if len(a) < len(result):
			result=a
	return	result

nloop=int(input())
for i in range(nloop):
	inputs=input().split()
	result=loop(int(inputs[0]), int(inputs[1]))
	print(' '.join([str(x) for x in result]))

Submission status: Accepted

Solution code:
n = int(input())

for _ in range(n):
    start, end = map(int, input().split())

    anslist = []
    if start <= 5:
        if start > end:
            [anslist.append(i) for i in range(start, end-1, -1)]
        else:
            [anslist.append(i) for i in range(start, end+1)]
    elif start < end:
        [anslist.append(i) for i in range(start, end+1)]
    else:
        [anslist.append(i) for i in range(start, 10)]
        if end <= 5:
            [anslist.append(i) for i in range(5, end-1, -1)]
        else:
            [anslist.append(i) for i in range(5, 0, -1)]
            [anslist.append(i) for i in range(0, end+1)]

    print(' '.join(str(ans) for ans in anslist))

Submission status: Accepted

Solution code:
A = "012345678954321012345678"
B = "6789543210123456789"
n = int(input())
for i in range(n):
    a, b = input().split()
    if int(a) < int(b):
        print(" ".join(A[A.index(a):A.index(b,A.index(a))+1]))
    else:
        print(" ".join(B[B.index(a):B.index(b,B.index(a))+1]))

Submission status: Runtime Error

Solution code:
stop=[0,1,2,3,4,5,6,7,8,9,5,4,3,2,1,0]
for i in range(input()):
	sp,ep=map(int,raw_input().split())
	if sp<ep:
		print stop[stop.index(sp):stop.index(ep)+1]
	else:
		print stop[stop.rindex(sp):stop.rindex(ep)+1]

Submission status: Runtime Error

Solution code:
bus = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2, 1, 0]
for i in xrange(input()):
    s, t = map(int, raw_input().split())
    if s>5:
        print " ".join(map(str, bus[s:bus.index(t, s)+1]))
    else:
        if s<t:
            print " ".join(map(str, range(s, t+1)))
        else:
            print " ".join(map(str, xrange(s, t-1, -1)))

Submission status: WA: Presentation Error

Solution code:
# AOJ 0140 Bus Line
# Python3 2018.6.23 bal4u

for i in range(int(input())):
	s, e = map(int, input().split())
	if s <= 5:
		if s > e: [print(i, end=' ') for i in range(s,e-1,-1)]
		else: [print(i, end=' ') for i in range(s,e+1)]
	elif s < e: [print(i, end=' ') for i in range(s,e+1)]
	else:
		ans = []
		for j in range(s, 10): ans.append(j)
		if e <= 5:
			for j in range(5, e-1, -1): ans.append(j)
		else:
			for j in range(5, 0, -1): ans.append(j)
			for j in range(0, e+1): ans.append(j)
		print(*ans, end='')
	print()