Java
Currently, my code is very long and tedious, I'm working on making it faster. I use a recursive method in order to find the values. It computes the first 5 within 2 or 3 seconds, but it gets much slower afterwards. Also, I'm not sure yet if the numbers are right, but the first few seem to line up with the comments. Any suggestions are welcome.
Output
2x2: 3
4x4: 30
6x6: 410
8x8: 6148
10x10: 96120
Explanation
The basic idea is recursion. Essentially you start with an empty board, a board with all zeros. The recursive method just checks to see if it can put a black or white pawn in the next position, if it can only put one color, it puts it there and calls itself. If it can put both colors it calls itself twice, one with each color. Each time it calls itself it decreases the squares left and the appropriate color left. When it has filled the entire board it returns the the current count + 1. If it finds out that there is no way to put a black or white pawn in the next position, it returns 0, which means it's a dead path.
Code
public class Chess {
public static void main(String[] args){
System.out.println(solve(1));
System.out.println(solve(2));
System.out.println(solve(3));
System.out.println(solve(4));
System.out.println(solve(5));
}
static int solve(int n){
int m =2*n;
int[][] b = new int[m][m];
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++){
b[i][j]=0;
}
}
return count(m,m*m,m*m/2,m*m/2,0,b);
}
static int count(int n,int sqLeft, int bLeft, int wLeft, int count, int[][] b){
if(sqLeft == 0){
/*for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.print(b[i][j]);
}
System.out.println();
}
System.out.println();*/
return count+1;
}
int x=(sqLeft-1)%n;
int y=(sqLeft-1)/n;
if(wLeft==0){
if(y!=0){
if ((x==0?true:b[x-1][y-1]!=1)&&(x==n-1?true:b[x+1][y-1]!= 1)) {
b[x][y] = 2;
return count(n, sqLeft-1, bLeft-1, wLeft, count, b);
} else {
return 0;
}
} else {
b[x][y]=2;
return count(n,sqLeft-1,bLeft-1,wLeft,count,b);
}
} else if(bLeft==0){
if(y!=n-1){
if((x==0?true:b[x-1][y+1]!=2)&&(x==n-1?true:b[x+1][y+1]!=2)){
b[x][y]=1;
return count(n,sqLeft-1,bLeft,wLeft-1,count,b);
} else {
return 0;
}
} else {
b[x][y]=1;
return count(n,sqLeft-1,bLeft,wLeft-1,count,b);
}
} else{
if(y==0){
if((x==0?true:b[x-1][y+1]!=2)&&(x==n-1?true:b[x+1][y+1]!=2)){
int[][] c=new int[n][n];
for(int i = 0; i < n; i++){
System.arraycopy(b[i], 0, c[i], 0, n);
}
b[x][y]=2;
c[x][y]=1;
return count(n,sqLeft-1,bLeft,wLeft-1,count,c)+count(n,sqLeft-1,bLeft-1,wLeft,count,b);
} else {
b[x][y]=2;
return count(n,sqLeft-1,bLeft-1,wLeft,count,b);
}
}else if(y==n-1){
if((x==0?true:b[x-1][y-1]!=1)&&(x==n-1?true:b[x+1][y-1]!=1)){
int[][] c=new int[n][n];
for(int i = 0; i < n; i++){
System.arraycopy(b[i], 0, c[i], 0, n);
}
b[x][y]=2;
c[x][y]=1;
return count(n,sqLeft-1,bLeft,wLeft-1,count,c)+count(n,sqLeft-1,bLeft-1,wLeft,count,b);
} else {
b[x][y]=1;
return count(n,sqLeft-1,bLeft,wLeft-1,count,b);
}
}else{
if(((x==0?true:b[x-1][y-1]!=1)&&(x==n-1?true:b[x+1][y-1]!=1))&&((x==0?true:b[x-1][y+1]!=2)&&(x==n-1?true:b[x+1][y+1]!=2))){
int[][] c=new int[n][n];
for(int i = 0; i < n; i++){
System.arraycopy(b[i], 0, c[i], 0, n);
}
b[x][y]=2;
c[x][y]=1;
return count(n,sqLeft-1,bLeft,wLeft-1,count,c)+count(n,sqLeft-1,bLeft-1,wLeft,count,b);
} else if ((x==0?true:b[x-1][y-1]!=1)&&(x==n-1?true:b[x+1][y-1]!=1)){
b[x][y]=2;
return count(n,sqLeft-1,bLeft-1,wLeft,count,b);
} else if ((x==0?true:b[x-1][y+1]!=2)&&(x==n-1?true:b[x+1][y+1]!=2)){
b[x][y]=1;
return count(n,sqLeft-1,bLeft,wLeft-1,count,b);
} else {
return 0;
}
}
}
}
}
Try it here (Doesn't run fast enough for Ideone so the last value doesn't print, looks like my solution isn't very good!)
2n^2
pawns, is that(2n)^2
or2(n^2)
? \$\endgroup\$