顯示具有 C++ 標籤的文章。 顯示所有文章
顯示具有 C++ 標籤的文章。 顯示所有文章

2012年6月19日 星期二

狼、羊、菜威力加強版問題 C++ 解題

題目:

Lulu has to transfer the cabbage, the goat, the wolf, the stick and the fire from one bank of the river to the other bank.
But there are only two seats available on his boat !

Furthermore, if the goat and the cabbage stay together on a bank as Lulu is leaving on his boat, the goat will eat the cabbage.
If the wolf and the goat stay together as Lulu is leaving, the wolf will eat the goat !
If the stick and the wolf stay together as Lulu is leaving, the stick will beat the wolf !
If the fire and the stick stay together as Lulu is leaving, the fire will burn the stick !

Click on the "items" to place them on the boat or on the bank. Click on the arrow to make the boat cross the river.

There are 2 levels in this game.

Good luck !


Play
http://jeux.lulu.pagesperso-orange.fr/html/anglais/loupChe/loupChe2.htm






#include <iostream>
#include <vector>
using namespace std;

const int TARGET_STATE = 63;  // 111 111b

vector<int> crossSnapshot;
vector<int> takerSnapshot;

void showPauseMsg();

enum{
  FARMER=32, // 100000 b
  WOLF=16, // 010000 b
  GOAT = 8, // 001000 b
  CABBAGE=4, // 000100 b
  FIRE = 2,  // 000010 b
  STICK = 1  // 000001 b
};


//toggle the bit N array
//at bankA = 0, at bankB = 1

int cross[] = {
 FARMER,
 FARMER | WOLF,
 FARMER | GOAT,
 FARMER | CABBAGE,
 FARMER | FIRE,
 FARMER | STICK,

 FARMER | WOLF | GOAT,
 FARMER | WOLF | CABBAGE,
 FARMER | WOLF | FIRE,
 FARMER | WOLF | STICK,

 FARMER | GOAT | CABBAGE,
 FARMER | GOAT | FIRE,
 FARMER | GOAT | STICK,

 FARMER | CABBAGE | FIRE,
 FARMER | CABBAGE | STICK,

 FARMER | FIRE | STICK
};

void printState(int iState){
 //print out Bit N, N start from 0

 int N=6;
 for(int i=N-1;i>=0;i--)
 {
  cout << ((iState >> i ) & 1);//show Bit 3
 }
 cout << endl;
}

int farmer(int iState){
 //get a Bit N of iState, N start from 0
 return ((iState >> 5 ) & 1);
}
int wolf(int iState){
 return ((iState >> 4 ) & 1);
}
int goat(int iState){
 return ((iState >> 3 ) & 1);
}
int cabbage(int iState){
 return ((iState >> 2 ) & 1);
}

int fire(int iState){
 return ((iState >> 1 ) & 1);
}

int stick(int iState){
 return ((iState >> 0 ) & 1);
}

bool isSafe(int state){
 if( goat(state) == wolf(state) &&
  goat(state) != farmer(state))
 {
  return false;
 }

 if( goat(state) == cabbage(state) &&
  goat(state)!=farmer(state))
 {
  return false;
 }

 if( stick(state) == wolf(state) &&
  stick(state) != farmer(state))
 {
  return false;
 }

 if( fire(state) == stick(state) &&
  fire(state) != farmer(state))
 {
  return false;
 }

 return true;
}

bool isTry(int state){
 bool isTried = false;

 for(int j=0; j < crossSnapshot.size();j++){
  //printState(crossSnapshot[j]);

  if (state == crossSnapshot[j]){
   isTried = true;
   break;
  }
 }
 return isTried;
}

void printTaker(int i){
 char* msg[] = {
  "farmer goes alone",
  "farmer takes wolf",
  "farmer takes goat",
  "farmer takes cabbage",
  "farmer takes fire",
  "farmer takes stick",

  "farmer takes wolf & goat",
  "farmer takes wolf & cabbage",
  "farmer takes wolf & fire",
  "farmer takes wolf & stick",

  "farmer takes goat & cabbage",
  "farmer takes goat & fire",
  "farmer takes goat & stick",

  "farmer takes cabbage & fire",
  "farmer takes cabbage & stick",

  "farmer takes fire & stick"
 };

 if(i >= 0 && i <= 15) {
  cout << msg[i] << endl;;
 }
}

void printEveryState(vector<int> &crossSnapshot){
 cout << "total number cross state = " << crossSnapshot.size()<<endl;

 for(int j=0; j < crossSnapshot.size();j++){
  printState(crossSnapshot[j]);
 }
}

void printEveryState2(vector<int> &takerSnapshot){
 cout << "The cross step is show as belowing." << endl;

 for(int j=0; j < takerSnapshot.size();j++){
  cout << j+1 << ". ";
  printTaker(takerSnapshot[j]);
 }

 cout <<endl;
}

void recordTriedState(int state, int i, vector<int> &crossSnapshot){
 //save snapshot
 crossSnapshot.push_back(state);
 takerSnapshot.push_back(i);
}

void RollbackRecordState(int state, vector<int> &crossSnapshot){
 //delete previous snapshot
 crossSnapshot.pop_back();
 takerSnapshot.pop_back();
}

void crossRiver(int state, vector<int> &crossSnapshot){
 if (state == TARGET_STATE){
  //printEveryState(crossSnapshot);
  printEveryState2(takerSnapshot);
  //stop recurive iteration

  showPauseMsg();
  exit(0);
  return;
 }

 //for every cross choice
 for(int i=0; i< 15; i++){

  int boat = (crossSnapshot.size()+1) % 2; //0 or 1, 0 at banka, 1 at bankb
  int mover = cross[i];

  if(boat==0)
  {
   if ((state & mover)!= 0){ continue; }
  }
  else
  {
   if ((state & mover)!= mover){ continue; }
  }

  int nextCrossState = state ^ cross[i];

  if(isSafe(nextCrossState) && !isTry(nextCrossState)){
   recordTriedState(nextCrossState, i, crossSnapshot);

   //printTaker(i, state);
   //recurive to call itself
   crossRiver(nextCrossState, crossSnapshot);

   RollbackRecordState(nextCrossState, crossSnapshot);
  }
 }
}

void showPauseMsg(){
    cout << "Press any key to be continue" << endl;
 getchar();
}

int main() {
 int initialState = 0;

 //save first snapshot
 crossSnapshot.push_back(initialState);

 crossRiver(initialState, crossSnapshot);
 showPauseMsg();
 return 0;
}
 
 
Output:

The cross step is show as belowing.
1. farmer takes goat & stick
2. farmer goes alone
3. farmer takes wolf
4. farmer takes wolf & goat
5. farmer takes wolf & cabbage
6. farmer takes wolf
7. farmer takes goat
8. farmer takes goat & stick
9. farmer takes wolf & fire
10. farmer goes alone
11. farmer takes goat & stick

Press any key to be continue

狼、羊、菜問題 C++ 解題

古老的智力遊戲題其實最適合用 functional language 或 logic language 或 AI language (Prolog)去解,但這解題的程式碼是我多年前寫下的,懶得修改,只貼出來留念,有興趣的人可以用更適合 language去解題,程式應該是少很多,而且解題思路會更清晰。

問題:

一個農夫帶著一只狼,一只羊和一籃菜回家,途中要過一條河,唯一的渡河工具是一條小船,因為船真的是太小了,農夫最多只能帶三樣東西的其中一件划船過河;可是,要是農夫不在旁的話,狼會咬羊,羊會吃菜,農夫應怎樣過河才可使得羊和菜都無損呢?



 

#include <iostream>
#include <vector>
using namespace std;

const int TARGET_STATE = 15;  // 1111b

vector<int> crossSnapshot;
vector<int> takerSnapshot;

enum{
  FARMER=8, // 1000 b
  WOLF=4, // 0100 b
  GOAT = 2, // 0010 b
  CABBAGE=1 // 0001 b
};


//toggle the bit N array
//at bankA = 0, at bankB = 1

int cross[] = {
 FARMER, //FARMER  //1000b, 8, Farmer at bankB
 FARMER | WOLF, //FARMER, WOLF //1100b, 12
 FARMER | GOAT, //FARMER, GOAT //1010b, 10
 FARMER | CABBAGE //FARMER,CABBAGE //1001b, 9
};

void printState(int iState){
 //print out Bit N, N start from 0

 int N=4;
 for(int i=N-1;i>=0;i--)
 {
  cout << ((iState >> i ) & 1);//show Bit 3
 }
 cout << endl;
}


int farmer(int iState){
 //get a Bit N of iState, N start from 0
 return ((iState >> 3 ) & 1);
}
int wolf(int iState){
 return ((iState >> 2 ) & 1);
}
int goat(int iState){
 return ((iState >> 1 ) & 1);
}
int cabbage(int iState){
 return ((iState >> 0 ) & 1);
}

bool isSafe(int state){
 if( goat(state) == wolf(state) &&
  goat(state) != farmer(state))
 {
  return false;
 }

 if( goat(state) == cabbage(state) &&
  goat(state)!=farmer(state))
 {
  return false;
 }

 return true;
}

bool isTry(int state){
 bool isTried = false;

 for(int j=0; j < crossSnapshot.size();j++){
  //printState(crossSnapshot[j]);

  if (state == crossSnapshot[j]){
   isTried = true;
   break;
  }
 }
 return isTried;
}


void printTaker2(int i){
 char* msg[] = {
  "farmer goes alone",
  "farmer takes wolf",
  "farmer takes goat",
  "farmer takes cabbage"
 };

 if(i >= 0 && i <= 3) {
  cout << msg[i] << endl;;
 }
}

void printEveryState(vector<int> &crossSnapshot){
 cout << "total number cross state = " << crossSnapshot.size()<<endl;

 for(int j=0; j < crossSnapshot.size();j++){
  printState(crossSnapshot[j]);
  //printTaker2(crossSnapshot[j]);
 }
}

void printEveryState2(vector<int> &takerSnapshot){
 cout << "The cross step is show as belowing." << endl;

 for(int j=0; j < takerSnapshot.size();j++){
  cout << j+1 << ". ";
  printTaker2(takerSnapshot[j]);
 }

 cout <<endl;
}

void recordTriedState(int state, int i, vector<int> &crossSnapshot){
 //save snapshot
 crossSnapshot.push_back(state);
 takerSnapshot.push_back(i);
}

void RollbackRecordState(int state, vector<int> &crossSnapshot){
 //delete previous snapshot
 crossSnapshot.pop_back();
 takerSnapshot.pop_back();
}

void crossRiver(int state, vector<int> &crossSnapshot){
 if (state == TARGET_STATE){
  //printEveryState(crossSnapshot);
  printEveryState2(takerSnapshot);
  //stop recurive iteration
  return;
 }

 //for every cross choice
 for(int i=0; i<4; i++){
  int nextCrossState = state ^ cross[i]; // use bitwise^ to toggle the Bit i

  if(isSafe(nextCrossState) && !isTry(nextCrossState)){
   recordTriedState(nextCrossState, i, crossSnapshot);

   //printTaker(i, state);
   //recurive to call itself
   crossRiver(nextCrossState, crossSnapshot);

   RollbackRecordState(nextCrossState, crossSnapshot);
  }
 }
}

int main() {
 int initialState = 0;

 //save first snapshot
 crossSnapshot.push_back(initialState);

 crossRiver(initialState, crossSnapshot);

    cout << "Press any key to be continue" << endl;
 getchar();
 return 0;
} 


Output:

The cross step is show as belowing.
1. farmer takes goat
2. farmer goes alone
3. farmer takes wolf
4. farmer takes goat
5. farmer takes cabbage
6. farmer goes alone
7. farmer takes goat

The cross step is show as belowing.
1. farmer takes goat
2. farmer goes alone
3. farmer takes cabbage
4. farmer takes goat
5. farmer takes wolf
6. farmer goes alone
7. farmer takes goat

Press any key to be continue

 
延伸閱讀:

狐狸、鵝、豆子問題 - 維基百科,自由的百科全書

http://en.wikipedia.org/wiki/Fox,_goose_and_bag_of_beans_puzzle

 數學遊戲與欣賞 - 過河問題

渡河問題 - 维基百科,自由的百科全书


過河問題的圖論解法 - 那個誰 - CSDNBlog

黑夜過橋 C++ 解題



    請幫助一家五口在黑夜中過河。

    注意事項:

    1.因為是黑夜,他們必須帶備燈。
    2.每位家庭成員都有不同的速度過橋,由1秒、3秒、6秒、8秒、到12秒。
    3.橋只可最多承受2人的重量。
    4.當2人同時在橋上行走,速度必跟隨較慢的一位。
    5.燈只足夠維持30秒。


#include <iostream>
#include <string>
#include <vector>

using namespace std;

const int TARGET_STATE = 64-1;  //  111 111b

vector<int> crossSnapshot;
vector<int> takerSnapshot;

void showPauseMsg();

enum{
 lamp=32,
 m12=16,
 m8=8,
 m6=4,
 m3=2,
 m1=1
};

int cross[] = {
 m12, m8, m6, m3, m1,
 m12|m8, m12|m6, m12|m3, m12|m1,
 m8|m6, m8|m3, m8|m1,
 m6|m3, m6|m1,
 m3|m1
};

int crossCost[]={
 12, 8, 6, 3, 1,
 12, 12, 12, 12,
 8,8,8,
 6,6,
 3
};

int crossUppIdx = (sizeof(cross)/ sizeof(int))-1;

void printState(int iState){
 //print out Bit N, N start from 0

 int N=8;
 for(int i=N-1;i>=0;i--)
 {
  cout << ((iState >> i ) & 1);//show Bit 3
 }
 cout << endl;
}

//get a binary digit N of iNumber, N start from Right to Left
// Rightmost is 1
//return 0 or 1, Bit N at iNumber

int getBitN(int iNumber, int N){
 return (iNumber >> (N-1)) & 1;
}

int getLampState (int iState){ return getBitN(iState, 6);}

bool isSafe(vector<int> &takerSnapshot, int addCost){
 int sum =0;

 for(int j=0; j<takerSnapshot.size();j++){
  sum += crossCost[takerSnapshot[j]];
 }

 sum += addCost;

 if (sum >30){ return false;}

 return true;
}

bool isTry(int state){
 bool isTried = false;

 for(int j=0; j < crossSnapshot.size();j++){
  if (state == crossSnapshot[j]){
   isTried = true;
   break;
  }
 }
 return isTried;
}

void printTaker(int i){
 char* msg[] = {
  "12", "8", "6", "3", "1",
  "(12,8)", "(12,6)", "(12,3)", "(12,1)",
  "(8,6)", "(8,3)", "(8,1)",
  "(6,3)", "(6,1)",
  "(3,1)"
 };

 if(i >= 0 && i <= crossUppIdx) {
  cout << msg[i] << endl;
 }
}

string getTaker(int i){
 char* msg[] = {
  "12", "8", "6", "3", "1",
  "(12,8)", "(12,6)", "(12,3)", "(12,1)",
  "(8,6)", "(8,3)", "(8,1)",
  "(6,3)", "(6,1)",
  "(3,1)"
 };

 if(i >= 0 && i <= crossUppIdx) {
  return string(msg[i]);
 }

 return string("");
}

void printEveryBitState(vector<int> &crossSnapshot){
 cout << "total number cross state = " << crossSnapshot.size()<<endl;

 for(int j=0; j < crossSnapshot.size();j++){
  printState(crossSnapshot[j]);
 }
}

void printEveryWordState(vector<int> &takerSnapshot){
 cout << "The cross step is show as belowing." << endl;

 for(int j=0; j < takerSnapshot.size();j++){
  cout << j+1 << ". ";
  cout << getTaker(takerSnapshot[j]);

  if (j%2 == 0){
   cout << " <---" << endl;
  } else {
   cout << " --->"<< endl;
  }
 }
 //add a dummy blank line
 cout <<endl;
}

void printTotalMins(vector<int> &takerSnapshot){
 int totalMins = 0;

 for(int j=0; j < takerSnapshot.size();j++){
  totalMins += crossCost[takerSnapshot[j]];
 }

 cout << "total mins used = " << totalMins << endl;
}

