0

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;
        }
    }
}
4
  • Only allowed to use system: is this an online judge problem e.g. leetcode? Commented Apr 9, 2024 at 9:33
  • I'd return results from the recursive function as a list rather than trying to build up line break separated strings. That's going to be harder to process in the calling function. I also think your contains check to prevent duplicates is going to catch false positives too, e.g. if you had tile 22 then it'd match any sequence where you join two other tiles on 2s, and won't work if e.g. you're given two copies of the same input tile. Commented Apr 9, 2024 at 9:34
  • I can't completely explain the null exception but you probably don't want to run the recursive case when x <= 1 as there's no more domines to append and you'll start getting array out of bounds exceptions as you keep recursing. Commented Apr 9, 2024 at 9:36
  • Yes it is an online judge problem, so I have to use lists, this means I have to make the type of function as a List<strings> ? And how can I verify if the end of the current string matches the last number of the temporary string ? Commented Apr 9, 2024 at 12:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.