变态暴力家庭过河问题

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

FZU--2188--过河(bfs暴力条件判断)

过河I Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u ...

用prolog解决农夫过河问题

  • 2012年03月03日 11:07
  • 758B
  • 下载

从那道并不变态的家庭作业说起

作者:李子李子短信 在微博上看到一道被称为"变态家庭作业"的数学题,对象是小学三年级的孩子。微博是这样的:朋友说,儿子有道题不会,问我:一个四位数ABCD×9=DCBA,ABCD=?这是我第一次遭遇...

农夫过河问题

  • 2014年01月02日 11:05
  • 47KB
  • 下载

传教士野人过河问题java源代码

  • 2015年05月07日 00:41
  • 9KB
  • 下载

算法系列之十四:狼、羊、菜和农夫过河问题

题目描述:农夫需要把狼、羊、菜和自己运到河对岸去,只有农夫能够划船,而且船比较小,除农夫之外每次只能运一种东西,还有一个棘手问题,就是如果没有农夫看着,羊会偷吃菜,狼会吃羊。请考虑一种方法,让农夫能够...
  • orbit
  • orbit
  • 2012年05月13日 22:56
  • 64742

农夫过河问题

  • 2014年06月10日 15:08
  • 4KB
  • 下载

Java实现野人过河问题含界面

  • 2015年06月21日 12:06
  • 18KB
  • 下载

图结构解决农夫过河问题(二附源代码)

【什么是图结构】:图结构就是所有数据元素都可以连接,跟树结构有很大区别。 图结构包括两大部分: 1、顶点:所有的数据元素,每个元素就是一个顶点(数字1、2、3、4、5、6)。 2、边:连接两个元...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:变态暴力家庭过河问题
举报原因:
原因补充:

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