void recordTriedState(int state, int i, vector<int> &crossSnapshot){
 //save snapshot
 crossSnapshot.push_back(state);
 takerSnapshot.push_back(i);
}

void RollbackRecordState(int state, vector<int> &crossSnapshot){
 //delete previous snapshot

 //cout << "roll back ...." << endl << endl;
 //printEveryWordState(takerSnapshot);
 //printEveryBitState(crossSnapshot);

 crossSnapshot.pop_back();
 takerSnapshot.pop_back();
}

bool isMoverSameSideWithBoat(int currentState, int mover){
 bool rv = false;
 int lampState = getLampState(currentState);

 if(lampState==0)
 {
  if ((currentState & mover) == 0){ rv = true; }
 }
 else
 {
  if ((currentState & mover)== mover){ rv = true;  }
 }

 return rv;
}

void crossRiver(int state,
    vector<int> &crossSnapshot)
{
 if (state == TARGET_STATE){
  //printEveryBitState(crossSnapshot);
  printEveryWordState(takerSnapshot);
  printTotalMins(takerSnapshot);

  //stop recurive iteration
  showPauseMsg(); // if u want to print similarly answer, hide this code
  exit(0);  // if u want to print similarly answer, hide this code
  return;
 }

 //for every cross choice
 for(int i=0; i<=crossUppIdx; i++){
  int mover = cross[i];

  bool isMoverSameWithBoat = isMoverSameSideWithBoat(state, mover);

  // if mover object is not same side as boat
  if (!isMoverSameWithBoat){
   continue;
  }

  int nextCrossState = (state ^ mover) ^ lamp; // use bitwise^ to toggle the Bit i

  bool isSafed = isSafe(takerSnapshot, crossCost[i]);

  bool isNotTry = (!isTry(nextCrossState));

  if( isSafed && isNotTry ){
   //printState(nextCrossState);
   //printTaker(i);

   //int x = takerSnapshot.size();
   recordTriedState(nextCrossState, i, crossSnapshot);
   //recurive to call itself
   crossRiver(nextCrossState, crossSnapshot);

   RollbackRecordState(nextCrossState, crossSnapshot);
  }
 }
}

void showPauseMsg(){
    cout << "Press any key to be continue" << endl;
 getchar();
}

int main() {
 //save first snapshot
 crossSnapshot.push_back(0);

 crossRiver(0, crossSnapshot);

 //showPauseMsg();
 return 0;
}


The cross step is show as belowing.
1. (6,1) <---
2. 1 --->
3. (3,1) <---
4. 3 --->
5. (12,8) <---
6. 1 --->
7. (3,1) <---

total mins used = 29
Press any key to be continue

延伸閱讀:

其他人的文章,如果是 N個人一共只帶了一只手電筒等變形問題的版本
http://blog.csdn.net/wuzhekai1985/article/details/6846934

http://renren.it/a/bianchengyuyan/_NET/20111006/133147.html


黑夜過橋問題是可遞迴解的

一家六口過河問題 C++解題

一家六口過河問題 C++解題 出處不知,有多個變化身份的版本

版本1

一家庭欲過河.成員為:爸爸.媽媽.哥哥.姊姊.弟弟.妹妹.僕人.與一隻狗...
過河條件如下:
1.僅有一條小船可供渡河.每次最多搭乘2人(含駕駛)..且僅有爸爸.媽媽.僕人這3人會駕船..
2.爸爸不在兄弟身邊時.媽媽會打兄弟..
3.媽媽不在姊妹身邊時.爸爸會打姊妹..
4.僕人不在狗身邊時.狗會咬其他人..
請問這家人要如何平安過河?


版本2:


http://res.hkedcity.net/general/0001/51/31/riverIQGame.swf



全家人一起到郊外野餐,原本是歡樂的氣氛,卻碰到了逃獄的囚犯的襲擊,所幸逮捕他的警察也即時趕到,全家人因此暫時沒有危險了...;在警察一邊要保護全家人,一邊要看緊囚犯的路上,來到了一個需要過河的地方,但是只有一艘只能同時在兩人的小船,爸爸、媽媽、兩個兒子與兩個女兒,加上一名囚犯與一名警察這8個人該如何順利過河呢?


