第一行输入一个整数
代表火车的数量。
第二行输入
个整数
代表火车的入站顺序。
输出若干行,每行输出
个整数,代表一种出站顺序。你需要按照字典序从小到大依次输出。
3 1 2 3
1 2 3 1 3 2 2 1 3 2 3 1 3 2 1
在这个样例中,每一种出栈顺序的详细出入站状况为(黑色普通字体代表入站、橙色加粗字体代表出站):
;
;
;
;
。
def helper(trains, stack, output): global total if not trains and not stack: total.append(output) return if stack: t = stack.pop() helper(trains, stack, output+t) stack.append(t) if trains: stack.append(trains.pop()) helper(trains, stack, output) trains.append(stack.pop()) while True: try: n = int(input()) trains = input().split()[::-1] stack, total = [], [] helper(trains, stack, "") for s in sorted(total): print(" ".join(s)) except: break
all_res=[] def solver(prefix,my_stack,remain): global all_res if remain==[]: if not my_stack: all_res.append(prefix[:]) return else: while my_stack: prefix.append(my_stack.pop()) all_res.append(prefix[:]) return if not my_stack: solver(prefix[:],[remain[0]], remain[1:]) else: solver(prefix[:], my_stack[:]+[remain[0]], remain[1:]) while my_stack: prefix.append(my_stack.pop()) solver(prefix[:], my_stack[:]+[remain[0]], remain[1:]) while True: try: all_res=[] number=int(input()) remain=input().split() solver([], [], remain) all_res.sort() for string in all_res: print(' '.join(string)) except: break
import itertools while True: try: n = input() l = input().split(" ") all = itertools.permutations(l) allc = [] for out in all: b = l.copy() station = [] bool = False for car in out: if car in station and car != station.pop(): # allc.remove(out) bool = True # print(out, "撞车了") break station = b[:b.index(car)] b.remove(car) # print(f"{car}号小火车走了,站内还有{station}") if not bool: allc.append(" ".join(out)) allc.sort() print("\n".join(allc)) except: break
def cal(x,s): if x == 1: return [[s[0],s[0]]] #只有一辆车时递归终止 else: res = [] for i in cal(x-1,s[0:x-1]): add = i.index(s[x-2]) #获得x-1辆车的操作序列中第x-1辆车编号第一次出现时的下标 for j in range(add+1,len(i)+1): #在第x-1辆车编号第一次出现之后的任意位置均可插入连续的第x辆车编号获得新序列 temp = i[:] temp.insert(j,s[x-1]) temp.insert(j,s[x-1]) res.append(temp) return res函数的输入参数x和s分别表示的是车的数量和进站的编号序列,递归的重点是车只有一辆时,只有进站出站一种排列方式,对于车有x辆时,由开头得出的两个结论,x辆车时的操作序列可以由x-1辆车时的操作序列中插入连续的第x辆车编号来获得。
while True: try: n = int(input()) sou = [int(x) for x in input().split()] ans = cal(n,sou) #得到操作序列 for i in ans: for j in range(1,n+1): i.remove(j) #删除每辆车编号的第一次出现 ans.sort() for i in ans: print(' '.join([str(x) for x in i])) except: break
明晰易懂的递归实现
''' 栈 火车出站问题 恭喜你通过本题 运行时间:29ms 占用内存:3920k ''' def station_scedule(wait_in, wait_out, leave): if (not wait_in) and (not wait_out): train_out.append(' '.join(leave)) if wait_in: wait_in_ = wait_in[:] wait_out_ = wait_out[:] leave_ = leave[:] wait_out_.append(wait_in_.pop(0)) station_scedule(wait_in_, wait_out_, leave_) if wait_out: wait_in_ = wait_in[:] wait_out_ = wait_out[:] leave_ = leave[:] leave_.append(wait_out_.pop()) station_scedule(wait_in_, wait_out_, leave_) while True: try: N, train_in = int(input()), input().strip().split(' ') train_out = [] leave = [] station_scedule(train_in, [], leave) for i in sorted(train_out): print(i) except: break