Advertisement
a53

mindist

a53
Jan 1st, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. /*
  2.  
  3. Pirnog Theodor Ioan
  4. Colegiul National "B. P. Hasdeu"
  5.  
  6. */
  7.  
  8. #include <fstream>
  9. #include <queue>
  10.  
  11. using namespace std;
  12.  
  13. class InParser
  14. {
  15. private:
  16. FILE *fin;
  17. char *buff;
  18. int sp;
  19.  
  20. char read_ch()
  21. {
  22. ++sp;
  23. if(sp==4096)
  24. {
  25. sp=0;
  26. fread(buff,1,4096,fin);
  27. }
  28. return buff[sp];
  29. }
  30.  
  31. public:
  32. InParser(const char* nume)
  33. {
  34. fin=fopen(nume,"r");
  35. buff=new char[4096]();
  36. sp=4095;
  37. }
  38.  
  39. InParser& operator >> (int &n)
  40. {
  41. char c;
  42. while(!isdigit(c=read_ch())&&c!='-');
  43. int sgn=1;
  44. if (c=='-')
  45. {
  46. n=0;
  47. sgn=-1;
  48. }
  49. else
  50. {
  51. n=c-'0';
  52. }
  53. while(isdigit(c=read_ch()))
  54. {
  55. n=10*n+c-'0';
  56. }
  57. n*=sgn;
  58. return *this;
  59. }
  60.  
  61. InParser& operator >> (long long &n)
  62. {
  63. char c;
  64. n=0;
  65. while(!isdigit(c=read_ch())&&c!='-');
  66. long long sgn=1;
  67. if(c=='-')
  68. {
  69. n=0;
  70. sgn=-1;
  71. }
  72. else
  73. {
  74. n=c-'0';
  75. }
  76. while(isdigit(c=read_ch()))
  77. {
  78. n=10*n+c-'0';
  79. }
  80. n*=sgn;
  81. return *this;
  82. }
  83. };
  84.  
  85. class OutParser
  86. {
  87. private:
  88. FILE *fout;
  89. char *buff;
  90. int sp;
  91.  
  92. void write_ch(char ch)
  93. {
  94. if(sp==50000)
  95. {
  96. fwrite(buff,1,50000,fout);
  97. sp=0;
  98. buff[sp++]=ch;
  99. }
  100. else
  101. {
  102. buff[sp++]=ch;
  103. }
  104. }
  105.  
  106. public:
  107. OutParser(const char* name)
  108. {
  109. fout=fopen(name,"w");
  110. buff=new char[50000]();
  111. sp=0;
  112. }
  113. ~OutParser()
  114. {
  115. fwrite(buff,1,sp,fout);
  116. fclose(fout);
  117. }
  118.  
  119. OutParser& operator <<(int vu32)
  120. {
  121. if(vu32<=9)
  122. {
  123. write_ch(vu32+'0');
  124. }
  125. else
  126. {
  127. (*this) <<(vu32/10);
  128. write_ch(vu32%10+'0');
  129. }
  130. return *this;
  131. }
  132.  
  133. OutParser& operator <<(long long vu64)
  134. {
  135. if(vu64<=9)
  136. {
  137. write_ch(vu64+'0');
  138. }
  139. else
  140. {
  141. (*this) <<(vu64/10);
  142. write_ch(vu64%10+'0');
  143. }
  144. return *this;
  145. }
  146.  
  147. OutParser& operator <<(char ch)
  148. {
  149. write_ch(ch);
  150. return *this;
  151. }
  152. OutParser& operator <<(const char *ch)
  153. {
  154. while(*ch)
  155. {
  156. write_ch(*ch);
  157. ++ch;
  158. }
  159. return *this;
  160. }
  161. };
  162.  
  163.  
  164. InParser cin("mindist.in");
  165. OutParser cout("mindist.out");
  166.  
  167. const int NMAX = 1e3;
  168. int a[NMAX + 1][NMAX + 1], n, m, k, p1, p2;
  169. queue <pair <int, int>> q;
  170. int dl[] = {-1, 0, 1, 0};
  171. int dc[] = {0, 1, 0, -1};
  172.  
  173. bool inside(int x, int y){
  174. return(x <= n && x >= 1 && y <= n && y >= 1);
  175. }
  176.  
  177. void lee(int x, int y){
  178.  
  179. a[x][y] = 1;
  180. q.push({x, y});
  181.  
  182. while(!q.empty()){
  183.  
  184. int f1 = q.front().first;
  185. int f2 = q.front().second;
  186.  
  187. for(int i = 0; i < 4; i++){
  188.  
  189. int iv = f1 + dl[i];
  190. int jv = f2 + dc[i];
  191.  
  192. if(inside(iv, jv) && (!a[iv][jv] || a[f1][f2] + 1 < a[iv][jv])){
  193.  
  194. a[iv][jv] = a[f1][f2] + 1;
  195. q.push({iv, jv});
  196.  
  197. }
  198.  
  199. }
  200.  
  201. q.pop();
  202. }
  203.  
  204. }
  205.  
  206. int main(){
  207.  
  208. cin >> n >> m >> k;
  209. for(int i = 1; i <= m; i++){
  210.  
  211. cin >> p1 >> p2;
  212. a[p1][p2] = -1;
  213.  
  214. }
  215.  
  216. for(int i = 1; i <= k; i++){
  217.  
  218. cin >> p1 >> p2;
  219. if(a[p1][p2] != -1)
  220. lee(p1, p2);
  221.  
  222. }
  223.  
  224. for(int i = 1; i <= n; i++){
  225. for(int j = 1; j <= n; j++){
  226.  
  227. if(a[i][j] <= 0)
  228. cout << "-1 ";
  229. else if(a[i][j] != -1)
  230. cout << a[i][j] - 1 << " ";
  231.  
  232. }
  233. cout << "\n";
  234. }
  235.  
  236. }
  237.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement