d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 1000 Fg For Whoever Can Fix My Code
123Next
Add Reply New Topic New Poll
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Mar 17 2014 09:42pm
http://stackoverflow.com/questions/22469781/c-creating-filled-sudoku-grid-from-empty-matrix-of-size-n

Formatting code on jsp is annoying, so it's in the above link along with a description
Need to have this done before 03/18, 11:59 pm EST

This post was edited by Foxic on Mar 17 2014 09:43pm
Member
Posts: 29,614
Joined: Sep 23 2008
Gold: 0.00
Mar 17 2014 09:56pm
It's possible that your checkRepeat function isn't working correctly. I say this because, you aren't passing the value of 'x' to it within your for loop, so I'm not sure how you are even checking for the repetition within the function.

Code
for (int x = 1; x < size+1 && (checkRepeat(size,grid,row,col) || holder == grid[row][col]); x++)


Also FYI, you may want to paste the checkRepeat function in your post as well. People would want to see that too.
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Mar 17 2014 10:06pm
Alright I posted the checkRepeat function, but I'm pretty sure it's fine.

And for that function, checkRepeat(size,grid,row,col) checks if the current value in grid[row][col] which is x, is repeated. So it does check all possible numbers (1 to N)

This post was edited by Foxic on Mar 17 2014 10:07pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Mar 17 2014 10:14pm
c++ is not my strong suit, but:

why not just make an array of the "allowed values" for a square, then randomly pick one from the array?
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 17 2014 10:22pm
Does your assignment actually say to use backtracking to solve a sudoku puzzle? I feel for whoever has to test these programs. Solving a 9x9 sudoku board trying every possible combination? Run it, come back a few days later, check the results. No thanks.

In any case, if you are supposed to be implementing recursive backtracking then you are not doing that at all. Fixing this algorithm would require it to be completely rewritten. Can you post the assignment specs, and your entire solution thus far. I don't want to make assumptions off of what you have omitted.
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Mar 17 2014 10:25pm
Quote (Minkomonster @ Mar 17 2014 09:22pm)
Does your assignment actually say to use backtracking to solve a sudoku puzzle? I feel for whoever has to test these programs. Solving a 9x9 sudoku board trying every possible combination? Run it, come back a few days later, check the results. No thanks.
actually this will (properly implemented) solve a sudoku in a few seconds, even with a slow language like python (in my experience).

Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Mar 17 2014 10:26pm
Quote (Azrad @ Mar 17 2014 11:14pm)
c++ is not my strong suit, but:

why not just make an array of the "allowed values" for a square, then randomly pick one from the array?


That's not what the problem is. When a cell runs out of values to put in (i.e they're all restricted) it just goes onto the next cell

Quote (Minkomonster @ Mar 17 2014 11:22pm)
Does your assignment actually say to use backtracking to solve a sudoku puzzle? I feel for whoever has to test these programs. Solving a 9x9 sudoku board trying every possible combination? Run it, come back a few days later, check the results. No thanks.

In any case, if you are supposed to be implementing recursive backtracking then you are not doing that at all. Fixing this algorithm would require it to be completely rewritten. Can you post the assignment specs, and your entire solution thus far. I don't want to make assumptions off of what you have omitted.


The program has to generate an N size (specified by user) grid filled with numbers, where there are no repeats in any row/column. Have to use backtracking, or recursion, or both, and result has to be random (different result each time you run the prog)

I can pay 1k fg to write me up a program, or more if it's time consuming (pm me the code though)

Also here's an example of what it's doing (4x4 grid, first 2 rows:)
3 1 2 4 - > 3 1 2 4
1 2 3 0 - > 1 2 3 4 <- it fills in this cell with 4 and keeps going. It ends up as

3 1 2 4
1 2 3 4
2 3 4 1
4 4 1 2

This post was edited by Foxic on Mar 17 2014 10:31pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Mar 17 2014 10:41pm
like I said, writing something like this in c++ would prob take me a couple days, but here is an overview of what I would do it (pseudo code)

Code
function MakeGrid(GRID, current_row, current_column, number_of_rows):
possible_entry_array = [1,2,3,4,5,6,7,8,9]
for each entry in current_row:
pop entry from possible_entry_array
for each entry in current_column:
pop entry from possible_entry_array
A = pick random element from possible_entry_array
set GRID[current_row][current_column] = A

if current_row == number_of_rows && current_column == number_of_rows:
return //we are done
elseif current column = 9:
current_column = 1
current_row++
else:
current column++

call MakeGrid(GRID, current_row, current_column, number_of_rows)


set up GRID with 0's in every position

call MakeGrid (GRID, current_row, current_column, number_of_rows)
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Mar 17 2014 10:48pm
I think my method could work too, the problem is implementation.

And I have to go to bed now, but my offer still stands until 11:59 pm est unless I get an extension (which I probably will)

Also I'll pay way over 1k fg if you do it from scratch and it takes a while, just send me the code and price

This post was edited by Foxic on Mar 17 2014 11:01pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 17 2014 11:06pm
If it is just row or column here you go:

Code
#include<cstdlib>
#include <stdio.h>
#include <time.h>
bool unassigned(int** grid, int n, int& r, int& c)
{
for(r = 0; r < n; r++)
for(c = 0; c < n; c++)
if(grid[r][c] == 0)
return true;
return false;
}

bool conflicts(int** grid,int n, int r, int c, int x)
{
for(int i = 0; i < n; i++)
if(grid[r][i] == x || grid[i][c] == x) return true;
return false;
}
bool solve(int** grid, int n)
{
int r, c;
if(!unassigned(grid,n,r,c))
return true;

for(int i = 1; i <= n; i++)
{
if(!conflicts(grid,n,r,c,i))
{
grid[r][c] = i;
if(solve(grid,n))
{
return true;
}
grid[r][c] = 0;
}
}
return false;
}

int main()
{
srand (time(NULL));
int n = 9;

int r = rand()%n;
int c = rand()%n;
int x = 1+rand()%n;

int* grid[n];
for(int i = 0; i < n; i++)
{
grid[i] = new int[n];
for(int j = 0; j < n; j++)
grid[i][j] = 0;

}

grid[r][c] = x;

solve(grid,n);

for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
printf("%d",grid[i][j]);
printf("\n");
}
scanf("%d");



}


1000fg?


Edit: Any questions, feel free to ask.

This post was edited by Minkomonster on Mar 17 2014 11:21pm
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll