Question:
N teams, numbered 0, 1, 2 .... N-1, known as the Strength Comparison between them, is stored in a two-dimensional array W [N] [N]. The value of W [I] [J] indicates I, j's team is a stronger one, so W [I] [J] = I or J. Now they are given the order of appearance and stored in the array order [N.
For example, if order [N] = {4, 3, 5, 8, 1...}, then the first round is 4 to 3, 5 to 8 ......
Winners are promoted, and losers are eliminated. The rankings of all teams eliminated in the same round are no longer subdivided,
In the next round, the winners in the previous round are compared by two in sequence, for example, four to five, until the first place appears.
Programming implementation: Two-dimensional array W, one-dimensional array order and the array result used to output the competition ranking [N] are given to obtain the result
Ideas:
The question clearly shows that you do not need to find every ranking, and the competition method has been clearly given.AlgorithmThe design is clear. Through simple examples, we can see that there is one of the most rounds in the competition process: Log2 (n)+ 1 round. It is a bit like the number of layers of a binary tree. The algorithm uses an auxiliary array to mark the next round of list.CodeBasically, the boundary conditions are not carefully tested based on this idea.
Uint32 W [N] [N]; // known condition uint32 order [N]; // known condition uint32 result [N]; // the I-th element corresponds to the I ranking (should be the elimination batch) uint32 GETID (uint32 list [N], uint32 startpos = 0) {// find the first position not 0, int I = startpos; For (; I <n; I ++) {If (list [I]! = 0) {return I ;}} return N;} uint32 * bisai (uint32 W [N] [N], uint32 order [N]) {uint32 result [N]; memset (result, n/2, sizeof (uint32) * n); uint32 nextid [N]; // progress list memset (nextid, 1, sizeof (uint32) * n ); uint32 I, j; while (1) {// start of the game for (I = GETID (nextid), j = GETID (nextid, I ); I <n & J <n; I = GETID (nextid, J), j = GETID (nextid, I )) {if (I = W [I] [J]) // I promotion, J obsolete {nextid [J] = 0; // J obsolete -- result [I];} If (j = W [I] [J]) {nextid [I] = 0; // I obsolete -- result [J] ;}} if (I = J) {// I is the first break;} return result ;}