递归的循环实现

 1 vector<int> chosen;
 2 int stack[100010],top=0,address=0,n,m;
 3 
 4 void call(int x,int ret_addr)
 5 {
 6     int old_top=top;
 7     stack[++top]=x;
 8     stack[++top]=ret_addr;
 9     stack[++top]=old_top;
10 }
11 
12 int ret()
13 {
14     address=stack[top-1];
15     top=stack[top];
16 }
17 
18 int main()
19 {
20     cin>>n>>m;
21     call(1,0);
22     while(top)
23     {
24         int x=stack[top-2];
25         switch(address)
26         {
27             case 0:
28                 if(chosen.size()>m||chosen.size()+(n-x+1)<m)
29                 {ret();continue;}
30                 if(x==n+1)
31                 {
32                     for(int i=0;i<chosen.size();++i)
33                         printf("%d ",chosen[i]);
34                     printf("
");
35                     ret();
36                     continue;
37                 }
38                 call(x+1,1);
39                 address=0;
40                 continue;
41             case 1:
42                 chosen.push_back(x);
43                 call(x+1,2);
44                 address=0;
45                 continue;
46             case 2:
47                 chosen.pop_back();
48                 ret();
49         }
50     }
51 }

CH0201 费解的开关 http://contest-hunter.org:83/contest/0x00%E3%80%8C%E5%9F%BA%E6%9C%AC%E7%AE%97%E6%B3%95%E3%80%8D%E4%BE%8B%E9%A2%98/0201%20%E8%B4%B9%E8%A7%A3%E7%9A%84%E5%BC%80%E5%85%B3

枚举第一行的点击方案,递推后面的

原文地址:https://www.cnblogs.com/universeplayer/p/10655104.html