Computer Science Worksheet
Computer Science Worksheet
: 09
1 : Queens On Board
2 : Find the number of ways that a given integer, X , can be expressed as the sum of the powers of unique, N
natural numbers.
For example, if X = 13 and N = 2 , we have to find all combinations of unique squares adding up to . The
only solution is 2^2 + 3^2 .
3. Steps for experiment/practical/Code:
Code 1 :
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
static int n, m;
static Map<String, Long> state1 = new HashMap<>(), state2 = new HashMap<>(), tmp;
state1.clear();
state2.clear();
rowConfigs[i] = createRowConfig(i);
clearAttackVector = "";
clearRow = "";
clearRow += "0";
if (isValidRowConfig(rowConfigs[ndx], row)) {
if (isValidRowConfig(rowConfigs[ndx], row)) {
if (compatible(rowConfigs[ndx], state)) {
tot %= mod;
state2.put(vect, tot);
tmp = state1;
state1 = state2;
state2 = tmp;
state2.clear();
long result = 0;
result += state1.get(state);
result %= mod;
return result;
int t = s.nextInt();
n = s.nextInt();
m = s.nextInt();
board[j] = s.next();
System.out.println(calcWays());
s.close();
while(rowConfig.length() < m) {
return rowConfig;
int count = 0;
if (board[row].charAt(i) == '#') {
if (hasQueen(rowConf, i)) {
return false;
count = 0;
continue;
if (hasQueen(rowConf, i)) {
if (++count > 1) {
return false;
return true;
if (!hasQueen(rowConf, i)) {
continue;
if (attackedFromUpperLeft(i, attVect) ||
attackedFromAbove(i, attVect) ||
attackedFromUpperRight(i, attVect)) {
return false;
return true;
return false;
return false;
return false;
if (col <= 0) {
return false;
if (col >= m - 1) {
return false;
if (!isOpenSpace(row, i - 1)) {
vect += "0";
hasQueen(prevRowConf, i - 1)) {
vect += "1";
else {
vect += "0";
if (!isOpenSpace(row, i)) {
vect += "0";
hasQueen(prevRowConf, i)) {
vect += "1";
}
else {
vect += "0";
if (!isOpenSpace(row, i + 1)) {
vect += "0";
hasQueen(prevRowConf, i + 1)) {
vect += "1";
else {
vect += "0";
return vect;
2 . Code -
import java.io.*;
import java.util.*;
System.out.println(countSumPower(num,power,1,0,0))
public static int countSumPower(int num, int power, int curr, int carry, int count){ int
if (sum == num)
return 1;
return 0;
return count;
4. Result/Output/Writing Summary:
Output 1 :
Output 2 :