Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 756679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:16:22+00:00 2026-05-14T15:16:22+00:00

I have to create a program that finds all the possible ways of filling

  • 0

I have to create a program that finds all the possible ways of filling a square of size x by y. You place a block which takes up 2 spaces to completely fill.

The problem is I don’t know how to code it to the point where you can remember the placements of each square. I can get it to where it fills the board completely once and maybe twice, but nothing past that. I also know that I’m supposed to use recursion to figure this out . Here is the code I started on so far. There is also a main method and I have the initial even/odd check working fine. This is the part I have no idea on.

    public void recurDomino(int row, int column) {
            if (Board[2][x - 1] != false) {

            } else if(Board[1][x-1]!=false)
            {

            }
            else {
                for (int n=0; n < x - 1; n++) {
                    Board[row][column] = true;
                    Board[row][column+1] = true;
                    column++;
                    counter++;
                } 
                recurDomino(1, 0);
                recurDomino(2, 0);

            }
        }

Thank you for any help you guys can give me. 


******************* EDIT ****************************************

I am a little confused still. I came up with this algorithm but I always get 2 for any value greater or equal to 2.

public boolean tryHorizontal(int row , int col){
        if( row < 0 || row >= array[0].length-1)
            return false;
        else
            return true;
    }

    public boolean tryVertical(int row, int col){
        if( col < 0 || col >= 2 )
            return false;
        else
            return true;
    }

    public boolean tryRowCol(int row, int col){
        if(this.tryHorizontal(row, col) && this.tryVertical(row, col)){
            return true;
        }
        else
            return false;
    }

    public int findWays(int row, int col){
        int n = 0;
        if( !this.tryRowCol(row, col))
            return 0;
        else
            n =+ 1 + this.findWays(row+1, col+1);

        return n;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:16:23+00:00Added an answer on May 14, 2026 at 3:16 pm

    This recursive solution actually generates all the possible tiling of a general MxN board. It’s more general than what your program requires, and therefore not optimized to just count the number of tiling of a 3xN board.

    If you just want to count how many there are, you can use dynamic programming techniques and do this much faster. Also, having the number of rows fixed at 3 actually makes the problem considerably easier. Nonetheless, this general generative solution should be instructive.

    public class Domino {
        final int N;
        final int M;
        final char[][] board;
        int count;
    
        static final char EMPTY = 0;
    
        Domino(int M, int N) {
            this.M = M;
            this.N = N;
            board = new char[M][N]; // all EMPTY
            this.count = 0;
            generate(0, 0);
            System.out.println(count);
        }
    
        void printBoard() {
            String result = "";
            for (char[] row : board) {
                result += new String(row) + "\n";
            }
            System.out.println(result);     
        }
    
        void generate(int r, int c) {
           //... see next code block
        }
        public static void main(String[] args) {
            new Domino(6, 6);
        }
    }
    

    So here’s the meat and potatoes:

        void generate(int r, int c) {
            // find next empty spot in column-major order
            while (c < N && board[r][c] != EMPTY) {
                if (++r == M) {
                    r = 0;
                    c++;
                }
            }
            if (c == N) { // we're done!
                count++;
                printBoard();
                return;
            }
            if (c < N - 1) {
                board[r][c] = '<';
                board[r][c+1] = '>';
                generate(r, c);
                board[r][c] = EMPTY;
                board[r][c+1] = EMPTY;
            }
            if (r < M - 1 && board[r+1][c] == EMPTY) {
                board[r][c] = 'A';
                board[r+1][c] = 'V';
                generate(r, c);
                board[r][c] = EMPTY;
                board[r+1][c] = EMPTY;
            }
        }
    

    This excerpt from the last few lines of the output gives an example of a generated board, and the final count.

    //... omitted
    
    AA<><>
    VVAA<>
    AAVV<>
    VVAA<>
    <>VVAA
    <><>VV
    
    //... omitted
    
    6728
    

    Note that 6728 checks out with OEIS A004003.

    A few things that you need to learn from this solutions are:

    • Clean-up after yourself! This is a very common pattern in recursive solution that modifies a mutable shared data. Feel free to do your thing, but then leave things as you found them, so others can do their thing.
    • Figure out a systematic way to explore the search space. In this case, dominoes are placed in column-major order, with its top-left corner as the reference point.

    So hopefully you can learn something from this and adapt the techniques for your homework. Good luck!


    Tip: if you comment out the printBoard line, you can generate all ~13 million boards for 8×8 in reasonable time. It’ll definitely be much faster to just compute the number without having to generate and count them one by one, though.


    Update!

    Here’s a recursive generator for 3xN boards. It doesn’t use a shared mutable array, it just uses immutable strings instead. It makes the logic simpler (no clean up since you didn’t make a mess!) and the code more readable (where and how the pieces are placed is visible!).

    Since we’re fixed at 3 rows, the logic is more explicit if we just have 3 mutually recursive functions.

    public class Domino3xN {
       static int count = 0;
    
       public static void main(String[] args) {
          addRow1(8, "", "", "");
          System.out.println(count);
       }
    
       static void addRow1(int N, String row1, String row2, String row3) {
          if (row1.length() == N && row2.length() == N && row3.length() == N) {
             count++; // found one!
             System.out.format("%s%n%s%n%s%n%n", row1, row2, row3);
             return;
          }
          if (row1.length() > row2.length()) { // not my turn!
             addRow2(N, row1, row2, row3);
             return;
          }
          if (row1.length() < N - 1)
             addRow2(N, row1 + "<>",
                        row2,
                        row3);
          if (row2.length() == row1.length())
             addRow3(N, row1 + "A",
                        row2 + "V",
                        row3);
       }
       static void addRow2(int N, String row1, String row2, String row3) {
          if (row2.length() > row3.length()) { // not my turn!
             addRow3(N, row1, row2, row3);
             return;
          }
          if (row2.length() < N - 1)
             addRow3(N, row1,
                        row2 + "<>",
                        row3);
          if (row3.length() == row2.length())
             addRow1(N, row1,
                        row2 + "A",
                        row3 + "V");
       }
       static void addRow3(int N, String row1, String row2, String row3) {
          if (row3.length() == row2.length()) { // not my turn!
             addRow1(N, row1, row2, row3);
             return;
          }
          if (row3.length() < N - 1)
             addRow1(N, row1,
                        row2,
                        row3 + "<>");
       }
    }
    

    You don’t often see 3 mutually recursive functions like this, so this should be educational.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to create a cipher program that shifts the letters of a message
For a school assignment I have to create a C++ program that will create
I have a program that is used to create other program and compile it
I have a small program that I'm trying to create to get ip addresses
I have to create a C# program that deals well with reading in huge
I have a problem that is seemingly solvable by enumerating all possible solutions and
We need to create a program in PHP/MySQL that will display all the 'people',
I have created a non-form c# program that uses the NotifyIcon class. The text
I have made a program that scans rss feeds. This same program creates feeds
I have a program that creates multiple text files of rdf triples. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.