Algorithm training arrangement problem

 

Problem description

Find a 0-N-1 permutation (that is, each number can only appear once), and give the restrictions (a table of N*N, 1 or 0 in row i, column j, expressed as j-1, which cannot appear after the number i-1, and ensure that row i, column i, is 0). Treat this permutation as a natural number, and find the K permutation from small to large.

Data size and engagement

  N<=10,K<=500000

Input format

The first line is n and K, and the next N lines are n lines. 0 means no, 1 means yes

Output format

Arrangement required

sample input

<span style="color:#333333">3 2
0 1 1
1 0 0
0 1 0
</span>

 

 

 

sample output

 

 

1 0 2
 Interpretation:
There is no limit for N=3
 First: 0 1 2
 Second: 0 2 1
 Third: 1 0 2
 Fourth: 1 20
 Fifth: 2001
 Sixth: 21 0
 According to the restrictions given by the title, because 2 cannot appear after 1, 0 cannot appear after 2
 First: 0 2 1
 Second: 1 0 2
 Third: 2 1 0

 

#include<bits/stdc++.h>
using namespace std;

int a[11][11];
int n, k;
int b[10];
struct{
    int i;
    int j;
}c[101];
int cnt = 0;
int main()
{
    cin >> n >> k;

    for(int i = 1; i <= n; i ++)
    for(int j = 1; j <= n; j ++)
    cin >> a[i][j];

    for(int i = 0; i < 10; i ++)
    b[i] = i;

    for(int i = 1; i <= n; i ++)
    for(int j = 1; j <= n; j ++)
    {
        if(i == j || a[i][j] == 1)
        continue;

        int t1,t2;
        t1 = j-1;
        t2 = i-1;
        c[cnt].i = t1;
        c[cnt].j = t2;
        cnt ++;
//        cout << t1 << " " << t2 << endl; 
    }
    int num = 0;
    do{
        int flag = 0;
        for(int i = 0; i < cnt; i ++)
        {
            int l1, l2;
            for(int j = 0; j < n; j ++)
            {//Unqualified, flag = 1
			            if(b[j] == c[i].j && b[j+1] == c[i].i)
								flag = 1;}

             if(flag)
                 break;
        }
        if(flag == 0)
        num ++;
        if(num == k)
        {
            for(int p = 0;p < n;p ++)
            {
                if(p)
                cout << " ";
                cout << b[p];
            }
            return 0;
        }
    }while(next_permutation(b, b+n));
    return 0;
}

 

Added by advancedfuture on Thu, 02 Jan 2020 22:02:48 +0200