#1
27.02.2020 um 22:59 UhrZeilen 14 ... 16 sind irrelevant, stammen noch auch der Testphase.

C# :: Aufgabe #265 :: Lösung #2
using static System.Console; using System.Collections.Generic; using System.Text; namespace Zahnstocher_Console { public static class Program { static void Main() { int max_generation = 25; int laenge = 5; List<Zahnstocher> lz = new List<Zahnstocher>(); Zahnstocher z = new Zahnstocher(0, 0, false, laenge); lz.Add(z); int i = max_generation; WriteLine(); WriteLine($" max_generation: {i, 2}"); WriteLine(); ZahnstocherGrid zg = new ZahnstocherGrid(laenge, i); WriteLine(zg.reference_grid.PrintIntArray(2)); for (var j = 0; j < zg.listing.Length; j++) WriteLine($"{j, 3}: {zg.listing[j].Count,2}"); "ready".Info(); ReadKey(); } static void Info(this string s) => WriteLine(s); public static string PrintIntArray(this int[,] a, int space = 4) { StringBuilder sb = new StringBuilder(); int y = a.GetLength(0); int x = a.GetLength(1); for (var iy = 0; iy < y; iy++) { for (var ix = 0; ix < x; ix++) { string s = " "; if(a[iy, ix] != 0) s = $"{a[iy, ix], 2}"; while (s.Length < space) s += " "; sb.Append($"{s}"); } sb.AppendLine(); } return sb.ToString(); } } public class Zahnstocher { public int CenterX { private set; get; } public int CenterY { private set; get; } public int Laenge { private set; get; } public bool Ausrichtung { private set; get; } public Zahnstocher(int centerX, int centerY, bool ausrichtung_horizontal, int laenge) { this.CenterX = centerX; this.CenterY = centerY; this.Laenge = laenge; this.Ausrichtung = ausrichtung_horizontal; } public Zahnstocher() { } public Zahnstocher[] NextTwo(Zahnstocher t) { Zahnstocher[] result = new Zahnstocher[2]; for (var i = 0; i < result.Length; i++) result[i] = new Zahnstocher(); int length = t.Laenge; bool bo_dir = !t.Ausrichtung; int lh = length / 2; result[0].Laenge = length; result[1].Laenge = length; result[0].Ausrichtung = bo_dir; result[1].Ausrichtung = bo_dir; int y = t.CenterY; int x = t.CenterX; if (t.Ausrichtung) { result[0].CenterX = x - lh; result[1].CenterX = x + lh; result[0].CenterY = y; result[1].CenterY = y; } else { result[0].CenterY = y - lh; result[1].CenterY = y + lh; result[0].CenterX = x; result[1].CenterX = x; } return result; } } public class ZahnstocherGrid { public List<Zahnstocher>[] listing; public int Max_generations { private set; get; } public int Toothpick_length { private set; get; } readonly int offset_Y, offset_X; readonly int grid_dimY, grid_dimX; public int[,] reference_grid; public ZahnstocherGrid(int toothpick_length, int max_generations) { this.Max_generations = max_generations + 1; this.Toothpick_length = toothpick_length; this.grid_dimY = 1 + (this.Max_generations / 2) * (this.Toothpick_length / 2) * 2; this.grid_dimX = 1 + ((this.Max_generations - 1) / 2) * (this.Toothpick_length / 2) * 2; this.offset_Y = grid_dimY / 2; this.offset_X = grid_dimX / 2; listing = new List<Zahnstocher>[Max_generations]; for (var i = 0; i < Max_generations; i++) listing[i] = new List<Zahnstocher>(); this.reference_grid = new int[this.grid_dimY, this.grid_dimX]; Run(); } private void Run() { bool ausrichtung_horizontal = false; int centerY = 0, centerX = 0; Zahnstocher z = new Zahnstocher(centerX, centerY, ausrichtung_horizontal, this.Toothpick_length); listing[0].Add(z); for (var i = 1; i < Max_generations; i++) { Adjust_RefMatrix(listing[i - 1]); ausrichtung_horizontal = !ausrichtung_horizontal; for (var j = 0; j < listing[i - 1].Count; j++) { Zahnstocher[] next = z.NextTwo(listing[i - 1][j]); for (var k = 0; k < next.Length; k++) { int ny = next[k].CenterY + offset_Y; int nx = next[k].CenterX + offset_X; if(reference_grid[ny, nx] < 2) listing[i].Add(next[k]); } } } } private void Adjust_RefMatrix(List<Zahnstocher> liste) { for (var i = 0; i < liste.Count; i++) { int y = liste[i].CenterY; int x = liste[i].CenterX; int xx, yy; int lh = Toothpick_length / 2; for (var j = -lh; j <= lh; j++) { if (liste[i].Ausrichtung) { xx = x + j; yy = y; } else { xx = x; yy = y + j; } reference_grid[yy + offset_Y, xx + offset_X] += 1; } } } } }
Kommentare:
hollst
Punkte: 13980
761 Aufgaben
132 Lösungen
117 Kommentare