Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package queens_8;
- public class board {
- private static int board[][];
- private int numQueens;
- public board() {//default constructor
- numQueens = 0;
- board = new int[8][8];
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 8; j++) {
- board[i][j] = 0;
- }
- }
- }
- public int getnumQueens() {
- return numQueens;
- }
- public void start() {
- solve(0);
- }
- public boolean solve(int numQueens) {
- if (numQueens == 8) {
- System.out.println("DONE");
- this.display();
- return true;
- } else {
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 8; j++) {
- if (isvalidMove(i, j) == 0) {
- this.placeQueen(i, j, 0);
- numQueens++;
- if (solve(numQueens)) {
- return true;
- } else {
- this.placeQueen(i, j, 1);
- numQueens--;
- }
- }
- }
- }
- }
- return false;
- }
- public static int isvalidMove(int x, int y) {
- for (int i = 0; i < 8; i++) {
- if (get(x, i) == 1) {
- return -1;
- }
- if (get(i, y) == 1) {
- return -1;
- }
- }
- // check diag
- for (int j = 0; j < 8; j++) {
- if (get(x - j, y - j) == 1) {
- return -1;
- }
- if (get(x - j, y + j) == 1) {
- return -1;
- }
- if (get(x + j, y - j) == 1) {
- return -1;
- }
- if (get(x + j, y + j) == 1) {
- return -1;
- }
- }
- return 0;
- }
- public int placeQueen(int x, int y, int type) {
- if (type == 0) {
- board[x][y] = 1;
- numQueens++;
- return 0;
- } else if (type == 1) {
- board[x][y] = 0;
- return 0;
- }
- System.err.println("Wrong type");
- return -3;
- }
- public static int get(int x, int y) {
- if (x < 0 || y < 0 || x > 7 || y > 7) {
- return -1;
- }
- return board[x][y];
- }
- public void display() {
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 8; j++) {
- System.out.print(this.get(i, j) + " ");
- }
- System.out.println("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement