经典指数          
原因
2279
浏览数
0
收藏数
 

和尚挑水,七个和尚,一周七天。每个人的安排挑水时间不同,输出所有的跳水可能排列组合。请编程实现。请编程实现。 输入:0 表示该和尚当天不可挑水, 1 表示该和尚当天可挑水

     举报   纠错  
 
切换
1 个答案

int stack[7];

std::vector stack_record;

std::set mon;

int count = 0;

void findNext(int record[7][7], int depth)

{

    if (depth == 7)

    {

        count++;

        int *numPtr = new int[7];

        for (int i = 0; i < 7; ++i)

        {

            numPtr[i] = stack[i] + 1;

        }

        stack_record.push_back(numPtr);

    }

    else

    {

        for (int i = 0; i < 7; i++)

        {

            if (record[i][depth] == 1 && mon.count(i) == 0)

            {

                stack[depth] = i;

                mon.insert(i);

                findNext(record, depth + 1);

                mon.erase(i);

            }

        }

    }

}

void main()

{

    int record[7][7];

    for (int i = 0; i < 7; ++i)

    {

        for (int j = 0; j < 7; ++j)

        {

            std::cin >> record[i][j];

        }

    }

    findNext(record, 0);

    std::cout << stack_record.size() << std::endl;

    for (int i = 0; i < stack_record.size(); ++i)

    {

        int *tmp = stack_record[i];

        for (int j = 0; j < 7; ++j)

        {

            std::cout << tmp[j] << " ";

        }

        std::cout << std::endl;

    }

    system("pause");

}

 
切换
撰写答案