Can You Help Me Find an Algorithm to Fill a Grid Without Repeating Places?

In summary, the person is looking for an algorithm to fill a grid without repeating places. They mention trying to use pseudorandom number lists but not understanding it clearly. They ask for help and someone suggests using a shuffling algorithm in two dimensions. The code for this algorithm is provided, but the person clarifies that they are looking for a solution that does not involve randomness.
Mathematics news on Phys.org
  • #2
calluscus said:
Hi:

I'm looking for an algorithm to fill a grid without repeating places.
I have looking for Pseudorandom number lists (http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators#gsl_rng_minstd) but i do not get it clearly...

Any help ? :)

Well, what exactly are you trying to accomplish? If the grid is small enough, you could fill in the grid with integers, 1 ... r * c, and then for each cell, swap its value with another, random cell, that is to the right or below the current cell. This is similar to Knuth's shuffling algorithm except in two dimensions. C code follows.

Code:
#include <stdlib.h>

#define X (80)
#define Y (24)

int main()
{
  int x, y; /* current grid position */
  int i, j; /* x, y for random grid position with which to swap*/
  int k;    /* auxiliary variable to fill out the initial sorted grid */
  int t;    /* auxiliary variable for swapping */
  int grid[X][Y];

  srand(time(NULL)); /* seed the random number generator */
  k = 0;

  /* fill in the grid with (1, 2, ..., x*y) */
  for (y = 0; y < Y; ++y)
    for (x = 0; x < X; ++x)
      grid[x][y] = ++k;

  for (y = 0; y < Y; ++y)
    for (x = 0; x < X; ++x)
    {
      /* select a random row between this one and the last one */
      j = rand() % (Y - y) + y;

      if (j == y)
      {
        /* if it's on the current row, select a column to our right */
        i = rand() % (X - x) + x;
      }
      else
      {
        /* if it's on a row below, select _any_ column */
        i = rand() % X;
      }

      /* swap */
      t = grid[i][j];
      grid[i][j] = grid[x][y];
      grid[x][y] = t;
    }

  /* do stuff with the grid here */

  return 0;
}

(note: I have not even so much as compiled the above code, but hopefully you can follow what I'm trying to do)
 
Last edited:
  • #3
ty very much
 

FAQ: Can You Help Me Find an Algorithm to Fill a Grid Without Repeating Places?

1. What is an algorithm to fill a grid?

An algorithm to fill a grid is a set of instructions or steps that are used to systematically fill a grid with specific elements or values. It is often used in computer science and mathematics to solve problems and create visual representations of data.

2. How does an algorithm to fill a grid work?

An algorithm to fill a grid typically involves breaking down the grid into smaller sections and filling each section one at a time. This can involve using specific rules or patterns to determine the placement of elements in the grid. The algorithm may also involve using conditional statements and loops to ensure the grid is filled correctly.

3. What are some common applications of an algorithm to fill a grid?

An algorithm to fill a grid can be used in a variety of applications, including creating visual representations of data in graphs and charts, solving puzzles and games, and generating patterns in art and design. It is also commonly used in computer graphics and image processing to fill in pixels and create digital images.

4. Are there different types of algorithms to fill a grid?

Yes, there are different types of algorithms to fill a grid, such as backtracking algorithms, greedy algorithms, and divide and conquer algorithms. Each type of algorithm uses a different approach to fill the grid and may be more suitable for certain types of problems or data sets.

5. How can someone create their own algorithm to fill a grid?

To create your own algorithm to fill a grid, you will need to have a good understanding of the problem you are trying to solve and the data you are working with. You can start by breaking down the grid into smaller sections and determining the rules or patterns that will guide the filling process. It may also be helpful to test and refine your algorithm through trial and error until you achieve the desired result.

Back
Top