给定一个正数n,我们需要找到2 * n个元素的所有组合,以使从1到n的每个元素都出现两次,并且其出现之间的距离恰好等于该元素的值。
例子:
Input : n = 3
Output : 3 1 2 1 3 2
2 3 1 2 1 3
All elements from 1 to 3 appear
twice and distance between two
appearances is equal to value
of the element.
Input : n = 4
Output : 4 1 3 1 2 4 3 2
2 3 4 2 1 3 1 4
解释
我们可以使用回溯来解决这个问题。这个想法是针对第一个元素的所有可能组合,然后递归地探索其余元素,以检查它们是否会导致解决方案。如果当前配置没有解决方案,我们将回溯。注意,可以将元素k放置在输出数组i> = 0且(i + k + 1)<2 * n的位置i和(i + k + 1)的位置。
请注意,元素的组合对于n的某些值(如2、5、6等)是不可能的。
C++
// C++ program to find all combinations where every
// element appears twice and distance between
// appearances is equal to the value
#include
using namespace std;
// Find all combinations that satisfies given constraints
void allCombinationsRec(vector &arr, int elem, int n)
{
// if all elements are filled, print the solution
if (elem > n)
{
for (int i : arr)
cout << i << " ";
cout << endl;
return;
}
// try all possible combinations for element elem
for (int i = 0; i < 2*n; i++)
{
// if position i and (i+elem+1) are not occupied
// in the vector
if (arr[i] == -1 && (i + elem + 1) < 2*n &&
arr[i + elem + 1] == -1)
{
// place elem at position i and (i+elem+1)
arr[i] = elem;
arr[i + elem + 1] = elem;
// recurse for next element
allCombinationsRec(arr, elem + 1, n);
// backtrack (remove elem from position i and (i+elem+1) )
arr[i] = -1;
arr[i + elem + 1] = -1;
}
}
}
void allCombinations(int n)
{
// create a vector of double the size of given number with
vector arr(2*n, -1);
// all its elements initialized by 1
int elem = 1;
// start from element 1
allCombinationsRec(arr, elem, n);
}
// Driver code
int main()
{
// given number
int n = 3;
allCombinations(n);
return 0;
}
Java
// Java program to find all combinations where every
// element appears twice and distance between
// appearances is equal to the value
import java.util.Vector;
class Test
{
// Find all combinations that satisfies given constraints
static void allCombinationsRec(Vector arr, int elem, int n)
{
// if all elements are filled, print the solution
if (elem > n)
{
for (int i : arr)
System.out.print(i + " ");
System.out.println();
return;
}
// try all possible combinations for element elem
for (int i = 0; i < 2*n; i++)
{
// if position i and (i+elem+1) are not occupied
// in the vector
if (arr.get(i) == -1 && (i + elem + 1) < 2*n &&
arr.get(i + elem + 1) == -1)
{
// place elem at position i and (i+elem+1)
arr.set(i, elem);
arr.set(i + elem + 1, elem);
// recurse for next element
allCombinationsRec(arr, elem + 1, n);
// backtrack (remove elem from position i and (i+elem+1) )
arr.set(i, -1);
arr.set(i + elem + 1, -1);
}
}
}
static void allCombinations(int n)
{
// create a vector of double the size of given number with
Vector arr = new Vector<>();
for (int i = 0; i < 2*n; i++) {
arr.add(-1);
}
// all its elements initialized by 1
int elem = 1;
// start from element 1
allCombinationsRec(arr, elem, n);
}
// Driver method
public static void main(String[] args)
{
// given number
int n = 3;
allCombinations(n);
}
}
Python3
# Python3 program to find all combinations
# where every element appears twice and distance
# between appearances is equal to the value
# Find all combinations that
# satisfies given constraints
def allCombinationsRec(arr, elem, n):
# if all elements are filled,
# print the solution
if (elem > n):
for i in (arr):
print(i, end = " ")
print("")
return
# Try all possible combinations
# for element elem
for i in range(0, 2 * n):
# if position i and (i+elem+1) are
# not occupied in the vector
if (arr[i] == -1 and
(i + elem + 1) < 2*n and
arr[i + elem + 1] == -1):
# place elem at position
# i and (i+elem+1)
arr[i] = elem
arr[i + elem + 1] = elem
# recurse for next element
allCombinationsRec(arr, elem + 1, n)
# backtrack (remove elem from
# position i and (i+elem+1) )
arr[i] = -1
arr[i + elem + 1] = -1
def allCombinations(n):
# create a vector of double
# the size of given number with
arr = [-1] * (2 * n)
# all its elements initialized by 1
elem = 1
# start from element 1
allCombinationsRec(arr, elem, n)
# Driver code
n = 3
allCombinations(n)
# This code is contributed by Smitha Dinesh Semwal.
输出:
3 1 2 1 3 2
2 3 1 2 1 3