Programming Challenge Question (N King) with Solution

0
2210

Following is The Programming Challenge given by Facebook when you apply for Job.  Solution is also posted.My Solution is in Java but you can solve this test in following Languages.

C, C++, Java, C#, PHP, Ruby, Python, Perl, Haskell, Clojure, Scala

Question 1 / 1 (N King)
You have an N x N chessboard and you wish to place N kings on it. Each row and column should contain exactly one king, and no two kings should attack each other (two kings attack each other if they are present in squares which share a corner).
The kings in the first K rows of the board have already been placed. You are given the positions of these kings as an array pos[ ]. pos[i] is the column in which the king in the ith row has already been placed. All indices are 0-indexed. In how many ways can the remaining kings be placed?

Input:
The first line contains the number of test cases T. T test cases follow. Each test case contains N and K on the first line, followed by a line having K integers, denoting the array pos[ ] as described above.

Output:
Output the number of ways to place kings in the remaining rows satisfying the above conditions. Output all numbers modulo 1000000007.

Constraints:
1 <= T <= 20
1 <= N <= 16
0 <= K <= N
0 <= pos_i < N
The kings specified in the input will be in different columns and not attack each other.

Sample Input:
5
4 1
2
3 0

5 2
1 3
4 4
1 3 0 2
6 1
2

Sample Output:
1
0
2
1
18

Explanation:
For the first example, there is a king already placed at row 0 and column 2. The king in the second row must belong to column 0. The king in the third row must belong to column 3, and the last king must beong to column 1. Thus there is only 1 valid placement.

For the second example, there is no valid placement.
Download sample testcases as zip [‘Compile & Test’ will run your code against these testcases]

Solution:

[java]

import java.util.Set;
import java.util.Scanner;
import java.util.HashSet;

public class Solution{
Set<Integer> filledColumns = new HashSet<Integer>();
boolean chess_board[][];
public static void main(String args[]) {
readInput();
}

Solution() {
}

Solution(int n) {
chess_board = new boolean[n][n];
}
static void readInput() {
Scanner myscan = new Scanner(System.in);
int t = myscan.nextInt();
for (int i = 0; i < t; ++i) {
int n = myscan.nextInt();
int k = myscan.nextInt();
boolean chess_board[][] = new boolean[n][n];
for (int row = 0; row < k; ++row) {
int col = myscan.nextInt();
chess_board[row][col] = true;
}
Solution s = new Solution();
s.set_chess_board(chess_board);
int ways = s.methodToPlace(k – 1);
System.out.println(ways);
}
}

int methodToPlace(int k) {
if (k == chess_board.length – 1) {
return 1;
}
int totalMethods = 0;
for (int pos = 0; pos < chess_board.length; ++pos) {
int ways = 1;
if (!isAttack(k + 1, pos)) {
chess_board[k + 1][pos] = true;
filledColumns.add(pos);
ways *= methodToPlace(k + 1);
chess_board[k + 1][pos] = false;
filledColumns.remove(pos);
} else {
ways = 0;
}
totalMethods += ways;
}
return totalMethods;
}

void printArray() {
for (int i = 0; i < chess_board.length; ++i) {
for (int j = 0; j < chess_board.length; ++j) {
System.out.print(chess_board[i][j] + " ");
}
System.out.println();
}
}

void set_chess_board(boolean[][] chess_board) {
this.chess_board = chess_board;
for (int i = 0; i < chess_board.length; ++i) {
for (int j = 0; j < chess_board.length; ++j) {
if (chess_board[i][j]) {
filledColumns.add(j);
}
}
}
}

boolean isAttack(int row, int col) {
if (filledColumns.contains(col)) {
return true;
}
if ((col > 0 && row > 0 && chess_board[row – 1][col – 1])
|| (col < chess_board.length – 1 && row > 0 && chess_board[row – 1][col + 1])) {
return true;
}
return false;
}
}

[/java]

Download Solution.zip having .java file.

LEAVE A REPLY

Please enter your comment!
Please enter your name here