Related
So I tried to make a program that shows you all the possible knight moves within the limit, the possible move marked with the number '1'. but when I run the program with a movement limit more than 2, it's kind of messed up. what did I do wrong?
#include<stdio.h>
int xpos[8]={2,1,-1,-2,-2,-1,1,2};
int ypos[8]={1,2,2,1,-1,-2,-2,-1};
void whiteMove(int wx, int wy,int map[8][8],int limit);
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
int map[8][8]={{0}};
int limit; char k1[3];
scanf("%d",&limit);getchar();
scanf("%s",k1);getchar();
int wx=k1[0]-'A';
int wy='8'-k1[1];
printf("Case #%d: \n",i+1);
whiteMove(wx,wy,map,limit+1);
for(int j=0;j<8;j++){
for(int k=0;k<8;k++){
printf("%d ",map[j][k]);
}
puts("");
}
}
return 0;
}
void whiteMove(int wx, int wy,int map[8][8], int limit){
if(wx<0||wy<0||wx>7||wy>7||limit==0){
return;
}else if(map[wy][wx]!=1){
map[wy][wx]=1;
}else{
return;
}
for(int i=0;i<8;i++){
whiteMove(wx+xpos[i],wy+ypos[i],map,limit-1);
}
}
input example:
1 => the amount of test case
3 => movement limit
A1 =>starting coordinate
th result is
Case #1:
0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 0
1 0 1 0 1 0 0 0
1 1 1 0 0 1 0 0
1 0 1 1 1 0 0 0
*0 1 0 1 0 1 0 0 *map[5][0]
1 0 *1 1 1 0 0 0 *map[6][2]
1 1 1 0 *0 1 0 0 *map[7][4]
as you can see theres some place that should be '1' but for some reason is not, for example:
map[5][0] and map[7][4] should be '1' because map[6][2] is '1', i dunno whats the problem with my code but my asumption is maybe the coordinate is visited twice, but i already make limitation about the coordinate, so i dont know wheres the problem.
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0
1 1 0 0 1 0 0 0
0 0 1 1 0 0 0 0
1 0 1 0 1 0 0 0
this is the result when the movement limit is 2.
as you can see map[5][0] and map[7][4] is '1' as it should be.
any help will be apreciated, thank you
what did I do wrong?
Stopping, when recursion should go on.
else if (map[wy][wx] != 1) { and map[wy][wx] = 1 are problems.
The test map[wy][wx] != 1 is simple asking, have I been here before? But that is not enough.
The test should be have I been here before in less moves?
If during the tour, it is possible to get to a square in fewer moves, then the recursion from that point needs to go deeper, even though the square has been visited.
Two changes I made help see this, but do not solve it. Leave that for OP. (Note there are many ways to solve this.)
if (wx < 0 || wy < 0 || wx > 7 || wy > 7 || limit == 0) {
return;
// } else if (map[wy][wx] != 1) {
} else if (map[wy][wx] == 0) {
// map[wy][wx] = 1;
map[wy][wx] = limit;
} else {
return;
}
Case #1:
0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 0
1 0 1 0 1 0 0 0
2 1 2 0 0 1 0 0
1 0 1 2 1 0 0 0
0 3 0 1 0 1 0 0
1 0(1)2 1 0 0 0 From this square, recursion should go deeper ...
4 1 2 0(0)1 0 0 ... to get to this square.
I have a file that stores the available seats in a theater. When I read the first few characters, they are garbage. Why is that?
bool readSeat(char** a) {
FILE* file = fopen(SEAT, "r");
if (!file)
{
return 1;
}
char ch;
while (fscanf(file, "%c", &ch) == 1)
{
//save ch into **a above
}
return 0;
}
Here's my file:
00000000000001000000000000111
01011111000000000000001111000
00000000000000000000000000000
00000000001111100000000000111
00011111000000000000001111000
00000000000000000000000000000
00000000000001001111000000111
11100011000000000000001111000
00101101111111111100000000000
00000000000001000000000000111
11111111000000000000001111000
00000000000000000000000000000
00000111111111000000000000111
11111111000000000000001111000
10000000001111000000000000000
Output:
■0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0//...
Update:
While debugging, I noticed the 2 first characters are 'ÿ'(-1) and 'þ'(-2).
I'm having trouble with my school homework. I have a chocolate bar that consists of either black, white or black & white (mixed) squares. I'm supposed to divide it in two groups, one that has only white or black&white pieces and the other that has only black or black&white pieces. Dividing the chocolate bar means cracking it either horizontally or vertically along the line that separates individual squares.
Given a layout of a chocolate bar, I am to find an optimal division which separates dark and white cubes and results in the smallest possible number of pieces, the chocolate bar being not bigger than 50x50 squares.
The chocolate bar is defined on the standard input like this:
first line consists of two integers M (number of rows in chocolate bar) and N (no. of columns), then there M columns each consisting of N characters symbolizing individual squares (0-black, 1-white, 2-mixed)
Some examples of an optimal division, their inputs respectively (correct outputs are 3 and 7):
3 3
1 1 2
1 2 0
2 0 0
4 4
0 1 1 1
1 0 1 0
1 0 1 0
2 0 0 0
My problem is that I managed to work out a solution, but the algorithm I'm using isn't fast enough, if the chocolate bar is big like this for example:
40 40
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 1 2 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 2 1 2 1 2 0 0 1 2 2 0 0 0 0 0 0 0 0 1 1 2 1 2 0 0 0 0 0 0 0 0 0 0
0 0 0 1 2 2 0 1 1 1 1 1 0 0 1 2 2 0 0 0 0 0 1 0 0 2 2 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 2 2 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 1 2 2 0 0 0 1 2 2 1 2 1 0 0 0 0 0 1 2 1 2 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 2 2 1 2 0 0 0 0 0 2 1 2 2 0 0 0 0 0 2 1 2 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 2 2 2 1 1 0 0 0 0 0 2 1 1 1 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 1 1 2 0 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 0 0 0 0 0 0 0 1 2 1 0 0
0 0 0 0 0 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 0 0 0 0 0 0 0 1 2 1 0 0
0 0 0 0 0 0 0 0 0 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
then it takes 10 seconds for my program to solve it (correct solution for that one is 126 and I should be able to solve it in under 2 seconds!)
My algorithm works roughly with some minor optimization like this: iterate through all possible lines where it's possible to cut and then recursively do the same for the 2 newly emerged rectangles, if they cannot be divided anymore, then return 1.
The function after it iterates trough all the possible cuts always returns the minimum, once the minimum is found then store it and if I'd happen to need to solve this rectangle again then just return the value.
I thought that maybe If I happen to have already solved a particular rectangle and now I need to solve one that is one row or column bigger or smaller, then I could somehow use the solution I already have for that one and use it for the new one. But I really don't know how would i implement such a feature.
Right now my algorithm treats it like a completely new unsolved rectangle.
My code so far:
#include <stdio.h>
#include <stdlib.h>
unsigned int M, N;
unsigned int ****pieces; ////already solved rectangles, the value of pieces[y0][x0][y1][x1] is the optimal number of pieces in which the particular rectangle(that has upperleft corner in [x0,y0] and bottomright corner in[x1,y1]) can be divided
int ****checked;
unsigned int inf;
unsigned int minbreaks(int mat[M][N], unsigned int starti, unsigned int startj, unsigned int maxi, unsigned int maxj) {
if (pieces[starti][startj][maxi][maxj] != 0) {
return pieces[starti][startj][maxi][maxj];
} else {
unsigned int vbreaks[maxj - 1];
unsigned int hbreaks[maxi - 1];
for (unsigned int i = 0; i < maxj - 1; i++) {
vbreaks[i] = inf;
}
for (unsigned int i = 0; i < maxi - 1; i++) {
hbreaks[i] = inf;
}
unsigned int currentmin = inf;
for (unsigned int i = starti; i < maxi; i++) {
for (unsigned int j = startj; j < maxj - 1; j++) {
if (mat[i][j] != 2) {
for (unsigned int k = startj + 1; k < maxj; k++) {
if (vbreaks[k - 1] == inf) {
for (unsigned int z = starti; z < maxi; z++) {
if (!checked[i][j][z][k]) {
if (mat[z][k] != 2 && mat[i][j] != mat[z][k]) {
vbreaks[k - 1] = minbreaks(mat, starti, startj, maxi, k) + minbreaks(mat, starti, k, maxi, maxj);
if (vbreaks[k - 1] < currentmin) {
currentmin = vbreaks[k - 1];
}
break;
}
checked[i][j][z][k] = 1;
}
}
}
}
}
}
}
for (unsigned int i = starti; i < maxi - 1; i++) {
for (unsigned int j = startj; j < maxj; j++) {
if (mat[i][j] != 2) {
for (unsigned int k = starti + 1; k < maxi; k++) {
if (hbreaks[k - 1] == inf) {
for (unsigned int z = startj; z < maxj; z++) {
if (!checked[i][j][k][z]) {
if (mat[k][z] != 2 && mat[i][j] != mat[k][z]) {
hbreaks[k - 1] = minbreaks(mat, starti, startj, k, maxj) + minbreaks(mat, k, startj, maxi, maxj);
if (hbreaks[k - 1] < currentmin) {
currentmin = hbreaks[k - 1];
}
break;
}
checked[i][j][k][z] = 1;
}
}
}
}
}
}
}
if (currentmin == inf) {
currentmin = 1;
}
pieces[starti][startj][maxi][maxj] = currentmin;
return currentmin;
}
}
int main(void) {
FILE *file = stdin;
fscanf(file, "%u %u", &M, &N);
int mat[M][N];
pieces = malloc(sizeof (unsigned int***)*M);
checked = malloc(sizeof (int***)*M);
for (unsigned int i = 0; i < M; i++) {//initialize the pieces,checked and mat arrays.
pieces[i] = malloc(sizeof (unsigned int**)*N);
checked[i] = malloc(sizeof (int**)*N);
for (unsigned int j = 0; j < N; j++) {
int x;
fscanf(file, "%d", &x);
mat[i][j] = x;
pieces[i][j] = malloc(sizeof (unsigned int*)*(M + 1));
checked[i][j] = malloc(sizeof (int*)*M);
for (unsigned int y = i; y < M + 1; y++) {
pieces[i][j][y] = malloc(sizeof (unsigned int)*(N + 1));
for (unsigned int x = j; x < N + 1; x++) {
pieces[i][j][y][x] = 0;
}
}
for (unsigned int y = 0; y < M; y++) {
checked[i][j][y] = malloc(sizeof (int)*N);
for (unsigned int x = 0; x < N; x++) {
checked[i][j][y][x] = 0;
}
}
}
}
inf = M * N + 1; //number one bigger than maximal theoretically possible number of divisions
unsigned int result = minbreaks(mat, 0, 0, M, N);
printf("%u\n", result);
return (EXIT_SUCCESS);
}
So anybody has any idea for improvements?
For any arbitrary rectangle, we can know if it contains either no white or no black pieces in O(1) time, with O(M * N) preprocessing of matrix prefix-sums for white and black separately (count 1 for each piece).
We can store potential horizontal and vertical split points separately in two k-d trees for O(log(|splitPoints|) + k) retrieval for an arbitrary rectangle, again preprocessing the entire input.
After that, a general recursive algorithm could look like:
f(tl, br):
if storedSolution(tl, br):
return storedSolution(tl, br)
else if isValid(tl, br):
return setStoredSolution(tl, br, 0)
best = Infinity
for p in vSplitPoints(tl, br):
best = min(
best,
1 +
f(tl, (p.x-1, br.y)) +
f((p.x, tl.y), br)
)
for p in hSplitPoints(tl, br):
best = min(
best,
1 +
f(tl, (br.x, p.y-1)) +
f((tl.x, p.y), br)
)
return setStoredSolution(tl, br, best)
There is a dynamic programming approach to this, but it won't be cheap either. You need to fill in a load of tables giving, for each size and position of rectangle within the main square, the minimum number of divisions necessary to divide up that smaller rectangle fully.
For a rectangle of size 1x1 then answer is 0.
For a rectangle of size AxB look and see if all of its cells are uniform enough that the answer is 0 for that rectangle. If so, fine. If not try all possible horizontal and vertical divisions. Each of these divisions gives you two smaller rectangles. If you work out the answers for all rectangles of size A-1xB and smaller and size AxB-1 and smaller before you try and work out the answers for rectangles of size AxB you all ready know the answers for the two smaller rectangles. So for each possible division, add up the answers for the two smaller rectangles and add one to get the cost for that division. Chose the division that gives you the smallest cost and that gives you the answer for your current AxB rectangle.
Working out the answers for all smaller rectangles before larger rectangles, the very last answer you work out gives you the optimum number of divisions for the full square. The easiest way to work out what the best division is is to keep a little extra information for each rectangle, recording what the best division found was.
For an NxN square there are O(N^4) rectangles - any two points in the square define a rectangle as opposite corners. A rectangle of size O(N)xO(N) has O(N) possible divisions so you have something like an O(N^5) algorithm, or O(N^2.5) if N is the input size since an NxN square has input data of size O(N^2).
(You could also do something very like this by taking your original code and storing the results from calls to minBreaks() so that if minBreaks() is called more than once with the same arguments it simply returns the stored answer instead of recalculating it with yet more recursive calls to minBreaks()).
Thanks to everybody who helped me, my mistake was that in those nested loops I tried to avoid some unnecessary breaks, like this for example
1 1 -> 1 | 1
1 1 1 | 1
1 1 1 | 1
thinking it would speed up the runtime but the correct approach was just simply breaking the chocolate bar always everywhere possible.
Anyway for anyone interested here is my working code:
#include <stdio.h>
#include <stdlib.h>
unsigned int M, N;
unsigned int ****pieces; ////already solved rectangles, the value of pieces[y0][x0][y1][x1] is the optimal number of pieces in which the particular rectangle(that has upperleft corner in [x0,y0] and bottomright corner in[x1,y1]) can be divided
unsigned int inf;
int isOneColor(int mat[M][N], unsigned int starti, unsigned int startj, unsigned int maxi, unsigned int maxj) {
int c = 2;
for (unsigned int i = starti; i < maxi; i++) {
for (unsigned int j = startj; j < maxj; j++) {
if (c == 2) {
if (mat[i][j] != 2) {
c = mat[i][j];
}
} else if (c != mat[i][j] && mat[i][j] != 2) {
return 0;
}
}
}
return 1;
}
unsigned int minbreaks(int mat[M][N], unsigned int starti, unsigned int startj, unsigned int maxi, unsigned int maxj) {
if (pieces[starti][startj][maxi][maxj] != 0) {
return pieces[starti][startj][maxi][maxj];
} else if (isOneColor(mat, starti, startj, maxi, maxj)) {
return pieces[starti][startj][maxi][maxj] = 1;
} else {
unsigned int currentmin = inf;
for (unsigned int j = startj; j < maxj - 1; j++) {
unsigned int c = minbreaks(mat, starti, startj, maxi, j + 1) + minbreaks(mat, starti, j + 1, maxi, maxj);
if (c < currentmin) {
currentmin = c;
}
}
for (unsigned int i = starti; i < maxi - 1; i++) {
unsigned int c = minbreaks(mat, starti, startj, i + 1, maxj) + minbreaks(mat, i + 1, startj, maxi, maxj);
if (c < currentmin) {
currentmin = c;
}
}
pieces[starti][startj][maxi][maxj] = currentmin;
return currentmin;
}
}
int main(void) {
FILE *file = stdin;
//FILE *file = fopen("inputfile", "r");
fscanf(file, "%u %u", &M, &N);
int mat[M][N];
pieces = malloc(sizeof (unsigned int***)*M);
for (unsigned int i = 0; i < M; i++) {
pieces[i] = malloc(sizeof (unsigned int**)*N);
for (unsigned int j = 0; j < N; j++) {
int x;
fscanf(file, "%d", &x);
mat[i][j] = x;
pieces[i][j] = malloc(sizeof (unsigned int*)*(M + 1));
for (unsigned int y = i; y < M + 1; y++) {
pieces[i][j][y] = malloc(sizeof (unsigned int)*(N + 1));
for (unsigned int x = j; x < N + 1; x++) {
pieces[i][j][y][x] = 0;
}
}
}
}
inf = M * N + 1; //number that is bigger by one than maximal theoretically possible number of divisions
unsigned int result = minbreaks(mat, 0, 0, M, N);
printf("%u\n", result);
return (EXIT_SUCCESS);
}
I am trying to expand a PBM image of say, 5x5 to 10x10 but I have hit somewhat of a road block.
I have read the header, and height / width with the following
int fSize = size(inFile);
char *inputBuffer = malloc(fSize);
int pbmHeight, pbmWidth;
memset(inputBuffer, 0, fSize);
if (!fgets(inputBuffer, sizeof(inputBuffer), inFile)) {
fprintf(stderr, "Unable to read image format.\n");
exit(-1);
}
if (inputBuffer[0] != 'P' || inputBuffer[1] != '4') {
fprintf(stderr, "Invalid image format.\n");
exit(-1);
}
if (fscanf(inFile, "%d %d", &pbmWidth, &pbmHeight) != 2) {
fprintf(stderr, "Invalid image size.\n");
exit(-1);
}
int i;
int bitRemainder = ((pbmWidth % 8) != 0 ? 8 - (pbmWidth % 8) : 0);
Then read the data
while (fgetc(inFile) != '\n');
fread(inputBuffer, pbmHeight * pbmWidth, 1, inFile);
I have a second char *secondPBM which contains another PBM image of the same height / width
What I am looking to do, is iterate through each bit of the 1st image, the 2nd image, compare, and then output specific bits to an output
For example, here is image 1 (Note the image width is 5, but it needs to fill 1 byte)
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Here is image 2
1 1 1 1 1 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 0 0
Based on image 1, and image 2 bits I need to output 4 bits so the new image size becomes 2 * pbmWidth
So for example, the output should be (a 10x10 image, with 6 bits set to 0 to fill the remaining byte) (The bit values in the 2nd image will be set with another piece of code and and isn't exactly what is listed here)
1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0
I'm reading the bit value of the 1st and 2nd image like this
Here are my getBitValue and setBitValue functions
int getBitVal(unsigned char *keyStrBin, int keyIndex) {
int keyMod = keyIndex % 8;
int keyIn = keyIndex / 8;
return (((keyStrBin[keyIn]) >> (7 - (keyMod))) & 1);
}
void setBitVal(unsigned char *keyC, int keyIndex) {
int keyMod = keyIndex % 8;
int keyIn = keyIndex / 8;
keyC[keyIn] |= (1 << (7 - (keyMod)));
}
int newWidth = 2 * pbmWidth;
int newHeight = 2 * pbmHeight;
int totalBits = newWidth * newHeight;
int newFileSize = (totalBits % 8 == 0) ? (totalBits / 8) : (totalBits / 8 + 1);
int bitRemainder = ((pbmWidth % 8) != 0 ? 8 - (pbmWidth % 8) : 0);
int newbitRemainder = ((newWidth % 8) != 0 ? 8 - (newWidth % 8) : 0);
for (i = 0; k = 0; i < newHeight * (newWidth + newbitRemainder); i++, k++) {
if (i != 0 && i % (pbmWidth - 1) == 0) {
i += (bitRemainder - 1);
}
if (k != 0 && k % (newWidth - 1) == 0) {
k += (newbitRemainder - 1);
}
if (getBitVal((unsigned char *)inputBuffer, i) == 0 && getBitVal(keyBuffer, k) == 0) {
// Code to set 1 bit to black in the 1st row, then set another bit to black in the 2nd row. For example, The bit at index 0 becomes a 1, then the bit at index 17 becomes a 1 (Making a 2x2 square)
// 1 0
// 0 1
// Then move on to the 2nd bit (or bit index 1) of the input image, compare.
}
else if (getBitVal((unsigned char *)inputBuffer, i) == 0 && getBitVal(keyBuffer, k) == 1) {
}
else if (getBitVal((unsigned char *)inputBuffer, i) == 1 && getBitVal(keyBuffer, k) == 0) {
}
else if (getBitVal((unsigned char *)inputBuffer, i) == 1 && getBitVal(keyBuffer, k) == 1) {
}
I can't seem to figure out how to set the bits in the comparative if statements.
I tried this, but it doesn't seem to work right.
setBitVal(pbmOut1Buffer, k * 2);
setBitVal(pbmOut1Buffer, (k * 2) + (newWidth + newbitRemainder) + 1);
Any help with this would be greatly appreciated. Thank you all in advance.
That's my homework, making a sudoku game. I have done my algorithm but it's entering infinite loop. I didn't understand why.
I am trying create a random number and control it for find true number. Checking all columns and rows for find same number as like as our random number if it is, it's changing test number and if test has changed trying find another number for true number. Simple sudoku logic.
#include <stdio.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
int num, col, row, row2, col2, test = 0;
int sudo[9][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
for (row = 0; row <= 8; row++) {
for (col = 0; col <= 8; col++) {
do {
test = 0;
num = rand() % 9 + 1;
//control
for (col2 = 0; col2 <= 8; col2++) {
if (num == sudo[col2][row]) {
test++;
}
}
for (row2 = 0; row2 <= 8; row2++) {
if (num == sudo[col][row2]) {
test++;
}
}
} while (test > 0);
sudo[col][row] = num;
}
}
//print
for (row = 0; row <= 8; row++) {
for (col = 0; col <= 8; col++) {
printf(" %d ", sudo[col][row]);
if (col == 2 || col == 5) {
printf(" | ");
}
}
if (row == 2 || row == 5) {
printf("\n---------------------------------");
}
printf("\n");
}
}
Your algorithm is broken, and I can demonstrate why. If it were possible to fill in a sudoku puzzle this way, it would also be trivial to solve a sudoku puzzle this way, which it is not.
Essentially your code boils down to the following. I've added early exits on the inner for-loops to stop searching once we find the number already in the current row or column (and actually made sense of what 99.9% of the world thinks of concerning "rows" and "columns" in a NxN matrix):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NSIZE 9
void print_matrix(int const ar[][NSIZE])
{
for (size_t i=0; i<NSIZE; ++i)
{
for (size_t j=0; j<NSIZE; ++j)
{
fputc('0' + ar[i][j], stdout);
fputc(' ', stdout);
}
fputc('\n', stdout);
}
}
int main()
{
srand((unsigned)time(NULL));
int sudo[NSIZE][NSIZE] = {{0}};
int row, col;
for(row=0;row<NSIZE;++row)
{
for(col=0;col<NSIZE;++col)
{
int row2 = 0, col2 = 0, num;
printf("Trying ");
do
{
num = rand()%9+1;
printf("%d ", num);
for(row2=0; row2<NSIZE && num!=sudo[row2][col]; ++row2);
for(col2=0; col2<NSIZE && num!=sudo[row][col2]; ++col2);
}
while (row2 < NSIZE || col2 < NSIZE);
fputc('\n', stdout);
sudo[row][col] = num;
printf("sudo[%d][%d] = %d\n", row, col, num);
print_matrix(sudo);
}
}
}
As the loops progress, we report what number we're trying, and what the matrix looks like upon placement of a keeper. For example, a test run of the above initially can look like this:
Trying 8
sudo[0][0] = 8
8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
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 4 6 7
sudo[0][6] = 7
8 1 9 6 3 4 7 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 1 3 1 3 4 1 3 8 4 9 3 8 1 4 7 9 3 8 8 8 4 9 6 5
sudo[0][7] = 5
8 1 9 6 3 4 7 5 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
and this continues for perhaps a while. But eventually, unless you get extraordinarily lucky, the following is bound to happen (and this one went pretty deep before the wheels fell off):
Trying 1 6 3 4
sudo[6][6] = 4
8 1 9 6 3 4 7 5 2
1 3 5 4 8 6 2 7 9
3 6 4 8 7 9 5 2 1
7 9 1 2 4 5 3 8 6
4 7 3 9 2 8 6 1 5
5 4 2 3 6 1 8 9 7
6 8 7 1 9 3 4 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Note we're about to try and populate sudo[6][7]. To do that we must find a number that is not in the sudo[r][7] column already, nor the sudo[6][c] row. But looking at the numbers already in those positions.
sudo[r][7] : {5,7,2,8,1,9}
sudo[6][c] : {6,8,7,1,9,3,4}
Therefore we're looking for a number from 1..9 that is NOT in: {1,2,3,4,5,6,7,8,9}, which we're NEVER going to find.
The algorithm is broken. There is a reason backtracking is used for tasks like this.