p1088火星人,萌新TLE20分求助
查看原帖
p1088火星人,萌新TLE20分求助
628730
potatotea楼主2022/3/19 12:19
#include<bits/stdc++.h>
using namespace std;
int n, m,cnt;
int arr[10010];
int finger[10010];
int book[10010];

void work(int j) {
	if (j - 1 == n) {
			if (!cnt) {
				for (int i = 1; i <= n; i++) {
					if (arr[i] != finger[i]) {
						break;
					}
					else if (i == n) {
						cnt++;
					}
				}
			}

			if (cnt) {
				cnt++;
			}
			if (cnt - 2 == m) {
				for (int i = 1; i <= n; i++) {
					cout << arr[i] << " ";					
				}
			}
			return;
		}
		
	for (int i = 1; i <= n; i++) {
		if (cnt - 2 == m) {
			return;
		}
		if (book[i] == 0) {
			arr[j] = i;
			book[i] = 1;
			work(j + 1);
			book[i] = 0;
		}
	}
}


int main() {
	cin >> n >> m;

	for (int i = 1; i <= n; i++) {
		cin >> finger[i];
	}
	work(1);
	return 0;
}
2022/3/19 12:19
加载中...