变态暴力家庭过河问题

原创 2016年08月30日 10:40:24

问题描述

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

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

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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

过河问题

过河问题
  • sankt
  • sankt
  • 2011-02-18 23:05
  • 1321

C语言农夫过河问题

一、        问题需求分析 一个农夫带着一只狼、一只羊和一棵白菜,身处河的南岸。他要把这些东西全部运到北岸。问题是他面前只有一条小船,船小到只能容下他和一件物品,另外只有农夫能撑船。另外,因为...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

警察犯人一家六口过河问题

游戏介绍:一家六口,爸爸、妈妈、两个儿子、两个女儿,在游玩时迷了路。路上遇到一 个越狱的犯人,幸好被一个警察捉住。他们不能和外界联络,有一条小船,船上只能坐两 人,会驾驶船的只有警...

农夫过河问题——c语言代码

//2011.11.13 //pan feng yun  //这个程序实现了功能! #include #include #define STEP 20 using namespace std; c...

变态暴力家庭过河问题

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

家庭暴力会遗传

郑小文,男,21岁,祖籍福建,现在美国留学。因与亲友关系相处障碍而向心理师寻求帮助,以下是我们在QQ上的对话节录——   游某:你是在什么时候感觉自己与同性伙伴相处困难的? 小文(想了想):大概...

Crossing River(贪心+模拟)

Crossing River Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Sub...

过河问题

描述 在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边。如果不借助手电筒的话,大家是无论如何也不敢过桥去的。不幸的是,N个人一共只带了一只手电筒,而桥窄得只够让两个人同时过。如果各自单独...

家庭问题

社会网络
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)