arten_1337

Snakes and Ladders

Jun 1st, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. /*
  2.     Author - Arten
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. #define endl "\n"
  10. #define ll long long
  11. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  12.  
  13. int main(){
  14.     IOS;
  15.     int t;
  16.     cin >> t;
  17.     while(t--){
  18.         int board[100] = {};
  19.         int n, m, from, to;
  20.         cin >> n;
  21.         for(int i = 0; i < n; i++){
  22.             cin >> from >> to;
  23.             board[from] = to - from;
  24.         }
  25.         cin >> m;
  26.         for(int i = 0; i < m; i++){
  27.             cin >> from >> to ;
  28.             board[from] = to - from;
  29.         }
  30.         vector <int> adjList[101];
  31.  
  32.         for(int i = 1; i <= 100; i++){
  33.             if(i + 1 + board[i + 1] <= 100)
  34.                 adjList[i].push_back(i + 1 + board[i + 1]);
  35.             if(i + 2 + board[i + 2] <= 100)
  36.                 adjList[i].push_back(i + 2 + board[i + 2]);
  37.             if(i + 3 + board[i + 3] <= 100)
  38.                 adjList[i].push_back(i + 3 + board[i + 3]);
  39.             if(i + 4 + board[i + 4] <= 100)
  40.                 adjList[i].push_back(i + 4 + board[i + 4]);
  41.             if(i + 5 + board[i + 5] <= 100)
  42.                 adjList[i].push_back(i + 5 + board[i + 5]);
  43.             if(i + 6 + board[i + 6] <= 100)
  44.                 adjList[i].push_back(i + 6 + board[i + 6]);
  45.         }
  46.         /*
  47.         for(int i = 1; i <= 100; i++){
  48.             cout << i << "->";
  49.             for(auto j: adjList[i]){
  50.                 cout <<j << ",";
  51.             }
  52.             cout << endl;
  53.         }*/
  54.         queue<int> q;
  55.         vector<int> dist(101, INT_MAX);
  56.  
  57.         q.push(1);
  58.         dist[1] = 0;
  59.  
  60.         while(!q.empty()){
  61.             int node = q.front();
  62.             q.pop();
  63.             for(auto i: adjList[node]){
  64.                 if(dist[i] == INT_MAX){
  65.                     q.push(i);
  66.                     dist[i] = dist[node] + 1;
  67.                 }
  68.             }
  69.         }
  70.         cout << dist[100] << endl;
  71.     }
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment