Just testing..

Associate
Joined
15 Mar 2006
Posts
2,097
using System;
using System.IO;
using System.Linq;

namespace Solver
{
class MainClass
{
public static int cycle, gridWidth, gridHeight;
public static int noOfShapes, maxShapeWidth, maxShapeHeight;
public static int blocksNeededToSolve, noOfShapesAdded;
public static string gridString;
public static string[] shapeStringArray;
public static int[] shapeWidthArray, shapeHeightArray;
public static int[] shapeHorFitArray, shapeVerFitArray, shapeTotalFitArray;
public static int[] shapeCurrentFitArray; // range is from 1 to totalFit
public static int[] maxBlockCoverage;
public static int[,] gridArray;
public static int[,,] shapeArray;

public static void Main(string[] args)
{
InitialiseVariables();
GetDataFromUser();
//DisplayGridData();
//DisplayShapeData();
CalculateBlocksNeededToSolve();
//AddShape(0, 0);
//RemoveShape(0, 0);

// add next shape at shapeCurrentFitArray position
for (int i = 0; i < noOfShapes; i++)
{
for (int j = 0; j < noOfShapes; j++)
{
Console.Write(shapeCurrentFitArray[j] + ",");
}
Console.WriteLine();
AddShape(noOfShapesAdded, shapeCurrentFitArray); // add shape i, increments noOfShapesAdded
CalculateBlocksNeededToSolve(); // calculates blocksNeededToSolve after shape i is added
if (SolvableInCurrentState(noOfShapesAdded))
{
// if all shapes have been added
if (noOfShapesAdded == noOfShapes)
{
if (CheckIfSolved())
{
Console.WriteLine("Solution found!");
Console.WriteLine();
for (int n = 0; n < noOfShapes; n++)
{
Console.WriteLine("Shape " + (n + 1) + ": (" + (shapeCurrentFitArray[n] % shapeHorFitArray[n]) + "," + (shapeCurrentFitArray[n] / shapeHorFitArray[n]) + ")");
for (int y = 0; y < shapeHeightArray[n]; y++)
{
for (int x = 0; x < shapeWidthArray[n]; x++)
{
Console.Write(shapeArray[n, x, y]);
}
Console.WriteLine();
}
Console.WriteLine();
}
}
else
{
RemoveShape((noOfShapesAdded - 1), shapeCurrentFitArray);
IncrementCurrentFit(i);
i = i - 1;
}
}
}
else
{
RemoveShape((noOfShapesAdded - 1), shapeCurrentFitArray); // remove shape i, decrements noOfShapesAdded
IncrementCurrentFit(i);
i = i - 1;
}
}
}

public static bool CheckIfSolved()
{
CalculateBlocksNeededToSolve();
if (blocksNeededToSolve == 0)
{
return true;
}
else
{
return false;
}
}

public static void IncrementCurrentFit(int shapeToBeIncrementedArg)
{
// if incrementing wont take value out of bounds
if (shapeTotalFitArray[shapeToBeIncrementedArg] > (shapeCurrentFitArray[shapeToBeIncrementedArg] + 1))
{
shapeCurrentFitArray[shapeToBeIncrementedArg] = shapeCurrentFitArray[shapeToBeIncrementedArg] + 1;
}
// if incrementing will take value out of bounds
else
{
if (shapeToBeIncrementedArg == 0) // if shape to be incremented is the first shape, but will go out of bounds
{
// solution not found
Console.WriteLine("Solution not found");
Environment.Exit(0);
}
// reset to 0, increment preceding shape's fit position
shapeCurrentFitArray[shapeToBeIncrementedArg] = 0;
IncrementCurrentFit(shapeToBeIncrementedArg - 1);
}
}

public static bool SolvableInCurrentState(int noOfShapesAddedArg)
{
if (noOfShapesAddedArg == noOfShapes)
{
return true;
}
// if unsolvable in current state due to not enough blocks remaining
if (blocksNeededToSolve > maxBlockCoverage[noOfShapesAddedArg])
{
return false;
}
else
{
return true;
}
}

public static void AddShape(int shapeNumberArg, int shapeFitPositionArg)
{
Console.WriteLine("Addshape:" + shapeNumberArg + "," + shapeFitPositionArg);
int shapeHorFit = shapeHorFitArray[shapeNumberArg];
int shapeVerFit = shapeVerFitArray[shapeNumberArg];
int xOffset = shapeFitPositionArg % shapeHorFit;
int yOffset = shapeFitPositionArg / shapeHorFit;
for (int y = 0; y < shapeHeightArray[shapeNumberArg]; y++)
{
for (int x = 0; x < shapeWidthArray[shapeNumberArg]; x++)
{
if (shapeArray[shapeNumberArg, x, y] != 0)
{
// if grid value becomes cycle after adding
if (gridArray[x + xOffset, y + yOffset] == (cycle - 1))
{
gridArray[x + xOffset, y + yOffset] = 0;
}
else
{
gridArray[x + xOffset, y + yOffset] = gridArray[x + xOffset, y + yOffset] + 1;
}
}
}
}
noOfShapesAdded = noOfShapesAdded + 1;
}

public static void RemoveShape(int shapeNumberArg, int shapeFitPositionArg)
{
int shapeHorFit = shapeHorFitArray[shapeNumberArg];
int shapeVerFit = shapeVerFitArray[shapeNumberArg];
int xOffset = shapeFitPositionArg % shapeHorFit;
int yOffset = shapeFitPositionArg / shapeHorFit;
for (int y = 0; y < shapeHeightArray[shapeNumberArg]; y++)
{
for (int x = 0; x < shapeWidthArray[shapeNumberArg]; x++)
{
if (shapeArray[shapeNumberArg, x, y] != 0)
{
// if grid value becomes -1 after subtracting
if (gridArray[x + xOffset, y + yOffset] == 0)
{
gridArray[x + xOffset, y + yOffset] = cycle - 1;
}
else
{
gridArray[x + xOffset, y + yOffset] = gridArray[x + xOffset, y + yOffset] - 1;
}
}
}
}
noOfShapesAdded = noOfShapesAdded - 1;
}

public static void CalculateBlocksNeededToSolve()
{
blocksNeededToSolve = 0;
for (int i = 0; i < gridHeight; i++)
{
for (int j = 0; j < gridWidth; j++)
{
if (gridArray[j, i] != 0)
{
blocksNeededToSolve = blocksNeededToSolve + (cycle - gridArray[j, i]);
}
}
}
}

public static void DisplayShapeData()
{
// display shapes
Console.WriteLine();
for (int n = 0; n < noOfShapes; n++)
{
Console.WriteLine("Shape " + (n + 1) + ":");
for (int i = 0; i < shapeHeightArray[n]; i++)
{
for (int j = 0; j < shapeWidthArray[n]; j++)
{
Console.Write(shapeArray[n, j, i]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Shape width = " + shapeWidthArray[n]);
Console.WriteLine("Shape height = " + shapeHeightArray[n]);
Console.WriteLine("This shape fits into the grid horizontally " + shapeHorFitArray[n] + " times");
Console.WriteLine("This shape fits into the grid vertically " + shapeVerFitArray[n] + " times");
Console.WriteLine("This shape fits into the grid " + shapeTotalFitArray[n] + " times");
Console.WriteLine();
}
}

public static void DisplayGridData()
{
// display grid data
Console.WriteLine();
Console.WriteLine("Grid:");
for (int i = 0; i < gridHeight; i++)
{
for (int j = 0; j < gridWidth; j++)
{
Console.Write(gridArray[j, i]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Grid width = " + gridWidth);
Console.WriteLine("Grid height = " + gridHeight);
Console.WriteLine();
}

public static void GetDataFromUser()
{
// get pattern cycle, e.g. 0 > 1 > 2 > 0
Console.WriteLine("Enter cycle");
//cycle = Convert.ToInt32(Console.ReadLine());
cycle = 2;

// get grid pattern
Console.WriteLine("Enter grid pattern");
//gridString = Console.ReadLine();
gridString = "101.100.001";
// calculate grid dimensions
gridWidth = gridString.Length - gridString.LastIndexOf('.') - 1;
gridHeight = (gridString.Length + 1) / (gridWidth + 1);
// use dimensions to initialise grid array
gridArray = new int[gridWidth, gridHeight];
// remove periods from gridString for processing
gridString = gridString.Replace(".", "");
// enter grid into gridArray
for (int j = (gridHeight - 1); j > -1; j--)
{
for (int i = (gridWidth - 1); i > -1; i--)
{
gridArray[i, j] = Convert.ToInt32(gridString.Substring(gridString.Length - 1));
gridString = gridString.Remove(gridString.Length - 1);
}
}

// get number of shapes
Console.WriteLine("Enter number of shapes");
noOfShapes = Convert.ToInt32(Console.ReadLine());
// use number of shapes to initialise arrays
shapeStringArray = new string[noOfShapes];
shapeWidthArray = new int[noOfShapes];
shapeHeightArray = new int[noOfShapes];
shapeHorFitArray = new int[noOfShapes];
shapeVerFitArray = new int[noOfShapes];
shapeTotalFitArray = new int[noOfShapes];
maxBlockCoverage = new int[noOfShapes];
shapeCurrentFitArray = new int[noOfShapes];
// get shape patterns
for (int i = 0; i < noOfShapes; i++)
{
// initialise arrays to 0
shapeWidthArray = 0;
shapeHeightArray = 0;
shapeHorFitArray = 0;
shapeVerFitArray = 0;
shapeTotalFitArray = 0;
maxBlockCoverage = 0;
shapeCurrentFitArray = 0;
// get shape date
Console.WriteLine("Enter pattern of shape " + (i + 1));
shapeStringArray = Console.ReadLine();
// calculate shape dimensions
shapeWidthArray = shapeStringArray.Length - shapeStringArray.LastIndexOf('.') - 1;
shapeHeightArray = (shapeStringArray.Length + 1) / (shapeWidthArray + 1);
// calculate how many positions each shape can fit into grid
shapeHorFitArray = gridWidth - shapeWidthArray + 1;
shapeVerFitArray = gridHeight - shapeHeightArray + 1;
shapeTotalFitArray = shapeHorFitArray * shapeVerFitArray;
// calculate max width and heights of shapes to be used for initialising shapeArray
if (shapeWidthArray > maxShapeWidth)
{
maxShapeWidth = shapeWidthArray;
}
if (shapeHeightArray > maxShapeHeight)
{
maxShapeHeight = shapeHeightArray;
}
// calculate max shape coverage
for (int j = i; j > -1; j--)
{
maxBlockCoverage[j] = maxBlockCoverage[j] + shapeStringArray.Count(a => a == '1');
}
}
// initialise shapeArray
shapeArray = new int[noOfShapes, maxShapeWidth, maxShapeHeight];
// enter shapes into shapeArray
for (int i = 0; i < noOfShapes; i++)
{
// remove periods from gridString for processing
shapeStringArray = shapeStringArray.Replace(".", "");
// enter shapes into shapeArray
for (int j = (shapeHeightArray - 1); j > -1; j--)
{
for (int k = (shapeWidthArray - 1); k > -1; k--)
{
shapeArray[i, k, j] = Convert.ToInt32(shapeStringArray.Substring(shapeStringArray.Length - 1));
shapeStringArray = shapeStringArray.Remove(shapeStringArray.Length - 1);
}
}
}
}

public static void InitialiseVariables()
{
maxShapeWidth = 0;
maxShapeHeight = 0;
blocksNeededToSolve = 0;
noOfShapesAdded = 0;
}
}
}
 
Last edited:
Back
Top Bottom