zoukankan      html  css  js  c++  java
  • 90 排列问题

    90 排列问题

    作者: Turbo时间限制: 1S章节: 深度优先搜索

    问题描述 :

      求一个0~N-1的排列(即每个数只能出现一次),给出限制条件(一张N*N的表,第i行第j列的1或0,表示为j这个数能不能出现在i这个数后面,并保证表中的第i行第i列为0,i和j都从0开始),将这个排列看成一个自然数,求从小到大排序第K个排列。

    样例输入

    3 2

    0 1 1

    1 0 0

    0 1 0

    样例输出

    1 0 2

    解释:

    对于N=3的没有任何限制的排列如下:

    第一:0 1 2

    第二:0 2 1

    第三:1 0 2

    第四:1 2 0

    第五:2 0 1

    第六:2 1 0

    根据题目所给的限制条件由于2不能出现在1后面,0不能出现在2后面

    第一:0 2 1

    第二:1 0 2

    第三:2 1 0

    输入说明 :

      第一行为N和K,接下来的N行,每行N个数,0表示不能,1表示能

       N<=10,K<=500000
    

    输出说明 :

      所求的排列

    输入范例 :
    3 2
    0 1 1
    1 0 0
    0 1 0
    输出范例 :
    1 0 2

    #include <iostream>
    using namespace std;
    const int maxn = 11;
    int p[maxn], hashtable[maxn] = { 0 };
    int mp[maxn][maxn] = {0};
    int n, k;
    int num = 0;
    void dfs(int index)
    {
    	if (index == n)
    	{
    		num++;
    		if (num == k)
    		{
    			for (int i = 0; i < n; i++)
    			{
    				cout << p[i] ;
    				if (i < n-1)
    				{
    					cout << " ";
    				}
    			}
    			cout << endl;
    		}
    		return;
    	}
    
    	for (int x = 0; x <n; x++)
    	{
    		if (hashtable[x] == 0&&mp[p[index-1]][x]==1||index==0)
    		{
    			hashtable[x] = 1;
    			p[index] = x;
    			dfs(index + 1);
    			hashtable[x] = 0;
    		}
    	}
    }
    
    int main()
    {
    	cin >> n >> k;
    	for (int i = 0; i < n; i++)
    	{
    		for (int j = 0; j < n; j++)
    		{
    			cin >> mp[i][j];
    		}
    	}
    	dfs(0);
    	return 0;
    }
    
    
    Yesterday is history,tomorrow ismystery,but today is a gift!That why it is called Present!
  • 相关阅读:
    HDU 5154 Harry and Magical Computer bfs
    opencv第一课 打开一个图片
    Codeforces Round #131 (Div. 1) A
    Topcoder SRM 643 Div1 250<peter_pan>
    bestcoder#23 1002 Sequence II 树状数组+DP
    bestcoder#23 1001 Sequence
    Oil Deposits 搜索 bfs 强联通
    迷宫问题 模拟队列 广度优先搜索
    Codeforces Round #283 (Div. 2) C. Removing Columns 暴力
    Codeforces Round #283 (Div. 2) B. Secret Combination 暴力水题
  • 原文地址:https://www.cnblogs.com/VictorierJwr/p/12878484.html
Copyright © 2011-2022 走看看