Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Time for a comeback or to just keep getting annihilated till now...*/
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long LL ;
- #define get(a) for( int i = 0; i < n; i++) sf("%d", &a[i]) ;
- #define bug printf("TRUST\n");
- #define pf printf
- #define sf scanf
- #define bye vector<LL>
- #define pb push_back
- #define r0 return 0
- #define ff first
- #define sc second
- #define mp make_pair
- #define pii pair <int, int>
- #define mod 1000000007
- #define nod 200003
- void func(void)
- {
- freopen("input.c","r",stdin);
- freopen("output.c","w",stdout);
- }
- vector <int> a[500];
- int vis[500];
- void bfs (LL x, LL y) {
- queue <int> Q ;
- memset (vis, 0, sizeof(vis)) ;
- vis[x] = 1;
- Q.push(x);
- while(!Q.empty()) {
- int node = Q.front() ;
- Q.pop() ;
- int size = a[node].size() ;
- for (int i = 0 ; i<size ;i++ ) {
- int cur = a[node][i] ;
- if (vis[cur] == 0) {
- vis[cur] = vis[node] + 1 ;
- //energy++;
- Q.push(cur) ;
- if(cur==y){
- break;
- }
- }
- }
- if(vis[y]) break;
- }
- }
- int main(){
- 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;
- //func();
- cin >> x >> n;
- y = n*n;
- for(i = 1; i <= y; i++){
- a[i].pb(i);
- k = i % n;
- // if(i - n >= 1)a[i].pb(i- n);
- if(i - n + 1>= 1 && k != 0)a[i].pb(i - n + 1);
- if(i - n - 1 >= 1 && k != 1)a[i].pb(i - n - 1);
- // if(i + n <= y)a[i].pb(i+n);
- if(i + n - 1 <= y && k != 1)a[i].pb(i+n - 1);
- if(i + n + 1 <= y && k != 0 )a[i].pb(i+n + 1);
- // if(k != 1)a[i].pb(i - 1);
- // if(k != 0)a[i].pb(i+1);
- }
- /* for(i = 1; i <= y; i++){
- cout << "THIS " << i << " === " ;
- for(j = 0; j < a[i].size(); j++)cout << a[i][j] << " ";
- cout << endl;
- }*/
- for(i = 1; i <= x; i++){
- sf("%lld%lld", &j, &k);
- LL c = (n*(j) + (k+1)); // c = current pos
- sf("%lld%lld", &j, &k);
- LL t = (n*(j) + (k+1)); // t = pos of the target
- // cout << c << " " << t << endl;
- //if(!c)c = 1;
- // if(!t)t=1;
- bfs(c, t);
- if(vis[t]>= 1)
- cout << vis[t] - 1 << endl;
- else cout << "can't reach!\n";
- }
- r0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement