Advertisement
mbah_bejo

minimun dice

May 12th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct queueEntry
  5. {
  6.     int v;    
  7.     int dist;  
  8. };
  9.  
  10. void dadu(int ularTangga[], int N)
  11. {
  12.     bool *visited = new bool[N];
  13.     for (int i = 0; i < N; i++)
  14.         visited[i] = false;
  15.  
  16.     queue<queueEntry> q;
  17.  
  18.     visited[0] = true;
  19.     queueEntry s = {0, 0};
  20.     q.push(s);  
  21.  
  22.     queueEntry qe;
  23.     while (!q.empty())
  24.     {
  25.         qe = q.front();
  26.         int v = qe.v;
  27.  
  28.         if (v == N-1)
  29.             break;
  30.  
  31.         q.pop();
  32.         for (int j=v+1; j<=(v+6) && j<N; ++j)
  33.         {
  34.             if (!visited[j])
  35.             {
  36.                 queueEntry a;
  37.                 a.dist = (qe.dist + 1);
  38.                 visited[j] = true;
  39.  
  40.                 if (ularTangga[j] != -1)
  41.                     a.v = ularTangga[j];
  42.                 else
  43.                     a.v = j;
  44.                 q.push(a);
  45.             }
  46.         }
  47.     }
  48.  
  49.     cout << qe.dist << endl ;
  50. }
  51.  
  52. int main()
  53. {
  54.     ios::sync_with_stdio(false);
  55.     cin.tie(NULL);
  56.     int jumKot, num, role, a, b;
  57.    
  58.     cin >> jumKot >> num;
  59.     int ularTangga[jumKot];
  60.         memset(ularTangga,-1,sizeof(ularTangga[0])*jumKot);
  61.     while(num--){
  62.        
  63.         cin >> role;
  64.         while(role--){
  65.             cin >> a >> b;
  66.            ularTangga[a] = b;
  67.         }
  68.         dadu(ularTangga, jumKot);
  69.  
  70.         memset(ularTangga,-1,sizeof(ularTangga[0])*jumKot);
  71.     }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement