Note that there are some explanatory texts on larger screens.

plurals
  1. PO8-Queens snippet
    primarykey
    data
    text
    <p>I have currently learning backtracking and got stuck on the 8-queen problem, I am using a 8x8 matrix and I think I've got some problems regarding the matrix passing to functions, any help would be highly apreciated.I wouldn't mind if anyone would bring any optimisation to the code, thanks.</p> <p>here is my code.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define MAX 7 //void azzera(int **mat); void posiziona(int **mat, int r,int c); void stampa(int **mat); int in_scacchi(int **mat,int r ,int c); int main(int argc, char *argv[]) { int i=0,j=0; int **mat=(int **)malloc(sizeof(int *)*MAX); for(i=0;i&lt;=MAX;i++){ mat[i]=(int *)malloc(MAX*sizeof(int)); for(j=0;j&lt;=MAX;j++){ mat[i][j]=-1; } } printf("insert pos of the first queen on the first row (1-8) :"); scanf("%d",&amp;i); i-=1; mat[0][i]=1; posiziona(mat,1,0); stampa(mat); system("PAUSE"); return 0; } /*void azzera(int **mat){ int i=0,j=0; for(i=0;i&lt;=MAX;i++){ for(j=0;j&lt;=MAX;j++){ mat[i][j]=-1; } } }*/ void stampa(int **mat){ int i,j; for(i=0;i&lt;=MAX;i++){ for(j=0;j&lt;=MAX;j++){ printf(" %d",mat[i][j]); } printf("\n"); } } void posiziona(int **mat, int r,int c){ int i=0,riga=1,flag_col=-1,flag_riga=-1; if(riga&lt;=7&amp;&amp;flag_riga!=1){ if(flag_riga==1){ flag_riga=-1; posiziona(mat,r+1,0); } else if(in_scacchi(mat,r,c)==1){ if(c==MAX) posiziona(mat,r-1,0); posiziona(mat,r,c+1); } else{ flag_riga=1; } } } int in_scacchi(int **mat,int r ,int c){ int i,j,k,m; int flag=0; //col for(i=0;i&lt;r;i++){ for(j=0;j&lt;=c;j++){ if(((mat[i][j]==1)&amp;&amp;(c==j))) return 1; } } //diag \ for(i=0;i&lt;MAX-r;i++){ for(j=0;j&lt;=MAX-c;j++){ if(mat[MAX-r-i][MAX-c-j]==1) return 1; } } //antidiag for(i=r+1;i&lt;=MAX;i++){ for(j=c+1;j&lt;=MAX;j++){ if(mat[r-i][c+j]==1) { return 1; } } } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload