ann8497

rare elements Samsung

Aug 15th, 2019
2,784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. /*
  2. remember the fucking indexing
  3. 5
  4. 5 2
  5. 4 3
  6. 3 4
  7. 1 1 0 0 0
  8. 1 1 0 0 0
  9. 1 1 1 1 1
  10. 1 1 1 0 1
  11. 1 1 1 1 1
  12. 8 2
  13. 5 6
  14. 6 4
  15. 1 1 1 1 1 1 0 0
  16. 1 1 1 1 1 1 1 0
  17. 1 1 0 1 0 1 1 0
  18. 1 1 1 1 0 1 1 0
  19. 1 1 1 1 1 1 1 0
  20. 1 1 1 1 1 1 1 0
  21. 0 0 0 0 0 0 0 0
  22. 0 0 0 0 0 0 0 0
  23. 10 3
  24. 8 2
  25. 5 3
  26. 7 1
  27. 0 0 0 1 1 1 1 1 1 0
  28. 1 1 1 1 1 1 1 1 1 0
  29. 1 0 0 1 0 0 0 0 1 0
  30. 1 1 1 1 1 1 1 1 1 1
  31. 1 1 1 1 0 1 0 0 1 1
  32. 1 1 1 1 0 1 0 0 1 1
  33. 1 1 1 1 0 1 0 0 1 1
  34. 1 1 1 1 1 1 1 1 1 1
  35. 1 1 1 0 0 1 0 0 1 1
  36. 1 1 1 1 1 1 1 1 1 1
  37. 15 4
  38. 11 15
  39. 15 9
  40. 1 2
  41. 14 3
  42. 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  43. 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1
  44. 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
  45. 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
  46. 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
  47. 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
  48. 1 0 1 0 0 0 1 1 1 1 1 1 1 1 1
  49. 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
  50. 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
  51. 1 0 1 0 0 0 1 0 0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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  81. 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
  82.  
  83. Output
  84. #1 1
  85. #2 2
  86. #3 2
  87. #4 12
  88. #5 15
  89.  
  90.  
  91. */
  92.  
  93. #include<iostream>
  94. using namespace std;
  95.  
  96. struct node{
  97.    int x;
  98.    int y;
  99.    int level;
  100. };
  101.  
  102. node q[1000];
  103. int front = 0, back = 0;
  104.  
  105. void init(){
  106.   front = back = 0;
  107. }
  108.  
  109. void push(int x, int y, int level){
  110.   q[back].x = x;
  111.   q[back].y = y;
  112.   q[back].level = level;
  113.   back++;
  114. }
  115. node pop(){
  116.    return q[front++];
  117. }
  118. bool empty(){
  119.   return (front == back);
  120. }
  121.  
  122.  
  123. int a[100][100];
  124. int rare[4][2];
  125. int c;
  126. int n;
  127.  
  128. bool valid(int r, int c){
  129.   return (r>=0 && r<n && c>=0 && c<n);
  130. }
  131.  
  132. int vis[100][100];
  133.  
  134. int xx[] = {-1,0,1,0};
  135. int yy[] = {0,1,0,-1};
  136.  
  137. int bfs(int sx,int sy,int dx,int dy){
  138.  
  139.     push(sx,sy,0);
  140.     vis[sx][sy] = 1;
  141.    
  142.     while(!empty()){
  143.  
  144.         node temp = pop();
  145.         if(temp.x == dx && temp.y == dy)return temp.level;
  146.    
  147.         for(int i = 0; i<4; i++){
  148.  
  149.             int valx = temp.x + xx[i];
  150.             int valy = temp.y + yy[i];
  151.             int lvl = temp.level + 1;
  152.  
  153.             if(valid(valx,valy)){
  154.                 if(a[valx][valy] == 1 && vis[valx][valy] == 0){
  155.                     push(valx,valy,lvl);
  156.                     vis[valx][valy] = 1;
  157.                 }
  158.             }
  159.         }
  160.     }
  161.    
  162. }
  163.  
  164.  
  165. int main(){
  166.  
  167.    
  168.   int t; cin>>t;
  169.   while(t--){
  170.      cin>>n;
  171.      cin>>c;
  172.    
  173.      init();
  174.    
  175.     for(int i =0; i<c; i++){
  176.        int x,y;cin>>x>>y;
  177.        /*  indexing is so fucking important*/
  178.        x--;y--;
  179.        rare[i][0] = x;
  180.        rare[i][1] = y;
  181.     }
  182.    
  183.     for(int i = 0; i<n; i++){
  184.       for(int j =0; j<n; j++){
  185.         cin>>a[i][j];
  186.       }
  187.     }
  188.    
  189.     int ans = 10000;
  190.    
  191.     for(int i =0; i<n; i++){
  192.       for(int j =0; j<n; j++){
  193.          int temp;
  194.         if(a[i][j] == 1){
  195.             temp = 0;
  196.            
  197.             for(int k = 0; k<c; k++){
  198.                 /*  fucking don't forgot to empty the queue */
  199.                 init();
  200.               for(int l =0; l<100; l++)for(int m =0; m<100; m++)vis[l][m] = 0;
  201.                int res = bfs(i,j,rare[k][0],rare[k][1]);
  202.                temp = max(res,temp);
  203.             }
  204.             /* if k bahar mt likhio pehle galti se likh dia tha */
  205.              ans = min(ans,temp);
  206.         }
  207.        
  208.       }
  209.     }
  210.      cout<<ans<<endl;
  211.   }
  212.    
  213.   return 0;
  214. }
Add Comment
Please, Sign In to add comment