I tried updating my code to this version but still, it doesn t work:
using System;
namespace RecursiveDomino
{
class Program
{
static void Main()
{
int n = Convert.ToInt32(Console.ReadLine());
bool[] used = new bool[n];
string[] pairList = new string[n];
for (int i = 0; i < n; i++)
{
pairList[i] = Console.ReadLine();
}
int x = Convert.ToInt32(Console.ReadLine());
List<string> results;
results = DominoChains(pairList, x, used);
foreach (string s in results)
{
Console.WriteLine(s);
}
}
static List<string> DominoChains(string[] lista, int x, bool[] used)
{
if (x == 1)
{
List<string> results = new List<string>();
for (int i = 0; i < lista.Length; i++)
{
if (!used[i])
{
results.Add(lista[i]);
}
}
return results;
}
List<string> finalChains = new List<string>();
List<string> temporaryChains = DominoChains(lista, x - 1, used);
for (int i = 0; i < temporaryChains.Count; i++)
{
for (int j = 0; j < lista.Length; j++)
{
if ((!used[j] && temporaryChains[i][temporaryChains[i].Length - 1] == lista[j][0]) || temporaryChains.Count == 0)
{
temporaryChains.Add(lista[j]);
if (temporaryChains.Count == x)
{
finalChains.AddRange(temporaryChains);
}
else { DominoChains(lista, x - 1, used); }
}
}
}
return finalChains;
}
}
}
I have a recursion problem where I have to input into console the following data:
On the first line a natural number equal with the number of tiles I have to input on the following lines (in my example the first number is 6).
On the following lines the domino tiles in the following order:
```lang-none
1 2
1 3
3 4
2 3
3 5
4 5
On the last line another number representing the number of tiles that needs to be returned on each separate line (in my example the number is equal with 3).
With this data I have to display all possible combinations of pairs. For each separate line the number the second number from the first pair has to be equal with the first number of the following pair and so on. I had a hint for this assignment where I have to declare an intermediary list that is equal with the function (using recursion from the start) but when I'm trying to run the code it gives me a null exception for the intermediaryList.
Also I have to mention that I'm allowed to use just "using System;" Not allowed to use anything else.
Ex:
6 1 2 1 3 3 4 2 3 3 5 4 5 3
In the console it will show:
1 2 2 3 3 4 1 2 2 3 3 5 1 3 3 4 4 5 2 3 3 4 4 5
I have to show all the possible combinations, and if I can't find any, it will show N/A.
I have a few problems: I realised the base case to end my recursion is wrong, it will always return me the first pair. So I don't get to try the other elements. And then it's the problem with spacing: My program returns me this:
1 2 2 3 3 4
1 2 2 3 3 5
Instead of this:
1 2 2 3 3 4 1 2 2 3 3 5 1 3 3 4 4 5 2 3 3 4 4 5
for this data set:
6 1 2 1 3 3 4 2 3 3 5 4 5 3
using System;
namespace RecursiveDomino
{
class Program
{
static void Main()
{
int n = Convert.ToInt32(Console.ReadLine());
string[] pairList = new string[n];
for (int i = 0; i < n; i++)
{
pairList[i] = Console.ReadLine();
}
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(DominoChain(pairList, x));
}
static string DominoChain(string[] list, int x)
{
string finalList = "";
for (int i = 0; i < list.Length; i++)
{
if (x == 1)
{
return list[i];
}
}
string temporaryList = DominoChain(list, x - 1);
for (int j = 0; j < list.Length; j++)
{
if (((temporaryList[temporaryList.Length - 1] == list[j][0]) && !temporaryList.Contains(list[j])) || temporaryList == "")
{
finalList += "\n" + temporaryList + " " + list[j];
}
}
return finalList;
}
}
}