蔡子经数据结构1.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63


#define MAXN 100

int Stack[MAXN];
int top;
int n;
int que[MAXN];//存储车厢
int res[MAXN];
//我入栈的时候没有检测栈是否满了
//出栈的时候也没有检测栈是否为空
void ()
{
top = 0;
}

void Push(int x)
{
Stack[top++] = x;
}

int Pop()
{
return Stack[--top];
}

int IsEmpty()
{
return top == 0;
}
//1表示入栈,0表示出栈
void dfs(int ptr, int lim)
{
if(ptr == n && IsEmpty())
{
for(int i = 0; i < lim; ++i)
printf("%d ",res[i]);
puts("");
return ;
}
if(ptr < n)//如果车队还有车,就入栈
{
Push(que[ptr]);
dfs(ptr+1, lim);
Pop();
}
if(!IsEmpty())//如果栈不空,就出栈
{
res[lim] = Pop();
dfs(ptr, lim+1);
Push(res[lim]);
}
}

int main()
{
initStack();
scanf("%d",&n);
for(int i = 0; i < n; ++i)
scanf("%d",&que[i]);
dfs(0, 0);//1表示入栈操作
return 0;
}