File tree Expand file tree Collapse file tree 1 file changed +87
-0
lines changed
Expand file tree Collapse file tree 1 file changed +87
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ class Queen {
4+ int a [][] = new int [10 ][10 ];
5+ int n , i ,c ,f ,m ;
6+ Queen ()
7+ {
8+ m =1 ;
9+ }
10+
11+ void input () {
12+ Scanner s = new Scanner (System .in );
13+ System .out .println ("Enter n:" );
14+ n = s .nextInt ();
15+ }
16+
17+ int condition (int i , int j , int n ) {
18+ int k , l ;
19+ for (int z = 0 ; ++c >0 &&z <= i - 1 ; z ++) {
20+ if (a [z ][j ] == 1 )
21+ return 0 ;
22+ }
23+
24+ for (k = i - 1 , l = j - 1 ; ++c >0 &&k >= 0 && l >= 0 ; k = k - 1 , l = l - 1 ) {
25+ if (a [k ][l ] == 1 )
26+ return 0 ;
27+ }
28+
29+ for (k = i - 1 , l = j + 1 ;++c >0 && k >= 0 && l <= n ; k = k - 1 , l = l + 1 ) {
30+ if (a [k ][l ] == 1 )
31+ return 0 ;
32+ }
33+
34+ return 1 ;
35+ }
36+
37+ void logic () {
38+ if (i == n && n !=1 ) {
39+ System .out .println ("the possible solution:" +m );
40+ m ++;
41+ print ();
42+ f =1 ;
43+ }
44+ int v =0 ;
45+ for (int j = 0 ; j < n ; j ++) {
46+ if (condition (i , j , n ) == 1 ) {
47+ a [i ][j ] = 1 ;
48+ v =j ;
49+ i = i + 1 ;
50+ logic ();
51+
52+ i = i - 1 ;
53+ a [i ][v ] = 0 ;
54+
55+ }
56+
57+ }
58+
59+ }
60+
61+ void print () {
62+ for (int i = 0 ; i < n ; i ++) {
63+ for (int j = 0 ; j < n ; j ++)
64+ {
65+ if (a [i ][j ]==1 )
66+ System .out .print (" " +"Q" );
67+ else
68+ System .out .print (" " +"-" );
69+ }
70+
71+ System .out .println ();
72+ }
73+ System .out .println ();
74+
75+ }
76+ }
77+
78+ class Main {
79+ public static void main (String args []) {
80+ Queen q = new Queen ();
81+ q .input ();
82+ q .logic ();
83+ if (q .f ==0 )
84+ System .out .println ("no" );
85+
86+ }
87+ }
You can’t perform that action at this time.
0 commit comments