-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueens.java
More file actions
156 lines (145 loc) · 4.23 KB
/
Copy pathQueens.java
File metadata and controls
156 lines (145 loc) · 4.23 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.*;
import java.util.*;
/**
* Finds all the solutions of Non attacking queens on a 8x8 board
*
* @Zach Doster
* @1/18/17
*/
public class NonAttackingQueens
{
// instance variables - replace the example below with your own
static private int SIZE =8;
private int board[][] = new int[SIZE][SIZE];
private ArrayList<Integer> moves = new ArrayList<Integer>();
/**
* initialize the board
*/
public NonAttackingQueens()
{
for(int i = 0; i < SIZE; i++)
for(int k =0; k<SIZE; k++)
board[k][i]=0;
}
/**
* Main method
*/
static public void main(String[] args)
{
// put your code here
NonAttackingQueens b = new NonAttackingQueens();
b.solve();
}
public void print()
{
for(int i = 0; i < SIZE; i++)
{
for(int k = 0; k< SIZE; k++)
{
System.out.print(board[k][i]);
}
System.out.println();
}
System.out.println("********");
}
//check to see if it is ok to place a queen
public boolean checkQueen(int row, int col)
{
//check vertical
for(int i = 0; i <SIZE; i++)
{
if(board[row][i]==1)
return false;
}
//check upper left diagnol
for(int i = 0; i <= row && i <= col; i++)
{
if(board[row-i][col-i]==1)
return false;
}
//check upper right diagnol
for(int i = 0; i <= 7 - row && i <= 7- col; i++)
{
if(board[row + i][col + i]==1)
return false;
}
return true;
}
//solve the nonAttackingQueens
public void solve()
{
int total=0,x=0,y=0,count=0, runs = 0;
//while the board doesnt have enought pieces
while(total != 8)
{
runs++;
total=0;
//count the number of queens
for(int i = 0; i < SIZE; i++)
{
for(int k = 0; k< SIZE; k++)
{
if(board[i][k]==1)
{
total++;
}
}
}
//Solution is found
if(total ==8)
{
total=0;
count++;
System.out.println(count + ".");
System.out.println(runs);
print();//print soludtion
//end if all solutions found
if(count>91)
return;
//move on to next solution
else
{
for(int b = 0; b < SIZE; b++)
{
moves.add(moves.get(moves.size()-8));//add solution to list
}
y--;//move back a row
x = moves.get(moves.size()-1).intValue();//move to last spot
board[x][y]=0;//set spot to empty
moves.remove(moves.size()-1);//remove last spot
if(x!=7)//move to next spot
x++;
if(x==7)//move back another row
{
y--;
x = moves.get(moves.size()-1).intValue();
board[x][y]=0;
x++;
moves.remove(moves.size()-1);
}
}
}
//check to see if you can place the queen
if(y < 8 && x < 8 && checkQueen(x,y))
{
Integer t = new Integer(x);
moves.add(t);
board[x][y]=1;
y++;
x=0;
}
//move to the next row if x is at the edge
else if(x>7)
{
y--;
x = moves.get(moves.size()-1).intValue();
board[x][y]=0;
x++;
moves.remove(moves.size()-1);
}
//move to the right
else
x++;
}
}
}