Advertisement
Guest User

maruf vai

a guest
Aug 24th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. /* Time for a comeback or to just keep getting annihilated till now...*/
  2.  
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. typedef long long LL ;
  7. #define get(a) for( int i = 0; i < n; i++) sf("%d", &a[i]) ;
  8. #define bug printf("TRUST\n");
  9. #define pf printf
  10. #define sf scanf
  11. #define bye vector<LL>
  12. #define pb push_back
  13. #define r0 return 0
  14. #define ff first
  15. #define sc second
  16. #define mp make_pair
  17. #define pii pair <int, int>
  18. #define mod 1000000007
  19. #define nod 200003
  20.  
  21. void func(void)
  22. {
  23. freopen("input.c","r",stdin);
  24. freopen("output.c","w",stdout);
  25. }
  26. vector <int> a[500];
  27. int vis[500];
  28. void bfs (LL x, LL y) {
  29. queue <int> Q ;
  30. memset (vis, 0, sizeof(vis)) ;
  31. vis[x] = 1;
  32. Q.push(x);
  33. while(!Q.empty()) {
  34. int node = Q.front() ;
  35. Q.pop() ;
  36. int size = a[node].size() ;
  37. for (int i = 0 ; i<size ;i++ ) {
  38. int cur = a[node][i] ;
  39. if (vis[cur] == 0) {
  40. vis[cur] = vis[node] + 1 ;
  41. //energy++;
  42. Q.push(cur) ;
  43. if(cur==y){
  44. break;
  45. }
  46. }
  47. }
  48. if(vis[y]) break;
  49. }
  50. }
  51.  
  52. int main(){
  53. LL n, q, i,j = 0, temp, t, k, ans = 0, sum = 0, cnt = 0, m, fg = 0, x, y, mx = 0, mx1 = 0, mn = 10000000000000000, mn1 = 10000000000000000;
  54. //func();
  55. cin >> x >> n;
  56.  
  57. y = n*n;
  58. for(i = 1; i <= y; i++){
  59. a[i].pb(i);
  60. k = i % n;
  61. // if(i - n >= 1)a[i].pb(i- n);
  62. if(i - n + 1>= 1 && k != 0)a[i].pb(i - n + 1);
  63. if(i - n - 1 >= 1 && k != 1)a[i].pb(i - n - 1);
  64. // if(i + n <= y)a[i].pb(i+n);
  65. if(i + n - 1 <= y && k != 1)a[i].pb(i+n - 1);
  66. if(i + n + 1 <= y && k != 0 )a[i].pb(i+n + 1);
  67. // if(k != 1)a[i].pb(i - 1);
  68. // if(k != 0)a[i].pb(i+1);
  69. }
  70. /* for(i = 1; i <= y; i++){
  71. cout << "THIS " << i << " === " ;
  72. for(j = 0; j < a[i].size(); j++)cout << a[i][j] << " ";
  73. cout << endl;
  74. }*/
  75. for(i = 1; i <= x; i++){
  76. sf("%lld%lld", &j, &k);
  77. LL c = (n*(j) + (k+1)); // c = current pos
  78. sf("%lld%lld", &j, &k);
  79. LL t = (n*(j) + (k+1)); // t = pos of the target
  80. // cout << c << " " << t << endl;
  81. //if(!c)c = 1;
  82. // if(!t)t=1;
  83. bfs(c, t);
  84. if(vis[t]>= 1)
  85. cout << vis[t] - 1 << endl;
  86. else cout << "can't reach!\n";
  87. }
  88. r0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement