Ich arbeite an einem Projekt, bei dem ich versuche, Land und Ozeane zu simulieren. Ich benutze einen Wert über einer bestimmten Zahl, um Land darzustellen, und darunter Wasser. Ich habe ein Programm, das realistische Inseln generieren soll, aber ich habe Probleme damit, sie in ein Array zu packen.public struct Coords {
public int x { get; set; } // Equivalent to ROW
public int y { get; set; } // Equivalent to COL
public Coords(int x, int y) {
this.x = x;
this.y = y; }
public override string ToString() => $"({x}, {y})";
}
< /code>
Um die Stellen der Zellen in einem Array darzustellen, in dem sich Land befindet. Eine Liste von Koordnungen entspricht einer Insel. Ich habe auch ein Programm, das eine Liste von Koordnungen erhält, wenn sie in ein neues Positons übersetzt wird. < /P>
public static List TranslateCoords(int rows, int cols, List coords, Coords newStart)
{
// Test that it works
if (coords == null || coords.Count == 0)
{
return new List();
}
// Get starting point (minimum x over minimum y)
Coords start = coords.OrderBy(c => c.x).ThenBy(c => c.y).First();
// Get the offset
int dx = newStart.x - start.x;
int dy = newStart.y - start.y;
// Translate
List translated = new List();
foreach (var coordy in coords)
{
int newX = coordy.x + dx;
int newY = coordy.y + dy;
// Check if out of bounds
if (newX < 0 || newX >= rows || newY < 0 || newY >= cols)
{
return new List();
}
translated.Add(new Coords(newX, newY));
}
return translated;
}
< /code>
Das Problem, das ich zu lösen versuche, ist, dass ich eine Gruppe von Koordner in ein Array einpacken und eine große Liste aller möglichen Startkoordnungen erhalten möchte, mit denen ich sie dann in ein 2D -Array in ein 2D -Array übersetzen kann. Dies funktionierte in kleinem Maßstab, aber es dauert lange und es war nicht nützlich für Listen von Koordni. < /P>
public static List FindValidIslandPlacementsInArray(int[,] inputArray, List sectionList, int rangeMin, int rangeMax, int distance, bool horizontalWrapping, bool verticalWrapping)
{
int rows = inputArray.GetLength(0);
int cols = inputArray.GetLength(1);
var validStarts = new List();
if (sectionList == null || sectionList.Count == 0)
{
return validStarts;
}
// Create a list of cells not to be checked
List cellsExcluded = new List();
List cellsWithinRangeLoL = Utility.Matrices.Selection.SelectSections(inputArray, rangeMin, rangeMax, horizontalWrapping, verticalWrapping);
List cellsWithinRangeDistLoL = Utility.Matrices.Selection.SelectionSectionEdges(inputArray, rangeMin, rangeMax, horizontalWrapping, verticalWrapping, distance);
cellsExcluded.AddRange(Utility.Lists.CollapseLists(cellsWithinRangeLoL));
cellsExcluded.AddRange(Utility.Lists.CollapseLists(cellsWithinRangeDistLoL));
// Iterate through the list, and test each translateList to see if any overlap
for (int i = 0; i < inputArray.GetLength(0); i++)
{
for (int j = 0; j < inputArray.GetLength(1); j++)
{
Coords newStart = new Coords(i, j);
// Iterates through all tiles not directly within a forbidden tile
if (!cellsExcluded.Contains(newStart))
{
List translateList = Utility.Matrices.Transformation.TranslatePosition(sectionList, rows, cols, newStart, horizontalWrapping, verticalWrapping);
bool overlaps = false;
// Tests all cells with a translated list for overlapping
foreach (Coords cellcoord in translateList)
{
if (inputArray[cellcoord.x, cellcoord.y] >= rangeMin && inputArray[cellcoord.x, cellcoord.y]
(Ignorieren Sie das Wickeln und Abstand). Anschließend begann ich mit einer anderen Version, die nur eine Liste von Koordeln erhalten und die Zeile hinuntergehen kann, jeden mit einem Array verglichen und dann eine Liste von Koordni -Arrays zurückgibt, wobei der Index des einzelnen Arrays einem gültigen Startort für eine Liste von Koordeln mit demselben Index in der angegebenen Liste entspricht. Ich habe eine Optionseinstellung beigefügt, bei der Minval über 0 festgelegt wurde. Sobald das Programm die Menge an Startorten erreicht hat, wurde es beendet (um Zeit zu sparen). Andernfalls wurde jeder Ort gefunden.// This algorithm takes a LoL of Coords, representing a section, and returns all possible starting locations
public static List FindValidPlacements(int rows, int cols, List sections, int minimumReturns = -1)
{
List returnableLists = new List();
#region Verify the lists
// Verify the lists are not null
if (sections == null || sections.Count == 0)
{
return returnableLists;
}
// Check to make sure that the total size is not larger than the list capacity
int limit = rows * cols;
int coordcount_verify = 0;
foreach (List list in sections)
{
foreach (Coords coords in list)
{
coordcount_verify++;
}
}
if (coordcount_verify >= limit)
{
return returnableLists;
}
#endregion
// For each section, iterate through every possible permutation (unless we already have a minimum number of returns
int currentSectionIndex = 0;
bool maxReturnsReached = false;
while (currentSectionIndex < sections.Count)
{
// Get the current comparison list
List primaryList = sections[currentSectionIndex];
// Create an int array for testing
int[,] testForPacking = new int[rows, cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
// Check each cell
}
}
// Iterate current section index
currentSectionIndex ++;
}
return returnableLists;
}
< /code>
Das Problem ist, dass ich nicht herausfinden kann, wie ich nach Listen überprüfen kann. Gibt es Packalgorithmen, die dies erreichen können?
Packalgorithmus für eine Liste von Zellen in einem 2D -Array [geschlossen] ⇐ C#
-
- Similar Topics
- Replies
- Views
- Last post
Mobile version