Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. #include <fstream>
  2. #include <bits/stdc++.h>
  3. #include <algorithm>
  4. #include <map>
  5. #include <vector>
  6. #include <queue>
  7.  
  8. using namespace std;
  9. //ifstream cin("pirati.in");
  10. //ofstream cout("pirati.out");
  11. char v[1005][1005];
  12. int nr[1005][1005],comp,n,m,tata[1005*1005],adanc[1005*1005];
  13. int lin[8]={0,0,-1,-1,-1,1,1,1};
  14. int col[8]={-1,1,0,1,-1,0,1,-1};
  15. // bitset<1005*1005>viz;
  16. // vector<int>H[666013],M[1005*1005];
  17. vector <int> M[1005*1005];
  18. map <pair <int, int>, bool> H;
  19. const int mod=666013;
  20. queue<pair<int,int > >Q;
  21. bool verif(int i,int j)
  22. {
  23. if(i<1 || i>n || j<1 || j>m) return 0;
  24. return 1;
  25. }
  26. void lee(int i,int j)
  27. {
  28. nr[i][j]=comp;
  29. Q.push(make_pair(i,j));
  30. while(Q.size())
  31. {
  32. int a=Q.front().first;
  33. int b=Q.front().second;
  34. for(int u=0;u<8;u++)
  35. {
  36. int nx = a + lin[u];
  37. int ny = b + col[u];
  38. if(verif(nx,ny)==1 && nr[nx][ny]==0 && v[i][j]==v[nx][ny])
  39. {
  40. nr[nx][ny]=comp;
  41. Q.push(make_pair(nx,ny));
  42. }
  43. }
  44. Q.pop();
  45. }
  46. }
  47. bool verificare_muchie(int a,int b)
  48. {
  49. return H[make_pair(a, b)];
  50. // int zum=a%mod;
  51. // for(vector<int>::iterator it=H[zum].begin();it!=H[zum].end();it++)
  52. // {
  53. // if(*it==b) return 1;
  54. // }
  55. // return 0;
  56. }
  57.  
  58. // int HashFn(int x) {
  59. // long long ans = 0;
  60. // while (x) {
  61. // ans *= 73;
  62. // ans += x % 10;
  63. // ans %= mod;
  64. // x /= 10;
  65. // }
  66.  
  67. // return ans;
  68. // }
  69.  
  70. void adauga(int a,int b)
  71. {
  72. H[make_pair(a, b)] = true;
  73. // H[HashFn(a)].push_back(b);
  74. // H[HashFn(b)].push_back(a);
  75. M[a].push_back(b);
  76. // M[b].push_back(a);
  77. }
  78. void dfs(int nod,int papa)
  79. {
  80. // fout << nod << endl;
  81. // viz[nod]=1;
  82. tata[nod]=papa;
  83. // for(vector<int>::iterator it=M[nod].begin();it!=M[nod].end();it++)
  84. for(auto const &it: M[nod])
  85. {
  86. if(it==papa) continue;
  87. adanc[it]=adanc[nod]+1;
  88. /*if(viz[*it]==0)*/ dfs(it,nod);
  89. }
  90. }
  91. int distanta(int a,int b)
  92. {
  93. int dist=0;
  94. if(adanc[a]<adanc[b]) swap(a,b);
  95. while(adanc[a]>adanc[b])
  96. {
  97. dist++;
  98. // fout << a << endl;
  99. a=tata[a];
  100. }
  101. while(a!=b)
  102. {
  103. dist+=2;
  104. // fout << a << ' ' << b << endl;
  105. a=tata[a];
  106. b=tata[b];
  107. }
  108. return dist;
  109. }
  110. int main()
  111. {
  112. int q,i,j,u,a,b,x1,y1,x2,y2;
  113. scanf("%d %d %d", &n, &m, &q);
  114. for(int i = 1; i <= n; ++i) scanf("%s", v[i] + 1);
  115.  
  116. // cin>>n>>m>>q;
  117. // for(i=1;i<=n;i++) cin>>(v[i]+1);
  118.  
  119. for(i=1;i<=n;i++)
  120. {
  121. for(j=1;j<=m;j++)
  122. {
  123. if(nr[i][j]==0)
  124. {
  125. comp++;
  126. lee(i,j);
  127. }
  128. }
  129. }
  130.  
  131. for(i=1;i<=n;i++)
  132. {
  133. for(j=1;j<=m;j++)
  134. {
  135. if(v[i][j] == '0') continue;
  136. for(u=0;u<8;u++)
  137. {
  138. a=i+lin[u];
  139. b=j+col[u];
  140. if(verif(a, b) == 0) continue;
  141. if(nr[i][j]!=nr[a][b])
  142. {
  143. if(verificare_muchie(nr[i][j],nr[a][b])==0)
  144.  
  145. {
  146. adauga(nr[i][j],nr[a][b]);
  147. adauga(nr[a][b], nr[i][j]);
  148. }
  149. }
  150. }
  151. }
  152. }
  153.  
  154. // for (int i = 1; i <= comp; ++i) {
  155. // fout << i << ": ";
  156. // for_each(M[i].begin(), M[i].end(), [&](const int& x) {
  157. // fout << x << ' ';
  158. // });
  159.  
  160. // fout << '\n';
  161. // }
  162.  
  163. dfs(1,0);
  164. for(i=1;i<=q;i++)
  165. {
  166. scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  167. printf("%d\n", distanta(nr[x1][y1],nr[x2][y2])/2);
  168. // cin>>x1>>y1>>x2>>y2;
  169. // cout<<distanta(nr[x1][y1],nr[x2][y2])/2<<"\n";
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement