ZOJ 1110 Dick and Jane

最近回溯写多了,这题其实可以两层循环枚举搞定的,但不自觉的就写成递归式了。。


#include 

int s, p, y, j;
bool dfs(int off[], int index)
{
    if (index == 3)
    {
        int ss = s + off[0];
        int pp = p + off[1];
        int yy = y + off[2];

        if (ss + pp == yy || ss + pp == yy + 1)
        {
            int temp =j + 12 - ss - pp * 2; 
            if (temp % 3 == 0)
            {
                int x3 = temp / 3;
                int x2 = x3 + pp;
                int x1 = x2 + ss;

                printf("%d %d %d\n", x1, x2, x3);
                return true;
            }
        }

        return false;
    }
    else
    {
        bool ret = false;

        if (false == dfs(off, index + 1))
        {
            off[index] = 1;
            ret == dfs(off, index + 1);
            off[index] = 0;
        }
        else
        {
            ret = true;
        }

        return ret;
    }
}

int main()
{

    while (scanf("%d %d %d %d", &s, &p, &y, &j) != EOF)
    {
        int off[3] = {0};

        dfs(off, 0);
    }
    return 0;
}



你可能感兴趣的:(搜索题,ACM简单题)