给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号,火车站只有一个方向进出,同时停靠在火车站的列车中,只有后进站的出站了,先进站的才能出站。
要求输出所有火车出站的方案,以字典序排序输出。
数据范围:
进阶:时间复杂度:,空间复杂度:
第一行输入一个正整数N(0 < N <= 10),第二行包括N个正整数,范围为1到10。
输出以字典序从小到大排序的火车出站序列号,每个编号以空格隔开,每个输出序列换行,具体见sample。
3 1 2 3
1 2 3 1 3 2 2 1 3 2 3 1 3 2 1
第一种方案:1进、1出、2进、2出、3进、3出 第二种方案:1进、1出、2进、3进、3出、2出 第三种方案:1进、2进、2出、1出、3进、3出 第四种方案:1进、2进、2出、3进、3出、1出 第五种方案:1进、2进、3进、3出、2出、1出 请注意,[3,1,2]这个序列是不可能实现的。
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