操作說明:

用滑鼠點選要乘船的人,在點選紅色拉桿,就可以過河。 注意,這家人有點...奇怪:

1.爸爸不可以在沒有媽媽的情況下跟女兒在一起,女兒會被責罵。

2.媽媽不可以在沒有爸爸的情況下跟兒子在一起,兒子會被修理。

3.囚犯不可以在沒有警察的情況下跟其他人在一起,囚犯會傷害他人。


這個版本被人用 flash 製造出來,有良好互動效果,所以就用這個題目做練習吧!

其他類似版本 iPad, iPhone

link


#include <iostream>
#include <string>
#include <vector>

using namespace std;

const int TARGET_STATE = 512-1;  // 1 1111 1111b

vector<int> crossSnapshot;
vector<string> takerSnapshot;

void showPauseMsg();

enum{
 Boat=256,
 Father=128,
 Mather=64,
 Police=32,
 Thief=16,

 Boy1=8,
 Boy2=4,
 Girl1=2,
 Girl2=1
};


int cross[] = {
 Father,//0
 Mather,
 Police,
 Father|Mather,
 Father|Police,
 Father|Boy1,
 Father|Boy2,

 Mather|Police,
 Mather|Girl1,
 Mather|Girl2,

 Police|Thief,
 Police|Boy1,
 Police|Boy2,
 Police|Girl1,
 Police|Girl2
};
int crossUppIdx = 14;

void printState(int iState){
 //print out Bit N, N start from 0

 int N=8;
 for(int i=N-1;i>=0;i--)
 {
  cout << ((iState >> i ) & 1);//show Bit 3
 }
 cout << endl;
}

//get a binary digit N of iNumber, N start from Right to Left
// Rightmost is 1
//return 0 or 1, Bit N at iNumber

int getBitN(int iNumber, int N){
 return (iNumber >> (N-1)) & 1;
}

int getBoatState (int iState){ return getBitN(iState, 9);}

int father (int iState){ return getBitN(iState, 8);}
int mother (int iState){ return getBitN(iState, 7);}
int police (int iState){ return getBitN(iState, 6);}
int thief  (int iState){ return getBitN(iState, 5);}
int boy1 (int iState){ return getBitN(iState, 4);}
int boy2(int iState){ return getBitN(iState, 3);}
int girl1(int iState){ return getBitN(iState, 2);}
int girl2(int iState){ return getBitN(iState, 1);}


bool isSafe(int state){
 if( police(state) != thief(state)
  && (thief(state) == father(state) ||
   thief(state) == mother(state) ||
   thief(state) == boy1(state) ||
   thief(state) == boy2(state) ||
   thief(state) == girl1(state) ||
   thief(state) == girl2(state))
  )
 {
  return false;
 }

 if( father(state)!= mother(state) &&
  (father(state) == girl1(state) ||
   father(state) == girl2(state))
  )
 {
  return false;
 }

 if( mother(state)!= father(state) &&
  (mother(state) == boy1(state) ||
   mother(state) == boy2(state))
  )
 {
  return false;
 }
 return true;
}

bool isTry(int state){
 bool isTried = false;

 for(int j=0; j < crossSnapshot.size();j++){
  //printState(crossSnapshot[j]);

  if (state == crossSnapshot[j]){
   isTried = true;
   break;
  }
 }
 return isTried;
}

void printTaker(int i){
 char* msg[] = {
  "father, _____",
  "mother, _____",
  "police, _____",

  "father, mother",
  "father, police",

  "father, boy1",
  "father, boy2",

  "mother, police",
  "mother, girl1",
  "mother, girl2",

  "police, thief",
  "police, boy1",
  "police, boy2",
  "police, girl1",
  "police, girl2"
 };

 if(i >= 0 && i <= crossUppIdx) {
  cout << msg[i] << endl;
 }
}


string getTaker(int i){
 char* msg[] = {
  "father, _____",
  "mother, _____",
  "police, _____",

  "father, mother",
  "father, police",

  "father, boy1",
  "father, boy2",

  "mother, police",
  "mother, girl1",
  "mother, girl2",

  "police, thief",
  "police, boy1",
  "police, boy2",
  "police, girl1",
  "police, girl2"
 };

 if(i >= 0 && i <= crossUppIdx) {
  return string(msg[i]);
 }

 return string("");
}

