C# 在随机位置仅更改数组的一部分
本文关键字:数组 一部分 随机 位置 | 更新日期: 2023-09-27 18:16:10
我在控制台 c# 代码(7X3 'cell'(中有一个 int 的 2D 数组。我创建它的那一刻,它被初始化为零。
我必须将只有五个"单元格"的值更改为 1,而不更改其他"单元格"的值。我知道这是一件基本的事情,但是,作为 C# 的绝对菜鸟,无论我使用什么单元格,我都无法让它工作。你可以帮我吗?
提前谢谢。
(注意:由于我是一个频繁的用户/贡献者,我知道这看起来像一个经典的"做我的功课"问题,我很抱歉,但由于我真的被困在这个weenie部分(我的项目的其余部分还可以(,我将不胜感激(。
这里有两种解决此问题的方法,第一种; 调用BruteForceRandomImplementation
很容易实现和理解,但是一旦您尝试用 1 标记大量位置,就会非常慢,因为它是蛮力,例如使用 Random
直到它填满足够的位置。
/// <summary>
/// This method uses a random based algorithm to create a two-dimensional array of [height, width]
/// with exactly locationsToFind locations set to 1.
/// </summary>
/// <param name="height"></param>
/// <param name="width"></param>
/// <param name="locationsToFind"></param>
/// <returns></returns>
public int[,] BruteForceRandomImplementation(int height, int width, int locationsToFind)
{
var random = new Random();
locationsToFind = LimitLocationsToFindToMaxLocations(height, width, locationsToFind);
// Create our two-dimensional array.
var map = new int[height, width];
int locationsFound = 0;
// Randomly set positons to 1 untill we have set locationsToFind locations to 1.
while (locationsFound < locationsToFind)
{
// Get a random Y location - limit the max value to our height - 1.
var randomY = random.Next(height);
// Get a random X location - limit the max value to our width - 1.
var randomX = random.Next(width);
// Find another random location if this location is already set to 1.
if (map[randomY, randomX] == 1)
continue;
// Otherwise set our location to 1 and increment the number of locations we've found.
map[randomY, randomX] = 1;
locationsFound += 1;
}
return map;
}
使用以下辅助方法来保持我们的locationsToFind
范围正常:
/// <summary>
/// Limits the locationsToFind variable to the maximum available locations. This avoids attempting to
/// mark more locations than are available for our width and height.
/// </summary>
/// <param name="height"></param>
/// <param name="width"></param>
/// <param name="locationsToFind"></param>
/// <returns></returns>
public int LimitLocationsToFindToMaxLocations(int height, int width, int locationsToFind)
{
return Math.Min(locationsToFind, height * width);
}
我的第二个实现称为 ShuffleImplementation
在标记大量唯一位置时要快得多。它创建一个一维数组,用足够的 1 填充它以满足您的需求,然后使用 Fisher-Yates shuffle 洗牌这个数组,最后继续将这个一维数组扩展到二维数组:
/// <summary>
/// This method uses a shuffle based algorithm to create a two-dimensional array of [height, width]
/// with exactly locationsToFind locations set to 1.
/// </summary>
/// <param name="height"></param>
/// <param name="width"></param>
/// <param name="locationsToFind"></param>
/// <returns></returns>
public int[,] ShuffleImplementation(int height, int width, int locationsToFind)
{
locationsToFind = LimitLocationsToFindToMaxLocations(height, width, locationsToFind);
// Create a 1 dimensional array large enough to contain all our values.
var array = new int[height * width];
// Set locationToFind locations to 1.
for (int location = 0; location < locationsToFind; location++)
array[location] = 1;
// Shuffle our array.
Shuffle(array);
// Now let's create our two-dimensional array.
var map = new int[height, width];
int index = 0;
// Expand our one-dimensional array into a two-dimensional one.
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
map[y, x] = array[index];
index++;
}
}
return map;
}
/// <summary>
/// Shuffles a one-dimensional array using the Fisher-Yates shuffle.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="arr"></param>
public static void Shuffle<T>(T[] array)
{
var random = new Random();
for (int i = array.Length - 1; i > 0; i--)
{
int n = random.Next(i + 1);
Swap(ref array[i], ref array[n]);
}
}
/// <summary>
/// Swaps two values around.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="valueA"></param>
/// <param name="valueB"></param>
public static void Swap<T>(ref T valueA, ref T valueB)
{
T tempValue = valueA;
valueA = valueB;
valueB = tempValue;
}
用法:
var map = BruteForceRandomImplementation(7, 3, 5);
或:
var map = ShuffleImplementation(7, 3, 5);
取决于您要使用的一个。有关两者之间性能差异的良好示例,请尝试:
int width = 1000;
int height = 1000;
int locationsToFind = (width * height) - 1;
var stopwatch = new Stopwatch();
stopwatch.Start();
BruteForceRandomImplementation(height, width, locationsToFind);
stopwatch.Stop();
Console.WriteLine(string.Format("BruteForceRandomImplementation took {0}ms", stopwatch.ElapsedMilliseconds));
stopwatch.Restart();
ShuffleImplementation(height, width, locationsToFind);
stopwatch.Stop();
Console.WriteLine(string.Format("ShuffleImplementation took {0}ms", stopwatch.ElapsedMilliseconds));
在我的笔记本电脑上,BruteForceRandomImplementation花了1205ms,ShuffleImplementation花了67ms或快了近18倍。
所以你定义了一个多维数组(基本上是一个矩阵(:
int[,] cells = new int[7,3];
然后将所有值初始化为 0。
要更改值,您应该能够像这样操作,例如:
cells[3,2] = 1;
这就是你尝试过的吗?您是否收到任何异常或警告?
如果您使用的是交错数组(数组的数组(,那么它可能如下所示:
int[][] cells = new int[7][3];
然后您可以设置值:
cells[3][2] = 1;
var array = new int[7,3];
array[5,2] = 1;
array[3,2] = 1;
请记住,数组索引从零开始,因此 int[7,3] 的有效范围为 [0..6,0..2]
int[,] cells = new int[7,3];
cells[3,2] = 1;