1- The Knight can move in the shape of the letter, 'L', over two in one direction and then over one in a perpendicular direction.
2- If the Knight rests at the square marked X.
3- The board is represented by an 8-by-8 two-dimensional array.
4- The Knights can be placed on the board as specified above so that no two Knights are attacking each other.
5- the most important thing is to complete number 4 (according to the code(queen) below need to change for Knight

public class Queens {
    int[] x;
    public Queens(int N) {
        x = new int[N];
    }
    public boolean canPlaceQueen(int r, int c) {
       /*
       Returns TRUE if a queen can be placed in row r and column c.
       Otherwise it returns FALSE. x[] is a global array whose first (r-1)
       values have been set.
       */
        for (int i = 0; i < r; i++) {
            if (x[i] == c || (i - r) == (x[i] - c) ||(i - r) == (c - x[i])) 
            {
                return false;
            }
        }
        return true;
    }
    public void printQueens(int[] x) {
        int N = x.length;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (x[i]==j) {
                    System.out.print("Q ");
                } else {
                    System.out.print("* ");
                }
            }
            System.out.println();
        }
        System.out.println();
    }
    /*
    public void placeNqueens(int r, int n) {
      //Using backtracking this method prints all possible placements of n
      //queens on an n x n chessboard so that they are non-attacking.
        for (int c = 0; c < n; c++) {
            if (canPlaceQueen(r, c)) {
                x[r] = c;
                if (r == n-2 && c==n-1 || r==n-1 && c==n-2 ) {
                    printQueens(x);
                } else {
                    placeNqueens(r+1, n);
                }
            }
        }
    }
    */
    public void placeNqueens(int r, int n) {
        /*
         Using backtracking this method prints all possible placements of n
         queens on an n x n chessboard so that they are non-attacking.
         */
        for (int c = 0; c < n; c++) {
            if (canPlaceQueen(r, c)) {
                x[r] = c;
                if (r == n-1) {
                    printQueens(x);
                } else {
                    placeNqueens(r+1, n);
                }
            }
        }
    } 
    public void callplaceNqueens() {
        placeNqueens(0, x.length);
    }
    public static void main(String args[]) {
        Queens Q = new Queens(8);
        Q.callplaceNqueens(); 
    }
}

asked 04 Nov '17, 18:04

hunted's gravatar image

hunted
111

edited 04 Nov '17, 18:43

Mosharraf%20Hosain's gravatar image

Mosharraf Hosain ♦
16618

Be the first one to answer this question!
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×204
×1

question asked: 04 Nov '17, 18:04

question was seen: 76 times

last updated: 04 Nov '17, 18:43