Problem:
Permutation Enumeration

For given an integer $n$, print all permutations of $\{1, 2, ..., n\}$ in lexicographic order.

Input

An integer $n$ is given in a line.

Output

Print each permutation in a line in order. Separate adjacency elements by a space character.

Constraints

$1 \leq n \leq 9$

Sample Input 1

2

Sample Output 1

1 2
2 1

Sample Input 2

3

Sample Output 2

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Solution in C (judge verdict: Accepted)

#include <stdio.h>

void dfs(char used[],char result[],int num,int n){
    int i;
    
    if(num==n){
        for(i=0;i<n-1;i++)printf("%d ",result[i]);
        printf("%d\n",result[i]);
        return;
    }
    
    for(i=1;i<=n;i++){
        if(used[i]==1)continue;
        
        result[num]=i;
        used[i]=1;
        
        dfs(used,result,num+1,n);
        
        used[i]=0;
    }
}
int main(){
    int i;
    
    int n;
    scanf("%d\n",&n);
    
    char used[n];
    for(i=0;i<n;i++)used[i]=0;
    char result[n];
    
    dfs(used,result,0,n);
    
    return 0;
}