变态暴力家庭过河问题

问题描述

话说有一家六口人,包括爸爸、妈妈、两个女儿及两个儿子在旅行途中迷了路,还不幸遇上了一个逃狱的犯人,幸好犯人让一个也在旅行的警察逮捕,一家六口才得以保住性命。他们只有通过一条河流方能回家,能帮帮他们么?

玩家的任务是,帮助这些人渡过河,交通工具只有一艘小船。只有爸爸、妈妈以及警察能控制小船,此外,不论成人或是小孩,小船每次最多只能搭乘二人。在渡河期间,玩家还要防止以下三种情况发生:

1、当警察与犯人分开时,犯人会伤害一家六口。

2、当爸爸看见妈妈离开女儿时,爸爸便会教训女儿。

3、当妈妈看见爸爸离开儿子时,妈妈便会教训儿子。

答案

1)警察带小偷先过河的对面,然后警察把小偷放下自己回来;
2)警察再带儿子A过河,将儿子A放下,把小偷带回来;
3)爸爸带儿子B过河,将儿子B放下,自己回来;
4)爸爸带妈妈过河,上岸后,妈妈自己回来;
5)警察再带小偷过河,警察和小偷都上岸,爸爸回来;
6)爸爸和妈妈再过河,妈妈自己回来;
7)妈妈带女儿A过河,妈妈和女儿A都上岸,警察带小偷回来;
8)警察带女儿B过河,女儿B上岸后,警察自己回来;
9)警察再带小偷过河就可以了。

代码

下面的代码写了一天,还是无法实现,一直在某几个状态里打转出不去。

求大神指点。

目前的想法是存起来曾经出现过的状态,如果再次出现该状态,就不遍历重复出现的状态,但是这个也还没有实现。目前只能先挂起这个事情了,以后再查资料或者怎么样,解决一下。

// can not finish this program, hang up
#include <iostream>
#include <string>
using namespace std;

// an array to record the status of people
// a[0]    [1] [2] [3]         [4]     [5] [6]  [7]
// police dad mom daughterA daughterB sonA sonB criminal 
// for example 
// left[0] = true : police is on the left side of the river


class Solution
{
public:
    bool left[8];// on the left side of the river
    bool right[8];// on the right side of the river
    bool mid[8];// on the boat
    string map[8];// the mapping array
    void solveone(int pre1, int pre2)
    {
        cout << endl << "one round!" << endl << endl;
        if (finish())
        {
            cout << "\n#### finally finish ####" << endl << endl;
            return;
        }
        for (int i = 0; i < 3; i++)// police or dad or mom get on the boat
        {
            if (left[i] == 0)// not here, choose next one
                continue;
            //left[i] = 0;
            //mid[i] = 1;
            if (true)// at least 2 people get on the boat, otherwise meaningless
            {
                //cout << m[i] << " go to boat" << endl;
                for (int j = 0; j < 8; j++)
                {
                    if (left[j] == 0)// not here, choose next one
                        continue;
                    if ((pre1 == i && pre2 == j) || (pre1 == j && pre2 == i)) // we should avoid that the same person go to right and loop forever.
                        continue;
                    left[i] = left[j] = 0;
                    mid[i] = mid[j] = 1;
                    if (allok())
                    {
                        cout << map[i] << ", " << map[j] << " go to boat and drive to right" << endl;
                        // get off the boat
                        mid[i] = mid[j] = 0;
                        right[i] = right[j] = 1;
                        if (allok())
                        {
                            cout << map[i] << ", " << map[j] << " get off and be on the right" << endl;
                            for (int m = 0; m < 3; m++)// police or dad or mon get on the boat                      
                            {
                                if (right[m] == 0)
                                    continue;
                                //right[m] = 0;
                                //mid[m] = 1;
                                for (int n = 0; n < 8; n++)// choose n to go to left
                                {
                                    if (right[n] == 0 && m != n)// m and n can be the same person, go to left alone. 
                                        continue;
                                    if ((m == i && n == j) || (m == j && n == i))// but we should avoid that two same person go to right and go to left and loop forerver
                                        continue;
                                    right[m] = right[n] = 0;
                                    mid[m] = mid[n] = 1;
                                    if (allok())
                                    {
                                        cout << map[m] << ", " << map[n] << " go to boat and drive to left" << endl;
                                        left[m] = left[n] = 1;
                                        mid[m] = mid[n] = 0;
                                        if (allok())
                                        {
                                            cout <<  map[m] << ", " << map[n] << " get off be on the left" << endl;
                                            solveone(m, n);
                                        }
                                        // backtrace
                                        cout << "# backtrace! should not " << map[m] << ", " << map[n] << " get off be on the left" << endl;
                                        left[m] = left[n] = 0;
                                        mid[m] = mid[n] = 1;
                                    }
                                    // backtrace
                                    cout << "# backtrace! should not " << map[m] <<  ", " << map[n] << " go to boat and drive to left" << endl;
                                    right[m] = right[n] = 1;
                                    mid[m] = mid[n] = 0;
                                }
                                // backtrace
                                //cout << "# backtrace! should not " << map[m] << " go to boat and drive to left" << endl;
                                //right[m] = 1;
                                //mid[m] = 0;
                            }
                        }

                        // backtrace
                        cout << "# backtrace! should not " << map[i] << ", " << map[j] << " get off and be on the right" << endl;
                        mid[i] = mid[j] = 1;
                        right[i] = right[j] = 0;


                    }

                    // backtrace    
                    cout << "# backtrace! should not " << map[i] << ", " << map[j]  << " go to boat and drive to right" << endl;
                    left[i] = left[j] = 1;
                    mid[i] = mid[j] = 0;

                }

            }
            // backtrace
            //cout << "# backtrace! should not " << map[i]  << " go to boat and drive to right" << endl;
            //left[i] = 1;
            //mid[i] = 0;
        }
    }

    Solution()// initialize: all people on the left 
    {
        map[0] = "police";
        map[1] = "dad";
        map[2] = "mom";
        map[3] = "dauA";
        map[4] = "dauB";
        map[5] = "sonA";
        map[6] = "sonB";
        map[7] = "criminal";

        for (int i = 0; i < 8; i++)
        {
            left[i] = 1;
            mid[i] = 0;
            right[i] = 0;
        }
    }
    bool ok(bool arr[])
    {
        if (arr[7] == 1 && arr[0] == 0)// criminal is not with police
        {
            for (int i = 1; i < 7; i++)
            {
                if (arr[i] == 1)// and there is other people. this is not ok.
                    return false;
            }
        }
        if (arr[1] == 1 && arr[2] == 0)// dad is here but mom is not
        {
            if (arr[3] == 1 || arr[4] == 1)// daughter is here, this is not ok.
                return false;
        }
        if (arr[1] == 0 && arr[2] == 1)// mom is here but dad is not
        {
            if (arr[5] == 1 || arr[6] == 1)// son is here, this is not ok.
                return false;
        }
        return true;
    }

    bool allok()
    {
        return (ok(left) && ok(mid) && ok(right));
    }

    bool finish()// all people on the right
    {
        for (int i = 0; i < 8; i++)
            if (right[i] == false)
                return false;
        return true;
    }



};


int main()
{
    Solution sol;
    cout << sol.allok() << endl;
    sol.solveone(-1, -1);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值