回溯法——圆排列问题

199 阅读1分钟
void backtrack(int t)
{
    if (t > N)
    {
        compute();
    }
    else
    {
        for (int j = t; j <= N; ++j)
        {
            swap(r[t], r[j]);
            double centerx = center(t); // 计算当前圆的圆心坐标

            // "centerx + r[t]"代表用当前圆作为最后一个圆,来确定右边界
            // "r[1] -0"代表用第一个圆作为第一个圆,来确定左边界
            // 这种剪枝函数是很粗的,所以在t>n时还要再做一个compute验证
            if (centerx + r[t] + r[1] < minlen)
            {
                x[t] = centerx;
                backtrack(t + 1);
            }
            swap(r[t], r[j]);
        }
    }
}