Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard, raimo
Mine goes row-by-row. It copies "123456789" into the row, calls random_shuffle on it, and checks if this introduced any duplicate digits in any column or 3x3 box. If it did, it re-shuffles the row and tries again.Safari wrote:You guys use similar look-up strings. Anyone care toexplain the logic of their algorithm? :)
Beer Hunter wrote:Mine goes row-by-row. It copies "123456789" into the row, calls random_shuffle on it, and checks if this introduced any duplicate digits in any column or 3x3 box. If it did, it re-shuffles the row and tries again.Safari wrote:You guys use similar look-up strings. Anyone care toexplain the logic of their algorithm?
In the event of unthinkable mayhem (about 35% of the time), the algorithm gives up and starts over.
I think the real trickery is in my code that checks for duplicate digits in columns and 3x3 boxes. It has two loops. The outer loop goes from '1' to '9': on the first iteration, it only detects duplicate 1s, and so on. The inner loop goes from 0 to 161, looping over a large lookup string. In the lookup string, each block of nine characters contains the offsets of the characters of a column or a 3x3 box.
The loop condition on the inner loop contains "i % 9 || (n = -1)". This resets 'n' to -1 whenever i is a multiple of nine (including when i == 0). Now, for the next nine iterations, the value of 'n' is incremented whenever it finds the current digit it's searching for. If 'n' is incremented twice (so it equals 1), there's a duplicate digit, so the code jumps back to the random_shuffle part.
Safari wrote:You guys use similar look-up strings. Anyone care toexplain the logic of their algorithm?
marcdan221 wrote:Is there something wrong with the posted Sudoku code found here? When I run them, they give me the same Sudoku grid every time.
258 691 374
367 254 891
194 378 625
615 732 948
482 169 753
739 485 216
926 513 487
871 946 532
543 827 169561 347 928
274 981 635
893 256 417
127 495 386
386 712 594
945 863 271
719 624 853
458 139 762
632 578 000
218357496796 325 481
413 879 652
825 164 379
164 738 925
537 692 148
289 451 763
351 246 897
948 517 236
672 983 514#include <iostream>
#include <algorithm>
#include <time.h>
#include <string>
#include "windows.h"
using namespace std;
int main ()
{
int countdown = 2;
while( countdown--)
{
string test = "0123456789";
srand((unsigned)time(0));
cout << "time: " << time(0) << "\n";
cout << "rand: " << rand() << "\n";
random_shuffle(test.begin(),test.end());
cout << "random_shuffle:" << test << "\n\n";
Sleep(1000);
}
}
time: 1138223683
rand: 24199
random_shuffle:6892143705
time: 1138223684
rand: 23984
random_shuffle:0913548672C:\Borland\BCC55\Projects>test2
time: 1138226180
rand: 9983
random_shuffle:6892143705
time: 1138226181
rand: 9768
random_shuffle:0913548672
C:\Borland\BCC55\Projects>test2
time: 1138226183
rand: 9337
random_shuffle:6892143705
time: 1138226184
rand: 9121
random_shuffle:0913548672
C:\Borland\BCC55\Projects>test2
time: 1138226425
rand: 22683
random_shuffle:6892143705
time: 1138226426
rand: 22467
random_shuffle:0913548672#include <iostream>
#include <algorithm>
#include <time.h>
#include <string>
#include "windows.h"
using namespace std;
int main ()
{
RandomNumberGenerator<int> rng;
int countdown = 2;
while( countdown--)
{
string test = "0123456789";
srand((unsigned)time(0));
random_shuffle(test.begin(),test.end(), rng );
cout << "random_shuffle:" << test << "\n\n";
Sleep(1000);
}
}
Users browsing this forum: No registered users and 1 guest