この投稿はほぼ5年前のものですが、私の答えは誰かに役立つかもしれません。以下の2つの解決策があります。1つは、マルチスレッドなしです。2つ目は、4つのスレッドを使用して問題を解決します。48x4パネルを計算します(48x10は非常に長く続きます)が、初期値を変更することで簡単に変更できます。
nbOfRows、nbOfCols、wallWidth、wall
マルチスレッドなしのソリューション:
import java.io.*;
import java.util.*;
class WallFlexOld
{
static final float blocks[] = {3.0f, 4.5f};
static final int nbOfRows = 4;
static final int nbOfCols = 16;
static final float wallWidth = 48.0f;
static final float wall[][] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
static long nbOfCombinations = 0;
public static void main(String args[])
{
long startTime = System.currentTimeMillis();
addBlock(0, 0);
long workingTime = System.currentTimeMillis() - startTime;
System.out.println("Working time: " + workingTime + "ms");
System.out.println("noc: " + nbOfCombinations);
}
static void addBlock(int row, int col)
{
for(float b: blocks)
{
wall[row][col] = b;
if(blockFit(row, col))
{
if(rowWidth(row) <= wallWidth)
{
if(rowWidth(row) == wallWidth)
{
if(row == (nbOfRows - 1))
nbOfCombinations++;
else
addBlock(row + 1, 0);
}
else //rowWidth < wallWidth
addBlock(row, col + 1);
}
}
wall[row][col] = 0;
}
}
static float rowWidth(int row)
{
float width = 0;
for(float b: wall[row])
width = width + b;
return width;
}
static boolean blockFit(int row, int col)
{
if(row == 0)
return true;
boolean fit = true;
float currentLenght = 0;
for(int i = 0; i < col; i++)
currentLenght = currentLenght + wall[row][i];
float lowerRowCurLenght = 0;
for(float b: wall[row - 1])
{
lowerRowCurLenght = lowerRowCurLenght + b;
if((currentLenght == lowerRowCurLenght) & (currentLenght != wallWidth))
fit = false;
}
return fit;
}
}
マルチスレッドによる解決策:
import java.io.*;
import java.util.*;
class Wall implements Runnable
{
private float blocks[];
private int nbOfRows;
private int nbOfCols;
private float wallWidth;
private float wall[][];
private long nbOfCombinations = 0;
private int row, col;
public long getNbOfCombinations() { return this.nbOfCombinations; }
Wall(float blocks[], int nbOfRows, int nbOfCols, float wallWidth, float wall[][], int row, int col)
{
this.blocks = blocks;
this.nbOfRows = nbOfRows;
this.nbOfCols = nbOfCols;
this.wallWidth = wallWidth;
this.wall = wall;
this.row = row;
this.col = col;
}
private boolean blockFit(int row, int col)
{
if(row == 0)
return true;
boolean fit = true;
float currentLenght = 0;
for(int i = 0; i < col; i++)
currentLenght = currentLenght + wall[row][i];
float lowerRowCurLenght = 0;
for(float b: wall[row - 1])
{
lowerRowCurLenght = lowerRowCurLenght + b;
if((currentLenght == lowerRowCurLenght) & (currentLenght != wallWidth))
fit = false;
}
return fit;
}
private float rowWidth(int row)
{
float width = 0;
for(float b: wall[row])
width = width + b;
return width;
}
private void addBlock(int row, int col)
{
for(float b: blocks)
{
wall[row][col] = b;
if(blockFit(row, col))
{
if(rowWidth(row) <= wallWidth)
{
if(rowWidth(row) == wallWidth)
{
if(row == (nbOfRows - 1))
nbOfCombinations++;
else
addBlock(row + 1, 0);
}
else //rowWidth < wallWidth
{
addBlock(row, col + 1);
}
}
}
wall[row][col] = 0;
}
}
@Override
public void run()
{
addBlock(row, col);
}
}
class WallMT
{
static final float blocks[] = {3.0f, 4.5f};
static final int nbOfRows = 4;
static final int nbOfCols = 16;
static final float wallWidth = 48.0f;
static final float wall[][] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
public static void main(String args[])
{
wall[0][0] = blocks[0];
wall[0][1] = blocks[0];
Wall myWall1 = new Wall(blocks, nbOfRows, nbOfCols, wallWidth, getWallCopy(wall), 0, 2);
Thread t1 = new Thread(myWall1);
wall[0][0] = blocks[0];
wall[0][1] = blocks[1];
Wall myWall2 = new Wall(blocks, nbOfRows, nbOfCols, wallWidth, getWallCopy(wall), 0, 2);
Thread t2 = new Thread(myWall2);
wall[0][0] = blocks[1];
wall[0][1] = blocks[0];
Wall myWall3 = new Wall(blocks, nbOfRows, nbOfCols, wallWidth, getWallCopy(wall), 0, 2);
Thread t3 = new Thread(myWall3);
wall[0][0] = blocks[1];
wall[0][1] = blocks[1];
Wall myWall4 = new Wall(blocks, nbOfRows, nbOfCols, wallWidth, getWallCopy(wall), 0, 2);
Thread t4 = new Thread(myWall4);
long startTime = System.currentTimeMillis();
t1.start();
t2.start();
t3.start();
t4.start();
try
{
t1.join();
t2.join();
t3.join();
t4.join();
}
catch(InterruptedException ie)
{
System.out.println("Thread " + t1 + " interrupted.");
}
long workingTime = System.currentTimeMillis() - startTime;
System.out.println("Working time: " + workingTime + "ms");
System.out.println("noc: " + (myWall1.getNbOfCombinations() + myWall2.getNbOfCombinations() + myWall3.getNbOfCombinations() + myWall4.getNbOfCombinations()));
}
static private float[][] getWallCopy(float wall[][])
{
float tmpWall[][] = new float[nbOfRows][nbOfCols];
for(int i = 0; i < nbOfRows; i++)
for(int j = 0; j < nbOfCols; j++)
tmpWall[i][j] = wall[i][j];
return tmpWall;
}
}