Help

My project was to create an 8 Queen with a chessboard that is 8x8. My problem is, i can't seem to understand where to put the command that will output the location of the 8 queens inside the 8x8 chessboard. If someone can help me, I would really appreciate that. Here's the code I use.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
    #include<cstdlib>
    #include <cmath>
    using namespace std;

    bool ok(int q[], int col){
        for(int i=0;i<col;i++){ 
         if((q[i] == q[col]) || (abs(q[col] - q[i]) == (col - i))) 
         {   return false;
         }
       }
         return true;
    }
   void backtrack(int &col){
        col--;
        if(col==-1) exit(1);
       }

   void print(int q[])
    {  static int count =0;
       count++;
       int i,j,board[8][8]={0};
       cout<<"#"<<count<<endl;
       for( i=0;i<8;i++)
       {
         board[q[i]][i]=1;           
       }
       
       for(i=0;i<8;i++)                 
        { for(j=0;j<8;j++)
             cout<<board[i][j]<<" ";
             
           cout<<endl;
        }
       
    }

    int main()
    {
      int q[8]; q[0]=0;
      int c=1;
      
      	

      bool from_backtrack=false;
      while(1){
        while(c<8){
         if(!from_backtrack) 
          q[c]=-1;   
            from_backtrack=false;
              while(q[c]<8){ 
                q[c]++;
                 
                
                    while(q[c]==8)
                     { backtrack(c);
                       q[c]++;
                                  
    				}
                 
                  if(ok(q, c))
                     break;
                                   
          }
          c++; 
     }
     print(q); 
     system("pause");
     backtrack(c);
     from_backtrack=true;
    }
   
}
             
Last edited on
are you having problems with printing the board, or solving (there are 92 solutions) ?
When i run the program, it just prints 1 and 0. Its like the 1 is the Queen the 0's are the chess boards. And I don't know how change the 1 to Q's and the 0's to a borders and make it look like a chess board.
did you solved it yourself?
you should post a sample text file, which kind of out output you want to see.
Topic archived. No new replies allowed.