Skip to content

Instantly share code, notes, and snippets.

@dluciano
Last active November 6, 2023 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dluciano/f1bdc50f54322eb82ee01e5964f556a8 to your computer and use it in GitHub Desktop.
Save dluciano/f1bdc50f54322eb82ee01e5964f556a8 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
public class Program {
public static void Main(string[] args) {
var K = 3;
var A = new[]{8, 7, 5, 6, 10};
//var A = new[]{9, 6};
var res = Sol.Solution(A, K);
Console.WriteLine($"Solution: {res}");
}
}
public static class Sol {
public static int Solution(int[] rolls, int k)
{
var counter = 0;
void bt(int curIndex, int size, List<int> cur, HashSet<int> friends)
{
if (cur.Count == size)
{
foreach (var c in cur)
Console.Write($"{c}, ");
Console.WriteLine();
counter++;
return;
}
for (var i = curIndex; i < rolls.Length; ++i)
{
if (friends.Contains(rolls[i]))
continue;
friends.Add(rolls[i] + k);
friends.Add(k - rolls[i]);
friends.Add(rolls[i] - k);
cur.Add(rolls[i]);
bt(i + 1, size, cur, friends);
if (cur.Count == 0)
continue;
cur.RemoveAt(cur.Count - 1);
friends.Remove(rolls[i] + k);
friends.Remove(k - rolls[i]);
friends.Remove(rolls[i] - k);
}
}
for (var s = 1; s <= rolls.Length; ++s)
bt(0, s, new(), new());
return counter;
}
}
/*
(5,7,6,8,10),(6,10),(6,5),(8,5),(10,5),(6,7), (8,7), (10,7), (6,10,5), (6,10,7)
[8,7,5,6,10]
8 x
7 x
5 x
6 x
10 x
-------
8,7 x
8,5 x
[8,6]
[8,10]
[7,5]
7,6 x
7,10 x
5,6 x
5,10 x
6,10 x
-------
[8,7,5]
[8,7,6]
[8,7,10]
[8,5,6]
[8,5,10]
[8,6,10]
[7,5,6]
[7,5,10]
7,6,10 x
5,6,10 x
--------
[8,7,5,6]
[8,7,5,10]
[7,5,6,10]
---------
[8,7,5,6,10]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment