简体   繁体   English

实现BINGO游戏的算法

[英]Algorithm to implement the BINGO game

For those who doesn't know a BINGO game, its played as follows 对于那些不了解BINGO游戏的人,其玩法如下

1)You get a BINGO card in which there is a NXN matrix of numbers randomly printed.Numbers are unique.The max number printed can be greater than N^2. 1)您得到一张BINGO卡,其中有一个随机打印的NXN数字矩阵,数字是唯一的,最大打印数字可以大于N ^ 2。 eg if you have 5x5 matrix then the max number can be 75 as well.But the range of numbers is pre-decided say 1 to M. 例如,如果您有5x5矩阵,则最大数也可以为75。但是数字范围是预先确定的,例如1到M。

2)A person speaks out numbers randomly in the range 1 to M. 2)一个人随机地说出1到M之间的数字。

3)If the number is on your card you cross the number. 3)如果号码在您的卡上,您会越过该号码。

4)The process of crossing numbers is repeated.When you have crossed a full row or a full column or the two diagonals,then you get your first bingo The game is still continued as the total BINGOs possible are N+N+2 for N rows,N columns and 2 diagonals. 4)重复交叉数字的过程。当您跨过整行或整列或两个对角线时,您将得到第一个宾果游戏。由于BINGO的总数可能为N + N + 2,因此游戏仍在继续行,N列和2条对角线。

Now I want to create an algorithm for it.The user will input random numbers and the algorithm will hear them and cross its numbers in the matrix(already provided).As soon as it gets BINGO it declares it.What is the best possible approach 现在我要为其创建一个算法,用户将输入随机数,算法将听到它们并在矩阵中交叉其数字(已提供),一旦它获得BINGO便对其进行声明。什么是最好的方法


I tried it as maintaining a 2-D matrix for the card 我尝试将其维护为卡片的二维矩阵

When a number is announced, i search it in O(NxN) time.When I find it ,I make it as 0. 宣布数字时,我会在O(NxN)时间中搜索它。当发现它时,我将其设为0。

After making it as 0, I search whether it the corresponding row & column has now all zeroes.If it was on the diagonal , I also search for the diagonal.It takes O(3N) time. 将其设为0后,我搜索对应的行和列是否现在都为零,如果它在对角线上,则还要搜索对角线,这需要O(3N)时间。

Any better approach? 有更好的方法吗?

You can form a map for each number that would map to a pair (row, column). 您可以为每个映射到一对(行,列)的数字形成一个映射。

if ( myMap[number] exists ) {
  then increment rowCount[ myMap[number].row ];
  then increment columnCount[ myMap[number].column ];
}

if ( rowCount[myMap[number].row] == N ) { bingo! }
if ( columnCount[myMap[number].column] == N ) { bingo! }

myMap.erase( number );

Similarly for diagonals. 对角线也一样。

Use an array to store the numbers on the card and keep it sorted. 使用数组将数字存储在卡上并保持其排序。 Upon number being called, search the number using Binary Search (O(logN) time). 呼叫号码后,使用二进制搜索(O(logN)时间)搜索该号码。 This should be a quick approach. 这应该是一种快速的方法。

Create a class Coordinate that holds x and y position in bingo card. 创建一个在Bingo卡中保持x和y位置的类坐标。 NxN array of booleans initialized to false to keep track of what gets crossed off on bingo card. NxN个布尔数组初始化为false,以跟踪在bingo卡上被舍弃的内容。

N^2 time to iterate through bingo card and add each number to hash table using the number as the key and a new Coordinate as the value. N ^ 2次遍历Bingo卡,并使用数字作为键并使用新的坐标作为值将每个数字添加到哈希表中。

n time to iterate through all the numbers that will be called out, retrieve the Coordinate from the hash table, and update the status of the boolean array. n次遍历将被调出的所有数字,从哈希表中检索坐标,并更新布尔数组的状态。 In case of duplicate numbers called, you must retrieve and update boolean array until the hash table does not contain the key. 如果调用了重复数字,则必须检索并更新布尔数组,直到哈希表不包含键为止。

4N time to check each direction on boolean array for first bingo 4N时间检查布尔数组上每个宾果的每个方向

N^2 + n*4N total runtime N ^ 2 + n * 4N总运行时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM