ann8497

Base Stations Samsung

Sep 5th, 2019
1,619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. /*
  2. our 5G base station towers needs to be installed in a Landscape which is divided as hexagon
  3. cells as shown in Fig below, which also contains number of people living in each cell. Need to find
  4. four cells to install the 5G towers which can cover maximum number of people combining all
  5. four cells, with below conditions
  6. – Only one tower can be placed in a cell
  7. – Each of the four chosen cell should be neighbor to atleast one of the remaining 3 cells.
  8. – All four cells should be connected (like one island)
  9. Refer next slide for some valid combinations
  10. Input range: 1 <= N, M <= 15
  11. Sample input Format for Fig in right
  12. 3 4
  13. 150 450 100 320
  14. 120 130 160 110
  15. 10 60 200 220
  16. Output
  17. Square of Maximum number of people covered by
  18. 4 towers
  19.  
  20. ANSWER FOR THIS TESTCASES IS 1130*1130
  21.  
  22. CODE IS VERIFIED FOR DIFFERENT FUNCTIONS
  23. */
  24.  
  25.  
  26. #include<iostream>
  27. using namespace std;
  28.  
  29. int n,m;
  30. int a[100][100];
  31. int vis[100][100];
  32.  
  33. int dx[] = {-2,-1,1,2,1,-1};
  34. int dy[] = {0,1,1,0,-1,-1};
  35.  
  36. bool valid(int r, int c){
  37.   return (r<2*n && r>=0 && c<m && c>=0);
  38. }
  39.  
  40. int sidha(int i, int j){
  41.   if(valid(i-1,j-1) && valid(i-1,j+1) && valid(i+2,j)){
  42.      return (a[i][j] + a[i-1][j-1] + a[i-1][j+1] + a[i+2][j]);
  43.   }
  44.   else return -1;
  45. }
  46.  
  47. int ulta(int i, int j){
  48.    if(valid(i+1,j-1) && valid(i+1,j+1) && valid(i-2,j)){
  49.      return a[i][j] + a[i+1][j-1] + a[i+1][j+1] + a[i-2][j];
  50.   }
  51.   else return -1;
  52. }
  53.  
  54. int dfs(int r, int c, int power){
  55.  
  56.   if(power == 0)return 0;
  57.  
  58.   vis[r][c] = 1;
  59.  
  60.   int ans = 0;
  61.   for(int i = 0; i<6; i++){
  62.  
  63.     int x = r + dx[i];
  64.     int y = c + dy[i];
  65.  
  66.     if(vis[x][y] == 0 && valid(x,y)){
  67.  
  68.       vis[x][y] = 1;
  69.       int temp = dfs(x,y,power -1);  
  70.       vis[x][y] = 0;
  71.  
  72.        ans = max(temp,ans);
  73.  
  74.     }
  75.   }
  76.  
  77.   vis[r][c] = 0;
  78.  
  79.   ans += a[r][c];
  80.   return ans;
  81. }
  82.  
  83. int main(){
  84.    
  85.    cin>>n>>m;
  86.     for(int i = 0; i<2*n;i+=2){
  87.         for(int j =0; j<m; j++){
  88.             if(!(j%2))
  89.             cin>>a[i][j];
  90.             else
  91.             cin>>a[i+1][j];
  92.         }        
  93.     }
  94.  
  95.    int ans = -1;
  96.    for(int i = 0; i<2*n; i++){
  97.      for(int j =0; j<m; j++){
  98.        if(a[i][j] ){
  99.          
  100.          int temp = dfs(i,j,4);
  101.          int temp2 = ulta(i,j);
  102.          int temp3 = sidha(i,j);
  103.          int temp1 = max(temp2, temp3);
  104.          ans = max(max(temp,temp1),ans);
  105.        }
  106.      }
  107.    }
  108.    
  109.     cout<<ans<<endl;
  110.    
  111.   return 0;
  112. }
Add Comment
Please, Sign In to add comment