lelouche29

Rare_elements_Samsung

Sep 11th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. /*
  2. 5
  3. 5 2
  4. 4 3
  5. 3 4
  6. 1 1 0 0 0
  7. 1 1 0 0 0
  8. 1 1 1 1 1
  9. 1 1 1 0 1
  10. 1 1 1 1 1
  11. 8 2
  12. 5 6
  13. 6 4
  14. 1 1 1 1 1 1 0 0
  15. 1 1 1 1 1 1 1 0
  16. 1 1 0 1 0 1 1 0
  17. 1 1 1 1 0 1 1 0
  18. 1 1 1 1 1 1 1 0
  19. 1 1 1 1 1 1 1 0
  20. 0 0 0 0 0 0 0 0
  21. 0 0 0 0 0 0 0 0
  22. 10 3
  23. 8 2
  24. 5 3
  25. 7 1
  26. 0 0 0 1 1 1 1 1 1 0
  27. 1 1 1 1 1 1 1 1 1 0
  28. 1 0 0 1 0 0 0 0 1 0
  29. 1 1 1 1 1 1 1 1 1 1
  30. 1 1 1 1 0 1 0 0 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 1 1 1 1 1 1
  34. 1 1 1 0 0 1 0 0 1 1
  35. 1 1 1 1 1 1 1 1 1 1
  36. 15 4
  37. 11 15
  38. 15 9
  39. 1 2
  40. 14 3
  41. 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  42. 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1
  43. 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
  44. 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
  45. 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
  46. 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
  47. 1 0 1 0 0 0 1 1 1 1 1 1 1 1 1
  48. 1 0 1 0 0 0 1 0 0 0 0 1 1 0 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 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1 1 1 1 1 1 1 1 1 1 1 1
  80. 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
  81.  
  82. output:-
  83. 1
  84. 2
  85. 2
  86. 12
  87. 15
  88. */
  89.  
  90. #include <iostream>
  91. using namespace std;
  92.  
  93. class que{
  94.     int row[1000];
  95.     int col[1000];
  96.     int front,rear;
  97.     public:
  98.         que(){
  99.             front=rear=0;
  100.         }
  101.         void push(int x,int y){
  102.             row[rear]=x;
  103.             col[rear]=y;
  104.             rear++;
  105.         }
  106.         void pop(){
  107.             front++;
  108.         }
  109.         void top(int *x,int *y){
  110.             *x=row[front];
  111.             *y=col[front];
  112.         }
  113.         bool empty(){
  114.             if(front==rear) return true;
  115.             return false;
  116.         }
  117. };
  118.  
  119. struct points{
  120.     int x, y;
  121. };
  122.  
  123. int mat[21][21];
  124. int n,m;
  125.  
  126. bool canChek(int x,int y,bool visited[][25]){
  127.     if(x>=0 && x<n && y>=0 && y<n && visited[x][y]==false && mat[x][y]==1)
  128.         return true;
  129.     return false;
  130. }
  131.  
  132. int bfs(int s1, int s2,int d1,int d2){
  133.     bool visited[25][25]={false};
  134.     int distance[100][100];
  135.  
  136.     for(int i=0; i<n; i++)
  137.         for(int j=0; j<n; j++)
  138.             distance[i][j]=-1;
  139.  
  140.     que q;
  141.     q.push(s1,s2);
  142.     visited[s1][s2]=true;
  143.     distance[s1][s2]=0;
  144.  
  145.     int ans=0;
  146.     while(!q.empty()){
  147.         int x,y;
  148.         q.top(&x,&y);
  149.         q.pop();
  150.  
  151.         // if(x==d1 && y==d2) return distance[d1][d2];
  152.         int dx[]={-1,0,1,0};
  153.         int dy[]={0,1,0,-1};
  154.         for(int i=0; i<4; i++){
  155.             int m = x + dx[i];
  156.             int n = y + dy[i];
  157.  
  158.             if(canChek(m,n,visited)){
  159.                 q.push(m,n);
  160.                 visited[m][n]=true;
  161.                 distance[m][n]= distance[x][y] +1;
  162.             }
  163.         }
  164.         // cout<<"hello ";
  165.     }
  166.     return distance[d1][d2];
  167. }
  168.  
  169. int main() {
  170.     int t;
  171.     cin>>t;
  172.     while(t--){    
  173.         cin>>n>>m;
  174.  
  175.         //rare elements location
  176.         points rare[5];
  177.         for(int i=0; i<m; i++){
  178.             cin>>rare[i].x>>rare[i].y;
  179.             // rare[i].x--;
  180.             // rare[i].y--;
  181.         }
  182.  
  183.         //matrix representing roads
  184.         for(int i=0; i<n; i++)
  185.             for(int j=0; j<n; j++)
  186.                 cin>>mat[i][j];
  187.  
  188.         int shortest_longest_distance=99999;
  189.  
  190.         for(int i=0; i<n; i++){
  191.             for(int j=0; j<n; j++){
  192.                 if(mat[i][j]==1){
  193.                     int longest_distance=0;
  194.                     for(int k=0; k<m; k++){
  195.                         int ans= bfs(i,j,rare[k].x-1,rare[k].y-1);
  196.                         longest_distance = max(longest_distance,ans);
  197.                     }
  198.                     shortest_longest_distance = min(longest_distance,shortest_longest_distance);
  199.                 }
  200.             }
  201.         }
  202.         cout<<shortest_longest_distance<<endl;
  203.     }
  204. }
Add Comment
Please, Sign In to add comment