void printEveryBitState(vector<int> &crossSnapshot){
 cout << "total number cross state = " << crossSnapshot.size()<<endl;

 for(int j=0; j < crossSnapshot.size();j++){
  printState(crossSnapshot[j]);
 }
}

void printEveryWordState(vector<string> &takerSnapshot){
 cout << "The cross step is show as belowing." << endl;

 for(int j=0; j < takerSnapshot.size();j++){
  cout << j+1 << ". ";
  cout << takerSnapshot[j];

  if (j%2 == 0){
   cout << " --->" << endl;
  } else {
   cout << " <---"<< endl;
  }
 }
 //add a dummy blank line
 cout <<endl;
}

void recordTriedState(int state, int i, vector<int> &crossSnapshot){
 //save snapshot
 crossSnapshot.push_back(state);
 takerSnapshot.push_back(getTaker(i));
}

void RollbackRecordState(int state, vector<int> &crossSnapshot){
 //delete previous snapshot

 //cout << "roll back ...." << endl << endl;
 //printEveryWordState(takerSnapshot);
 //printEveryBitState(crossSnapshot);

 crossSnapshot.pop_back();
 takerSnapshot.pop_back();
}

bool isMoverSameSideWithBoat(int currentState, int mover){
 bool rv = false;
 int boat = getBoatState(currentState);

 if(boat==0)
 {
  if ((currentState & mover) == 0){ rv = true; }
 }
 else
 {
  if ((currentState & mover)== mover){ rv = true;  }
 }

 return rv;
}

void crossRiver(int state, vector<int> &crossSnapshot){
 if (state == TARGET_STATE){
  //printEveryBitState(crossSnapshot);
  printEveryWordState(takerSnapshot);
  //stop recurive iteration

  showPauseMsg(); // if u want to print similarly answer, hide this code
  exit(0);  // if u want to print similarly answer, hide this code
  return;
 }

 //for every cross choice
 for(int i=0; i<=crossUppIdx; i++){
  int mover = cross[i];

  bool isMoverSameWithBoat = isMoverSameSideWithBoat(state, mover);

  // if mover object is not same side as boat
  if (!isMoverSameWithBoat){
   continue;
  }

  int nextCrossState = (state ^ mover) ^ Boat; // use bitwise^ to toggle the Bit i

  bool isSafed = isSafe(nextCrossState);
  bool isNotTry = (!isTry(nextCrossState));

  if( isSafed && isNotTry ){
   //printState(nextCrossState);
   //printTaker(i);

   //int x = takerSnapshot.size();
   recordTriedState(nextCrossState, i, crossSnapshot);
   //recurive to call itself
   crossRiver(nextCrossState, crossSnapshot);

   RollbackRecordState(nextCrossState, crossSnapshot);
  }
 }
}

void showPauseMsg(){
    cout << "Press any key to be continue" << endl;
 getchar();
}

int main() {
 int initialState = 0;

 //save first snapshot
 crossSnapshot.push_back(initialState);
 crossRiver(initialState, crossSnapshot);

 //showPauseMsg();
 return 0;
}


Output: 結果



The cross step is show as belowing.

1. police, thief --->
2. police, _____ <---
3. police, boy1 --->
4. police, thief <---
5. father, boy2 --->
6. father, _____ <---
7. father, mother --->
8. mother, _____ <---
9. police, thief --->
10. father, _____ <---
11. father, mother --->
12. mother, _____ <---
13. mother, girl1 --->
14. police, thief <---
15. police, girl2 --->
16. police, _____ <---
17. police, thief --->

Press any key to be continue


-------------------------------------------------

DOS/capture screen output to a file

click the control menu button on a command window (you can find this button on the left upper corner). Or press Alt + space_bar.

Once the control menu opens, select Edit. From there you can Select All, Mark, or Copy the text you require from the command window.

This text is then copied to the standard Windows clipboard. From there you can paste it in another Windows program.