# 问题描述

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[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;
}


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

2016-09-02 11:43:49

#### 使用DEDE发现个变态问题

2012-08-19 09:13:33

#### 借船过河：一个据说能看穿你的人性和欲望的心理测试

2016-07-01 10:48:06

#### 心理测试------借船过河

2015-03-05 08:51:46

#### python执行“古怪的”js代码问题

2018-03-07 13:54:51

#### 借船过河：一个据说能看穿你的人性和欲望的心理测试

2011-11-25 13:14:28

#### 借船过河：一个据说能看穿你的人性和欲望的心理测试

2016-11-01 15:31:47

#### 变态教程:apache 1.3.39+apache 2.0.52+zeus 4.3+lighttpd+nginx+zend opt+eaccele

2008-06-21 21:55:00

#### NYOJ45：过河问题【贪心】

2015-08-10 15:24:28

#### 野人传教士过河问题 一个实现Demo

2009年01月14日 479KB 下载