Ηelp... I have to make a program that fills a board perfect with numbers.
The first cell should always starts with number 1.
Each number should be placed only: vertically (three cells) or levelly (three cells) or across(two cells) of the previous number.
E.X.
if the given number is 5 then the result:
Code:
1 19 16 4 20
14 24 8 11 23
17 5 21 18 6
2 10 15 3 9
13 25 7 12 22
Here is my attempt but i can see the mistake.
Code:
#include<stdio.h>
#include<math.h>
int table (int x,int y,int s,int n);
int a[5][5];
int main (void){
int i,j,m;
/*fill the table with zero*/
for(i=0;i<5;i++){
for(j=0;j<5;j++){
a[i][j]=0;
}
}
/*get number*/
scanf("%d",&m);
m=m*m;
/*function call*/
table(0,0,1,m);
/*print the board*/
for(i=0;i<sqrt(m);i++){
for(j=0;j<sqrt(m);j++){
printf("%d ",a[i][j]);
}
putchar('\n');putchar('\n');putchar('\n');
}
getchar();getchar();
return 0;
}
/*function that fills the board*/
int table (int x,int y,int s,int n){
/*check conditions*/
if((s<=n)&&(x<sqrt(n))&&(x>=0)&&(y<sqrt(n))&&(y>=0)&&(a[x][y]==0)){
a[x][y]=s;
if(table(x,y+3,s+1,n)==1){ /*check the next number three cells horizontally*/
return 1; /*return 1 if every condition is correct*/
}
else if (table(x+2,y+2,s+1,n)==1){
return 1;
}
else if (table(x+3,y,s+1,n)==1){
return 1;
}
else if (table(x+2,y-2,s+1,n)==1){
return 1;
}
else if (table(x-3,y,s+1,n)==1){
return 1;
}
else if (table(x-2,y-2,s+1,n)==1){
return 1;
}
else if (table(x,y-3,s+1,n)==1){
return 1;
}
else if (table(x-2,y+2,s+1,n)==1){
return 1;
}
else{ /*if no condition is correct then delete the number and return 0*/
a[x][y]=0; /*in order to check somewhere else*/
return 0; }
}
if(s<n)
return 2;
return 1;
}