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 8047441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:05:55+00:00 2026-06-05T06:05:55+00:00

I’m making a word game. The board is represented as a 2D C array.

  • 0

I’m making a word game. The board is represented as a 2D C array. A simplified version of my populated array might look something like this.

[x][x][0][0][0][0][0]
[x][0][0][0][x][0][0]
[x][0][0][x][x][x][x]
[0][0][0][0][x][0][0]

There are no diagonal connections between x blocks, only up, down, left and right.

I want an algorithm to check if there are two non-contiguous ‘blocks’ of X values. This algorithm, given a particular 2D game array, will enable me to decide between:

  • Yes, there is only one contiguous block of Xs
  • No, there is more than one contiguous block of Xs

For the example shown above, I would find ‘No’, because there is more than one contiguous block of Xs.

My first simple idea is to find one contiguous block of Xs and catalogue those values. It is easy for me to get a starting point in the middle of a contiguous block, for reasons that aren’t important to this question. If I can find an X anywhere that is not in my catalogued list, I can return ‘No’, else, ‘Yes’. However, this strikes me as a pretty inefficient way of doing it.

I understand that I would need to touch every item of the 2D array to comprehensively rule out another contiguous block. I’m more interested in the quickest way of finding a ‘No’.

Because the array is pretty small, I could implement my idea without any real performance problems. However, I’m sure there is a more elegant way, and I thought this problem might interest some algorithm-loving part of the stack overflow hive mind.

This could also be described somewhere in another SO question or the internet at large. Maybe I just don’t know the right way to phrase the question. If you do, please enlighten me. Best solution also gets a free copy of the as-yet-to-be-completed iOS word game. 😀

  • 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-06-05T06:05:58+00:00Added an answer on June 5, 2026 at 6:05 am

    Your array forms a graph, and you are asking if the graph has more than one connected component. The easiest way to do this is probably to copy the array, erase the first connected component you find, then look for another X. If you find one, there were more than one component to begin with.

    Erasing is a nice application of graph search. For example, you could do it this way (in C):

    #define N_ROWS 4
    #define N_COLS 7
    typedef char WORD_BOX[N_ROWS][N_COLS];
    
    void erase_cc(WORD_BOX box, int i, int j)
    {
      if (i < 0 || j < 0 || i >= N_ROWS || j >= N_COLS || box[i][j] != 'X')
        return;
      box[i][j] = 'O';
      erase_cc(box, i - 1, j);
      erase_cc(box, i + 1, j);
      erase_cc(box, i, j - 1);
      erase_cc(box, i, j + 1);
    }
    
    int find_cc(WORD_BOX box, int *i_ret, int *j_ret)
    {
      int i, j;
      for (i = 0; i < N_ROWS; i++)
        for (j = 0; j < N_COLS; j++)
          if (box[i][j] == 'X') {
            *i_ret = i;
            *j_ret = j;
            return 1;
          }
      return 0;
    }
    
    
    int more_than_one_cc(WORD_BOX box)
    {
      int i,  j;
      WORD_BOX copy;
      memcpy(copy, box, sizeof(WORD_BOX));
      if (find_cc(copy, &i, &j)) {
        erase_cc(copy, i, j);
        return find_cc(copy, &i, &j);
      }
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

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.