-1

I really puted so much effort to understand how to solve this question but unfortuantly i am still missing something thats why i am not able to solve this issue. The question is: Write a function that takes undetermined amount of domino tiles as input and determines if you can place them in a row with only matching numbers side by side. Return true of it is possible and false otherwise. Each domino has two sides with the number 0-6, as depicted bellow.
Example A: [2,2], [2,3],[3,6].. this case return true.
Example B: [3,1], [1,5],[3,6].. this case return false.

Detailed instructions: the ammount of domino tiles are given as input to the function in the following format, using example above: [[2,2], [2,3],[3,6]]

This is how they started the recommended function:

public bool dominos(long[,] arr1,  long dim_1, long dim_2){    
}
Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
Kob_24
  • 592
  • 1
  • 10
  • 26

3 Answers3

0

I would approach the problem with recursive search - write a function that will travers all allowed combinations of domino tiles. That way you will have exact solution for any set of tiles, although not very efficient. Then you can start looking for faster algorithm, but I'm not sure if there exist faster way of determining answer for your problem or is it relevant for you that the solution to be found in faster than exponential time.

Pseudocode for searching all solution would be like this):

bool dominos(int[,] current, int[,] left)
{
  if (left.Length == 0) {
     Console.Writeln(solution = current);
     return true;
  } else {
     bool result = false;
     foreach(next in left.Where(tile matches ending of current path)) {
         result = result || dominos(current with appended next, left without next)
         if (result)
             break;
     }
  }
}
PanJanek
  • 6,593
  • 2
  • 34
  • 41
0

Basically, it seems like you would want to have some kind of object to denote your Domino tile. It should have the 2 values, and maybe an indication of left/right.

From there, it's a trivial thing to have a recursive call that checks if you take the first tile, can you put the rest behind it, if not, swap it, and try again. If you can, return success, if not, return failure.

Every step, you basically say, take one, try the rest. So the last step will get 1 tile, and return true, and from there, it's up to the tiles you have.

Noctis
  • 11,507
  • 3
  • 43
  • 82
0

All you need is to count the how often each number is there.

Then return true if two or less numbers have an odd count, false otherwise..

Remove all doubles with numbers that are on any other tiles before the count!

Return false for an empty list!

TaW
  • 53,122
  • 8
  • 69
  • 111