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

  • Context: Undergrad 
  • Thread starter Thread starter calluscus
  • Start date Start date
  • Tags Tags
    Algorithm Grid
Click For Summary
SUMMARY

This discussion focuses on an algorithm to fill a grid without repeating positions, utilizing a method inspired by Knuth's shuffling algorithm. The proposed solution involves initializing a grid with integers from 1 to r * c and then randomly swapping values between cells. The provided C code demonstrates this approach, which includes seeding the random number generator and executing swaps based on random selections of grid positions.

PREREQUISITES
  • Understanding of grid data structures
  • Familiarity with C programming language
  • Knowledge of random number generation techniques
  • Basic concepts of algorithms and shuffling methods
NEXT STEPS
  • Study Knuth's shuffling algorithm for deeper insights
  • Learn about pseudorandom number generators and their implementations
  • Explore advanced grid-filling algorithms for larger datasets
  • Investigate optimization techniques for grid manipulation in C
USEFUL FOR

Programmers, algorithm enthusiasts, and anyone interested in grid-based problem-solving techniques will benefit from this discussion.

Mathematics news on Phys.org
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:
ty very much
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
Replies
14
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K