Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author - Arten
- */
- #include <bits/stdc++.h>
- using namespace std;
- #define endl "\n"
- #define ll long long
- #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- int main(){
- IOS;
- int t;
- cin >> t;
- while(t--){
- int board[100] = {};
- int n, m, from, to;
- cin >> n;
- for(int i = 0; i < n; i++){
- cin >> from >> to;
- board[from] = to - from;
- }
- cin >> m;
- for(int i = 0; i < m; i++){
- cin >> from >> to ;
- board[from] = to - from;
- }
- vector <int> adjList[101];
- for(int i = 1; i <= 100; i++){
- if(i + 1 + board[i + 1] <= 100)
- adjList[i].push_back(i + 1 + board[i + 1]);
- if(i + 2 + board[i + 2] <= 100)
- adjList[i].push_back(i + 2 + board[i + 2]);
- if(i + 3 + board[i + 3] <= 100)
- adjList[i].push_back(i + 3 + board[i + 3]);
- if(i + 4 + board[i + 4] <= 100)
- adjList[i].push_back(i + 4 + board[i + 4]);
- if(i + 5 + board[i + 5] <= 100)
- adjList[i].push_back(i + 5 + board[i + 5]);
- if(i + 6 + board[i + 6] <= 100)
- adjList[i].push_back(i + 6 + board[i + 6]);
- }
- /*
- for(int i = 1; i <= 100; i++){
- cout << i << "->";
- for(auto j: adjList[i]){
- cout <<j << ",";
- }
- cout << endl;
- }*/
- queue<int> q;
- vector<int> dist(101, INT_MAX);
- q.push(1);
- dist[1] = 0;
- while(!q.empty()){
- int node = q.front();
- q.pop();
- for(auto i: adjList[node]){
- if(dist[i] == INT_MAX){
- q.push(i);
- dist[i] = dist[node] + 1;
- }
- }
- }
- cout << dist[100] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment