toj 3072 Train Order

来源:互联网 发布:匿名内部类java构造器 编辑:程序博客网 时间:2024/04/19 08:41
<center><span size="+2" style="">3072.</span>   <span size="+2" style="color:blue;">Train Order</span> <hr /><span style="color:green;">Time Limit:</span> 1.0 Seconds   <span style="color:green;">Memory Limit: </span>65536K<span style="color:green;">Total Runs: </span>435   <span style="color:green;">Accepted Runs: </span>244</center><hr /><span lang="en"></span><div id="problem">There is a railway station in PopPush City (See the picture below). All the trains arriving from A are numbered 1,2,3,...<em>N</em>. The station is dead-end, and some trains may stop here temporarily to let the behind trains pass by. Please note the trains cannot go backward, that is, once they enter the station, they cannot return to the direction A, and once they left the station to direction B, they cannot return the station too. <p></p><p align="center"><img src="http://acm.tju.edu.cn/acm/image/exam/1048_train.gif" alt="" /></p>Now your task is, given <em>N</em>, output all the possible sequence when all the trains left the station. Each sequence should be represented as a string containing only 1,2,3...<em>N</em>. And the string should be sorted lexicographically.<p></p><h3>Input</h3>The first line is an integer <em>T</em>, the number of test cases. Then <em>T</em> cases follows.<p>Each case contains only one number <em>N</em> in one line. You can assume 1 ≤ <em>N</em> ≤ 9.</p><p></p><h3>Output</h3>Output all the possible sequences for each test case. Each line contains one sequence.<p></p><h3>Sample Input</h3><tt></tt><pre>223

Sample Output

1221123132213231321
#include<stdio.h>#include<algorithm>#include<stack>using namespace std;int mark[15];int a[15],b[15];int judge(int b[],int n){stack<int>s;int i,k=0;for(i=1;i<=n;i++) { s.push(i); if(i==b[k]) { while(!s.empty()&&b[k]==s.top()) { s.pop(); k++; }} } if(s.empty()) return 1;else return 0;}int main(){int T;int n;scanf("%d",&T);for(int i=0;i<=10;i++) a[i]=i+1;while(T--){scanf("%d",&n);sort(a,a+n);do{if(judge(a,n)){for(int i=0;i<n;i++) printf("%d",a[i]);printf("\n");}}while(next_permutation(a,a+n));}return 0;}

0 0