Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n , m , z;
  6. int bfs () ;
  7.  
  8. int main()
  9. {
  10. //freopen("output.txt","w",stdout) ;
  11. int h ;
  12. cin >> h ;
  13. for(int i = 0;i < h;i++){
  14. cin >> n >> m >> z ;
  15. if (z > n&&z > m) cout << -1 <<endl ;
  16. else
  17. cout << bfs() <<endl ;
  18. }
  19.  
  20. return 0;
  21. }
  22.  
  23.  
  24. int bfs ()
  25. {
  26. queue <pair<int,int> > q ;
  27. q.push(make_pair(0,0));
  28. int cnt = 0 ,sz = 1;
  29. bool vis[n][m] = {false} ;
  30. for(;!q.empty();cnt++,sz = q.size())
  31. {
  32. while(sz--){
  33. pair<int,int> p = q.front() ;
  34. q.pop() ;
  35. if (vis[p.first][p.second])
  36. continue ;
  37. vis[p.first][p.second] = true ;
  38. cout << cnt << endl ;
  39. if (p.first == z ||p.second == z) return cnt ;
  40. q.push(make_pair(n,p.second)) ;
  41. q.push(make_pair(p.first,m)) ;
  42. q.push(make_pair(0,p.second)) ;
  43. q.push(make_pair(p.first,0)) ;
  44. if(p.first+p.second > n) q.push(make_pair(n,p.second-(n-p.first))) ;
  45. else q.push(make_pair(p.first+p.second,0)) ;
  46. if(p.first+p.second > m) q.push(make_pair(p.first-(m-p.second),m)) ;
  47. else q.push(make_pair(0,p.first+p.second)) ;
  48. }
  49. }
  50. return 0 ;
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement