给定一个正整数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 updateTrainExitSequence(train_outside,train_inside,all_exit_way,exit_way_arr): if(train_outside): enter_train = train_outside.pop(0) train_inside.append(enter_train) updateTrainExitSequence(train_outside,train_inside,all_exit_way,exit_way_arr) train_inside.pop() train_outside.insert(0,enter_train) if(train_inside): exit_train = train_inside.pop() exit_way_arr.append(exit_train + " ") updateTrainExitSequence(train_outside,train_inside,all_exit_way,exit_way_arr) exit_way_arr.pop() train_inside.append(exit_train) if(not train_outside and not train_inside): all_exit_way.append("".join(exit_way_arr)) train_num = int(input()) train_outside = input().split() all_exit_way = [] updateTrainExitSequence(train_outside,[],all_exit_way,[]) all_exit_way.sort() for exit_way in all_exit_way: print(exit_way)
该代码核心思想参考其它大神(并非自创),我这边大概写一下我理解的思路 n, nums = input(), input().split() res = [] def dfs(w, i, o): # 如果没有 待需要进栈的火车 和 需要出栈的火车,证明所有火车都已出栈 # 需要将都已出栈的火车添加到res if not w and not i: res.append(' '.join(map(str, o))) # 如果有 待需要进栈的火车,那么则安排一辆火车进栈 if w: dfs(w[1:], i + [w[0]], o) #如果有进栈的火车,那么可以安排火车出栈 if i: dfs(w, i[:-1], o + [i[-1]]) dfs(nums, [], []) #sorted 按照题目要求,字典序输出 for i in sorted(res): print(i)
n=input().strip() in_order=input().strip().split(' ') res=[] def in_out(station,waiting,out_station): if waiting: in_out(station+[waiting[0]],waiting[1:],out_station) if station: in_out(station[:-1],waiting,out_station+[station[-1]]) if not station and not waiting: res.append(out_station) in_out([],in_order,[]) res.sort() for i in res: print(*i)
n = input() ss = input().split() ss.reverse() ou = [] def cel(s, i, o): if not s and not i: ou.append(o) return if s: on = o.copy() sn = s.copy() sn.pop() inew = i.copy() inew.append(s[-1]) cel(sn, inew, on) if i: on = o.copy() sn = s.copy() inew = i.copy() inew.pop() on.append(i[-1]) cel(sn, inew, on) cel(ss, [], []) ou.sort() for i in ou: for j in i: print(j, end=' ') print('')
#此题动态规划解题,等候入站的序列和出站的序列不为空都会有 对应的入站和出站,边界为两者都为空时,将此时用于装出站序列的out列表 加入到出站方案列表中。 def trail(wait,stack,out): if not wait and not stack:#边界 l.append(' '.join(map(str,out))) else: if wait:#入站 trail(wait[1:],stack+[wait[0]],out) if stack:#出站 trail(wait,stack[:-1],out+[stack[-1]]) while True: try: l = [] n = int(input()) wait = list(map(int,input().split())) trail(wait,[],[]) l = sorted(l)#排序 for i in l: print(i) except: break
n=int(input()) s=list(map(int,input().split(' '))) def inout(l:list): myl=[] if len(l)==1: myl.append(l) else: for i in range(1,len(l)): l1=inout(l[:i]) l2=inout(l[i:]) tmp=l[:i][::-1] for i in l1: for j in l2: k=i+j if k in myl: continue else: myl.append(k) for j in l2: k=j+tmp if k in myl: continue else: myl.append(k) return(myl) out=inout(s) out=sorted(out) for i in out: i=list(map(str,i)) print(' '.join(i))
小白答案都得看几十遍 while True: try: n = int(input()) trains = input().strip().split(" ") res = [] def rec_trains(cur_index, in_trains, out_trains): # 最后一个火车已经入站,此时只能出站 print( cur_index,in_trains,out_trains,'/') if trains[-1] in in_trains: res.append(" ".join(out_trains + in_trains[::-1])) # print(res,cur_index) return # 没有火车入站,此时只能入站 elif in_trains == []: rec_trains(cur_index + 1, in_trains + [trains[cur_index]], out_trains) else: # 出站,当前车站的最后一个火车出战 rec_trains(cur_index, in_trains[:-1], out_trains + [in_trains[-1]]) # print(cur_index) # 入站,当前的火车入站 rec_trains(cur_index + 1, in_trains + [trains[cur_index]], out_trains) rec_trains(0, [], []) res.sort() print("\n".join(res)) except: break
python DFS就是香
class Solution: def __init__(self): self.ans = [] def dfs(self, wait, stack, leave): if len(wait) == 0 and len(stack) == 0: self.ans.append(leave) if len(wait) > 0: # 从等待队列中 入栈 self.dfs(wait[1:], stack + [wait[0]], leave[:]) if len(stack) > 0: # 出栈 self.dfs(wait[:], stack[:-1], leave + [stack[-1]]) return self.ans # 处理一些输入输出 if __name__ == '__main__': while True: try: n = input() nums = list(map(int, input().split())) sol = Solution() ret = sol.dfs(nums, [], []) for line in sorted(ret): print(" ".join(map(str, line))) except: break