Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <bits/stdc++.h>
- #include <algorithm>
- #include <map>
- #include <vector>
- #include <queue>
- using namespace std;
- //ifstream cin("pirati.in");
- //ofstream cout("pirati.out");
- char v[1005][1005];
- int nr[1005][1005],comp,n,m,tata[1005*1005],adanc[1005*1005];
- int lin[8]={0,0,-1,-1,-1,1,1,1};
- int col[8]={-1,1,0,1,-1,0,1,-1};
- // bitset<1005*1005>viz;
- // vector<int>H[666013],M[1005*1005];
- vector <int> M[1005*1005];
- map <pair <int, int>, bool> H;
- const int mod=666013;
- queue<pair<int,int > >Q;
- bool verif(int i,int j)
- {
- if(i<1 || i>n || j<1 || j>m) return 0;
- return 1;
- }
- void lee(int i,int j)
- {
- nr[i][j]=comp;
- Q.push(make_pair(i,j));
- while(Q.size())
- {
- int a=Q.front().first;
- int b=Q.front().second;
- for(int u=0;u<8;u++)
- {
- int nx = a + lin[u];
- int ny = b + col[u];
- if(verif(nx,ny)==1 && nr[nx][ny]==0 && v[i][j]==v[nx][ny])
- {
- nr[nx][ny]=comp;
- Q.push(make_pair(nx,ny));
- }
- }
- Q.pop();
- }
- }
- bool verificare_muchie(int a,int b)
- {
- return H[make_pair(a, b)];
- // int zum=a%mod;
- // for(vector<int>::iterator it=H[zum].begin();it!=H[zum].end();it++)
- // {
- // if(*it==b) return 1;
- // }
- // return 0;
- }
- // int HashFn(int x) {
- // long long ans = 0;
- // while (x) {
- // ans *= 73;
- // ans += x % 10;
- // ans %= mod;
- // x /= 10;
- // }
- // return ans;
- // }
- void adauga(int a,int b)
- {
- H[make_pair(a, b)] = true;
- // H[HashFn(a)].push_back(b);
- // H[HashFn(b)].push_back(a);
- M[a].push_back(b);
- // M[b].push_back(a);
- }
- void dfs(int nod,int papa)
- {
- // fout << nod << endl;
- // viz[nod]=1;
- tata[nod]=papa;
- // for(vector<int>::iterator it=M[nod].begin();it!=M[nod].end();it++)
- for(auto const &it: M[nod])
- {
- if(it==papa) continue;
- adanc[it]=adanc[nod]+1;
- /*if(viz[*it]==0)*/ dfs(it,nod);
- }
- }
- int distanta(int a,int b)
- {
- int dist=0;
- if(adanc[a]<adanc[b]) swap(a,b);
- while(adanc[a]>adanc[b])
- {
- dist++;
- // fout << a << endl;
- a=tata[a];
- }
- while(a!=b)
- {
- dist+=2;
- // fout << a << ' ' << b << endl;
- a=tata[a];
- b=tata[b];
- }
- return dist;
- }
- int main()
- {
- int q,i,j,u,a,b,x1,y1,x2,y2;
- scanf("%d %d %d", &n, &m, &q);
- for(int i = 1; i <= n; ++i) scanf("%s", v[i] + 1);
- // cin>>n>>m>>q;
- // for(i=1;i<=n;i++) cin>>(v[i]+1);
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=m;j++)
- {
- if(nr[i][j]==0)
- {
- comp++;
- lee(i,j);
- }
- }
- }
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=m;j++)
- {
- if(v[i][j] == '0') continue;
- for(u=0;u<8;u++)
- {
- a=i+lin[u];
- b=j+col[u];
- if(verif(a, b) == 0) continue;
- if(nr[i][j]!=nr[a][b])
- {
- if(verificare_muchie(nr[i][j],nr[a][b])==0)
- {
- adauga(nr[i][j],nr[a][b]);
- adauga(nr[a][b], nr[i][j]);
- }
- }
- }
- }
- }
- // for (int i = 1; i <= comp; ++i) {
- // fout << i << ": ";
- // for_each(M[i].begin(), M[i].end(), [&](const int& x) {
- // fout << x << ' ';
- // });
- // fout << '\n';
- // }
- dfs(1,0);
- for(i=1;i<=q;i++)
- {
- scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
- printf("%d\n", distanta(nr[x1][y1],nr[x2][y2])/2);
- // cin>>x1>>y1>>x2>>y2;
- // cout<<distanta(nr[x1][y1],nr[x2][y2])/2<<"\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement