Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include "function.h"
  4.  
  5. using namespace std;
  6.  
  7. void Crossing::solve()
  8. {
  9.     _npeople.push_back(0);
  10.     _npeople.push_back(0);
  11.     _npeople.push_back(1);
  12.     list<State> a;
  13.     a.push_back(_npeople);
  14.     _paths.insert(a);
  15.     while(!_paths.empty())
  16.     {
  17.         set<list<State>> newPaths;
  18.         set<list<State>> oldPaths;
  19.  
  20.         for (auto p : _paths)//p is a list<State>
  21.         {
  22.             //cout << "!!!!";
  23.             _explored.insert(p.back());//last elem. of p
  24.             //cout << "!!!!";
  25.             auto nextStates = extend(p.back());
  26.             //cout << nextStates.size() << endl;
  27.             for (auto s : nextStates)
  28.             {
  29.                 if (found(s))
  30.                 {
  31.                     //cout << "!!!!";
  32.                     auto np = p;
  33.                     np.push_back(s);
  34.                     _solutions.insert(np);
  35.                 }
  36.                 else
  37.                 {
  38.                     auto search = _explored.find(s);
  39.                     if (search == _explored.cend())
  40.                     {
  41.                         auto np = p;
  42.                         np.push_back(s);
  43.                         newPaths.insert(np);
  44.                     }
  45.                 }
  46.             }
  47.             oldPaths.insert(p);
  48.         }
  49.  
  50.         for (auto p : oldPaths)
  51.         {
  52.             _paths.erase(p);
  53.         }
  54.         for (auto p : newPaths)
  55.         {
  56.             _paths.insert(p);
  57.             //cout << "!!!!\n";
  58.         }
  59.       /*  if(!_paths.empty())
  60.             cout << 1234;*/
  61.         //--steps;
  62.     }
  63. }
  64. set<State> Crossing::extend(State s)
  65. {
  66.     set<State> ss;
  67.     int shipmem[5][2] = {{1,0},{0,1},{1,1},{2,0},{0,2}};
  68.     for (int i = 0; i <= 4; i++)
  69.     {
  70.         //
  71.         State newstate;
  72.         if (s[4] == 1)
  73.         {
  74.             newstate.push_back(s[0]-shipmem[i][0]);
  75.             newstate.push_back(s[1]-shipmem[i][1]);
  76.             newstate.push_back(s[2]+shipmem[i][0]);
  77.             newstate.push_back(s[3]+shipmem[i][1]);
  78.             newstate.push_back(-1);
  79.             //cout << newstate[0] << newstate[1] << newstate[2] << newstate[3] << endl;
  80.         }
  81.         else
  82.         {
  83.             newstate.push_back(s[0]+shipmem[i][0]);
  84.             newstate.push_back(s[1]+shipmem[i][1]);
  85.             newstate.push_back(s[2]-shipmem[i][0]);
  86.             newstate.push_back(s[3]-shipmem[i][1]);
  87.             newstate.push_back(1);
  88.         }
  89.  
  90.         if (valid(newstate))
  91.         {
  92.             ss.insert(newstate);
  93.             //cout << "?";
  94.         }
  95.     }
  96.     return ss;
  97. }
  98. bool Crossing::valid(State s)
  99. {
  100.     if (s[1] > s[0] && s[0] != 0)
  101.         return false;
  102.     if (s[3] > s[2] && s[2] != 0)
  103.         return false;
  104.     if (s[0] < 0 || s[1] < 0 || s[2] < 0 || s[3] < 0)
  105.         return false;
  106.     return true;
  107. }
  108. bool Crossing::found(State s)
  109. {
  110.     if (s[0] == 0 && s[1] == 0)
  111.     {
  112.         return true;
  113.     }
  114.     return false;
  115. }
  116. State Crossing::Go(State s, int missionary, int cannibal)
  117. {
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement