|
- #include <cstdio>
- #include <vector>
- const int N = 100010;
- int stak[N], top, p; /// 模拟系统栈
- std::vector<int> v;
- inline void call(int x, int add) {
- int pre_top = top;
- stak[++top] = x;
- stak[++top] = add;
- stak[++top] = pre_top;
- p = 0;
- return;
- }
- inline void ret() {
- p = stak[top - 1];
- top = stak[top];
- return;
- }
- int main() {
- int n, m;
- scanf("%d%d", &n, &m);
- call(1, 0);
- while(top) {
- int x = stak[top - 2];
- if(p == 0) {
- if(v.size() == m) {
- for(int i = 0; i < v.size(); i++) {
- printf("%3d", v[i]);
- }
- printf("\n");
- ret(); /// return;
- continue;
- }
- if(x == n + 1 || v.size() + (n - x + 1) < m) {
- ret(); /// return;
- continue;
- }
- v.push_back(x);
- call(x + 1, 1); /// DFS(x + 1);
- continue;
- }
- else if(p == 1) {
- v.pop_back();
- call(x + 1, 2); /// DFS(x + 1);
- continue;
- }
- else {
- ret(); /// return;
- continue;
- }
- }
- return 0;
- }
复制代码 |
|