我在这个问题(https://www.youtube.com/watch?v=kb83NwyYI68)上花费了过多时间,但仍然遇到一些错误
:( lock_pairs skips final pair if it creates cycle
lock_pairs did not correctly lock all non-cyclical pairs
:( lock_pairs skips middle pair if it creates a cycle
lock_pairs did not correctly lock all non-cyclical pairs
感谢有人可以提供有关我创建的lock_pair函数的反馈,并指出逻辑错误。为了避免循环,我假设锁定数组的所有列都不应该为真
// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
//update the locked array with relavant pair one by one
for (int i = 0; i < pair_count; i++)
{
locked[pairs[i].winner][pairs[i].loser] = true;
//initiate an array which will count the number of false in each row of locked
int row_cnt[candidate_count];
for (int m = 0; m < candidate_count; m++)
{
row_cnt[m] = 0;
}
for (int m = 0; m < candidate_count; m++)
{
for (int n = 0; n < candidate_count; n++)
{
if (locked[m][n] == true)
{
row_cnt[m]++;
}
}
}
//check if the last updated in locked array created a cycle
//test for cycle is that all columns of updated have atleast one true (implying at least one arrow is pointing)
int zero_cnt = 0;
for (int k = 0; k < candidate_count; k++)
{
if (row_cnt[k] == 0)
{
zero_cnt++;
}
}
if (zero_cnt < 1)
{
locked[pairs[i].winner][pairs[i].loser] = false;
break;
}
}
return;
}