P1088 [NOIP2004 普及组] 火星人

火星人

这题做了好久,原因是对dfs生成全排列理解的还不够透彻。

1  2  3  4  5    这一行就像盒子 

1    /竖着的这一列就像是扑克牌,从1号扑克牌到5号扑克
2    排开始不断尝试,放入1号盒子。   2...5号盒子也
3    是如此 /
4  
5  
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <map>

using namespace std;
 
const int maxn = 1e5 + 5;
int n, m, cnt;
int a[maxn], book[maxn], now[maxn];

void dfs(int step)
{
	if(cnt > m + 1) return;
	if(step > n){
		cnt++;
		if(cnt == m + 1) for(int i = 1; i <= n; i++) cout << a[i] << " ";
		return;
	}
	/*就好比,本来从1号扑克牌开始放入箱子,现在直接从5
	号扑克牌开始放入箱子*/
	for(int i = (cnt == 0 ? now[step] : 1); i <= n; i++){
		if(book[i] == 0){
			book[i] = 1;
			a[step] = i;
			dfs(step + 1);
			book[i] = 0;
		}
	}
}

int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i++) cin >> now[i];
	dfs(1);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值