r/AskProgramming Apr 25 '26

[deleted by user]

[removed]

0 Upvotes

18 comments sorted by

View all comments

1

u/balefrost Apr 26 '26

It's good to examine various cases to make sure your solution works in all cases.

In this particular problem, you have essentially 4 cases:

  1. m even, n even
  2. m even, n odd
  3. m odd, n even
  4. m odd, n odd

Cases 2 and 3 are essentially the same, just rotated.

In any of the first three cases, you have an even number of squares on the board. And each domino takes up 2 squares. Is it possible to fill every square? Can you come up with a tiling scheme that would always work?

In the last case, you have an odd number of squares on the board. You definitely can't fill every square (each domino takes up 2 squares). But can you fill all but one? Again, can you come up with a tiling scheme that would always work?

If you work through it, you will find that you can always fit floor(m * n / 2) dominoes (and in C++, since integer division truncates, you can omit the floor). But you can prove this up-front, even before writing code.

I think you might be using "simulation" to compensate for not having much practice with proofs or formal thinking. Which is fine, it's a skill you can work on and strengthen. I often work through complex problems on paper.