tendua

Missionaries-Cannibal Problem

Aug 25th, 2011
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. # include <iostream>
  2. # include <cstdio>
  3. # include <set>
  4. # include <queue>
  5.  
  6. using namespace std;
  7.  
  8. # define FOR(C, F, L) for ( int C = F; C <= L; C++ )
  9. # define MP(X,Y) make_pair((X),(Y))
  10.  
  11.  
  12. typedef pair<int, int> pi;
  13. typedef pair<pi, pi>ppii;
  14. typedef pair<pi,int> ppi;
  15. queue < ppii > Q;
  16. int C, M, R, level=0;
  17. set <pair<pi,int> > S;
  18.  
  19. int elementFound ( ppi node)
  20. {
  21.     set < ppi > :: iterator it;
  22.     it = S.find(node);
  23.     if ( it == S.end() )return 0;
  24.     else return 1;
  25. }
  26.  
  27. void generatecases ( ppii X )
  28. {
  29.     int x = X.first.first;
  30.     int y = X.first.second;
  31.     int b = X.second.first;
  32.     int dist = X.second.second;
  33.     if (b)
  34.         {
  35.         b += 1; b %= 2;
  36.         FOR(I,0,R)
  37.         FOR(J,0,R)
  38.             {
  39.             if (!(I+J));
  40.             else {
  41.                 if ( (y-J>=0) && (I+J <=R))
  42.                 if ((x-I==0) || (x-I==M) ||
  43.                      ((x-I==y-J) &&(M-(x-I) == C-(y-J)&&(x-I>=0))))
  44.                     {
  45.                     ppi temp = MP(MP(x-I,y-J),b);
  46.                     if ( !elementFound(temp))
  47.                         {
  48.                         Q.push(MP(MP(x-I,y-J),MP(b,dist+1)));
  49.                         S.insert(temp);
  50.                         }
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.     else { 
  56.         b += 1; b %= 2;
  57.         FOR(I,0,R)
  58.         FOR(J,0,R)
  59.             {
  60.             if (!(I+J));
  61.             else {
  62.                 if ((y+J<=C) && (I+J<=R))
  63.                 if ((x+I==M) || (x+I== 0) ||
  64.                      (x+I==y+J && M-(x+I)==C-(y+J)&&(x+I<=M)) )
  65.                     {
  66.                     ppi temp = MP(MP(x+I,y+J),b);
  67.                     if ( !elementFound(temp) )
  68.                         {
  69.                         Q.push(MP(MP(x+I,y+J),MP(b, dist+1)));
  70.                         S.insert( temp );
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.         }
  76. }
  77.  
  78. int main()
  79. {
  80.     int t;cin >> t;
  81.     FOR(U,1,t){
  82.     scanf("%d%d%d",&M,&C,&R);
  83.     while(!Q.empty())Q.pop();
  84.     S.erase(S.begin(),S.end());
  85.     Q.push(MP(MP(M,C),MP(1,0)));
  86.     S.insert(MP(MP(M,C),1));
  87.     int flag=1;
  88.     while (!Q.empty())
  89.     {
  90.         ppii top = Q.front();
  91.         Q.pop();
  92.         if (top.first.first==0 && top.first.second==0)
  93.             {
  94.             cout << top.second.second <<endl;flag=0;break;
  95.             }
  96.         else generatecases(top);
  97.     }
  98.     if(flag){
  99.     int impossible = -1;
  100.     cout << impossible<<endl;}
  101. }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment