-1

I have to create a program in C that will be checking for a solution of Knight problem (in chess) for every possible starting point in the chess board. Sadly after writing everything down it won't compile and after searching for a long time, I wasnt able to find any solution.

#include <stdio.h>
void print(int **ruchytab,int size);
void zewnetrzne(int size);
int knight(int **ruchytab,int x,int y,int ktory,int size);
int move(int **ruchytab,int x,int y,int wariant,int *newx,int *newy,int size);
int main()
{
    int size=5;
    zewnetrzne(size);
    return 0;
}
void print(int **ruchytab,int size)
{
    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size;j++)
            printf("%2.d ",ruchytab[i][j]);
        putchar('\n');
    }
}
void zewnetrzne(int size)
{
    int ruchytab[size][size];
    for(int j=0;j<size;j++)          //cleaning tab
    {
        for(int i=0;i<size;i++)
            ruchytab[j][i]=0;
    }
    for(int a=0;a<size;a++)          //diffrent start points
        for(int b=0;b<size;b++)
        {
            knight(ruchytab,a,b,1,size);
            for(int j=0;j<size;j++)  //cleaning tab
            {
                for(int i=0;i<size;i++)
                    ruchytab[j][i]=0;
            }
        }
}
int knight(int **ruchytab,int x,int y,int ktory,int size)
{
    int newx,newy;
    ruchytab[x][y]=ktory;
    if(ktory>=size*size) //we have only n^2 possible moves ,we have to be everywhere only once
    {
        print(ruchytab,size);
        return 1;
    }
    else
    {
        for(int war=1;war<=8;war++)
            if(move(ruchytab,x,y,war,&newx,&newy,size)==1)
                if(knight(ruchytab,newx,newy,ktory+1,size)==1)
                    return 1;
    }
    return 0;
}
int move(int **ruchytab,int x,int y,int wariant,int *newx,int *newy,int size)
{
    switch(wariant)         //8 diffrent moves
    {
        case 1:
            *newx=x-1;
            *newy=y-2;
            break;
        case 2:
            *newx=x+1;
            *newy=y-2;
            break;
        case 3:
            *newx=x+2;
            *newy=y-1;
            break;
        case 4:
            *newx=x+2;
            *newy=y+1;
            break;
        case 5:
            *newx=x+1;
            *newy=y+2;
            break;
        case 6:
            *newx=x-1;
            *newy=y+2;
            break;
        case 7:
            *newx=x-2;
            *newy=y+1;
            break;
        case 8:
            *newx=x-2;
            *newy=y-1;
            break;
    }
    if(*newx>=0 && *newx <size && *newy>=0 && *newy<size && ruchytab[*newx][*newy]==0) //checking if the move is possible and if the place was visited already
        return 1;
    else
        return 0;
}
marko
  • 9,029
  • 4
  • 30
  • 46
jakubby
  • 21
  • 4

2 Answers2

1

just copy and past it same homework some years ago :) i still have it

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROWS 8
#define COLS 8
#define NUMBER_OF_QUEEN 8

typedef struct
{
    int col;
    int row;
} queen_t;

void getAllPossibleQueenLand(queen_t queens[] , int*count);
int canPlaceQueen(queen_t queen1 , queen_t queen2);
int checkAllTheQueenTillNow(queen_t queens[] , int length);

void main()
{
    int count = 0;
    int numberOfIteration = 0;
    queen_t queens[NUMBER_OF_QUEEN];
    queens[0].col = 0;


    while(queens[0].col != COLS){
        getAllPossibleQueenLand(queens , &count , &numberOfIteration);
    }

    printf("number of iteration is : %d \n" , count);

    system("pause");

}

void getAllPossibleQueenLand(queen_t queens[] , int *count , int    *numberOfIteration)
{
    if(queens[(*numberOfIteration)].row != (*numberOfIteration))
    {
        queens[(*numberOfIteration)].row = (*numberOfIteration);
        queens[(*numberOfIteration)].col = 0;
    }

if(queens[0].col == COLS)
{
    return;
}

else if(queens[(*numberOfIteration)].col == COLS)
{
    queens[(*numberOfIteration)].col = 0;
    queens[(*numberOfIteration) - 1].col++;
    (*numberOfIteration)--;
    getAllPossibleQueenLand(queens , count , numberOfIteration);
    return;
}

else if(checkAllTheQueenTillNow(queens , (*numberOfIteration) + 1) == 0 )
{
    queens[(*numberOfIteration)].col++;
    getAllPossibleQueenLand(queens , count , numberOfIteration);
    return;
}
else if( (*numberOfIteration) != NUMBER_OF_QUEEN - 1)
{
    (*numberOfIteration)++;
    getAllPossibleQueenLand(queens , count , numberOfIteration);
}
else{
    //found another possible place .. did the bonuse!!
    (*count)++;
    queens[(*numberOfIteration)].col = 0;
    queens[(*numberOfIteration)-1].col++;
    (*numberOfIteration)--;
}

}

int checkAllTheQueenTillNow(queen_t queens[] , int length)
{
int i;
for(i = 0 ; i < length - 1 ; i++)
{

    if(canPlaceQueen(queens[i] , queens[length-1]) == 0)
    {
        return 0;
    }

}

return 1;

}

  int canPlaceQueen(queen_t queen1 , queen_t queen2)
{
    int i;
    for(i = 0 ; queen1.col - i > 0 || queen1.col + i < COLS || queen1.row - i > 0 || queen1.row + i < ROWS ; i++)
{
    if(((queen1.row + i == queen2.row) &&(queen1.col == queen2.col))     ||
       ((queen1.row - i == queen2.row) &&(queen1.col == queen2.col))     ||
       ((queen1.row == queen2.row) &&(queen1.col + i == queen2.col))     ||
       ((queen1.row == queen2.row) &&(queen1.col - i == queen2.col))     ||
       ((queen1.row + i == queen2.row) &&(queen1.col + i == queen2.col)) ||
       ((queen1.row + i == queen2.row) &&(queen1.col - i == queen2.col)) ||
       ((queen1.row - i == queen2.row) &&(queen1.col + i == queen2.col)) ||
       ((queen1.row - i == queen2.row) &&(queen1.col - i == queen2.col)))
    {
        return 0;
    }

}

return 1;

}

Ori Yampolsky
  • 125
  • 13
  • 1
    Would be awesome if u found error in my solution , I want to know what I did wrong. But anyway thank you. – jakubby Nov 21 '15 at 19:00
1

You cannot pass the array:

int ruchytab[size][size];

to a function accepting an int** pointer. An N-dimensional array is internally represented as a pointer to a sequential block of memory of size * size integers (similar to a 1-dimensional array).

In order to make your code work you would need to change the function signature to accept an int* instead, and linearize the 2-D array access as follows:

void print(int *ruchytab,int size) {
   ...
   ruchytab[i*size + j];
}

If you want to keep the [][] syntax there are approaches to achieve and you can read about those in the related questions.

simpel01
  • 1,792
  • 12
  • 13
  • Sadly the related questions seem to give mostly questions related to the knight problem. Perhaps you can link one or two of them? – CompuChip Nov 21 '15 at 19:32
  • I though about 2-dimensional array like a 1-dimensional array so everyone index of that array is a pointer to the vaule ,but in my case the pointer points pointer to the 1-dimensional array . Sadly I am kind of new to the programming and your answer didnt make it clear :/ If linked the right site it would be AWESOME !!! Anyway ty ! EDIT. So I have to use malloc function ? – jakubby Nov 21 '15 at 19:41