Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n , m , z;
- int bfs () ;
- int main()
- {
- //freopen("output.txt","w",stdout) ;
- int h ;
- cin >> h ;
- for(int i = 0;i < h;i++){
- cin >> n >> m >> z ;
- if (z > n&&z > m) cout << -1 <<endl ;
- else
- cout << bfs() <<endl ;
- }
- return 0;
- }
- int bfs ()
- {
- queue <pair<int,int> > q ;
- q.push(make_pair(0,0));
- int cnt = 0 ,sz = 1;
- bool vis[n][m] = {false} ;
- for(;!q.empty();cnt++,sz = q.size())
- {
- while(sz--){
- pair<int,int> p = q.front() ;
- q.pop() ;
- if (vis[p.first][p.second])
- continue ;
- vis[p.first][p.second] = true ;
- cout << cnt << endl ;
- if (p.first == z ||p.second == z) return cnt ;
- q.push(make_pair(n,p.second)) ;
- q.push(make_pair(p.first,m)) ;
- q.push(make_pair(0,p.second)) ;
- q.push(make_pair(p.first,0)) ;
- if(p.first+p.second > n) q.push(make_pair(n,p.second-(n-p.first))) ;
- else q.push(make_pair(p.first+p.second,0)) ;
- if(p.first+p.second > m) q.push(make_pair(p.first-(m-p.second),m)) ;
- else q.push(make_pair(0,p.first+p.second)) ;
- }
- }
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement