lelouche29

HexagonBaseStation_Samsung

Sep 14th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. //tested and verified
  2. /*
  3. our 5G base station towers needs to be installed in a Landscape which is divided as hexagon
  4. cells as shown in Fig below, which also contains number of people living in each cell. Need to find
  5. four cells to install the 5G towers which can cover maximum number of people combining all
  6. four cells, with below conditions
  7. – Only one tower can be placed in a cell
  8. – Each of the four chosen cell should be neighbor to atleast one of the remaining 3 cells.
  9. – All four cells should be connected (like one island)
  10. Refer next slide for some valid combinations
  11. Input range: 1 <= N, M <= 15
  12. Sample input Format for Fig in right
  13. 3 4
  14. 150 450 100 320
  15. 120 130 160 110
  16. 10 60 200 220
  17. Output
  18. Square of Maximum number of people covered by
  19. 4 towers
  20.  
  21. ANSWER FOR THIS TESTCASES IS 1130*1130
  22.  
  23. CODE IS VERIFIED FOR DIFFERENT FUNCTIONS
  24. */
  25.  
  26. #include <iostream>
  27. using namespace std;
  28.  
  29. //no of rows and colomns
  30. int n,m;
  31. int a[1000][1000];
  32. bool visited[1000][1000];
  33. int dx[] = {-2,-1,1,2,1,-1};
  34. int dy[] = {0,1,1,0,-1,-1};
  35.  
  36. bool valid(int x, int y){
  37.     if(x>=0 && x<2*n && y>=0 && y<m)
  38.         return true;
  39.     return false;
  40. }
  41.  
  42. int non_inverted_Y(int x, int y){
  43.     if(valid(x+2,y) && valid(x-1, y-1) && valid(x-1, y+1))
  44.         return (a[x][y] + a[x+2][y] + a[x-1][y+1] + a[x-1][y-1]);
  45.     return -1;
  46. }
  47.  
  48. int inverted_Y(int x, int y){
  49.     if(valid(x-2,y) && valid(x+1, y-1) && valid(x+1, y+1))
  50.         return (a[x][y] + a[x-2][y] + a[x+1][y+1] + a[x+1][y-1]);
  51.     return -1;
  52. }
  53.  
  54. int dfs(int x,int y, int count){
  55.     if(count==0) return 0;
  56.  
  57.     visited[x][y]=true;
  58.     int ans=0;
  59.     for(int i=0; i<6; i++){
  60.         int m = x + dx[i];
  61.         int n = y + dy[i];
  62.  
  63.         if(valid(m,n) && visited[m][n]!=true){
  64.             visited[m][n]=true;
  65.             int temp= dfs(m,n,count -1);
  66.             visited[m][n]=false;
  67.  
  68.             ans=max(temp,ans);
  69.         }
  70.     }
  71.     visited[x][y]=false;
  72.     ans+=a[x][y];
  73.     return ans;
  74. }
  75.  
  76. int main() {
  77.     int t;
  78.     cin>>t;
  79.     while(t--){
  80.         cin>>n>>m;
  81.  
  82.         //taking input in hexagon style
  83.         for(int i = 0; i<(2*n); i+=2){
  84.             int k = i;
  85.             for(int j =0; j<m; j++){
  86.                 cin>>a[k][j];
  87.                 if(k == (1+i))k = i;
  88.                 else
  89.                 k = 1+i;
  90.             }
  91.         }
  92.  
  93.         int ans=-1;
  94.         for(int i=0; i<2*n; i++){
  95.             for(int j=0; j<m; j++){
  96.                 if(a[i][j]){
  97.                     int temp1 = dfs(i,j,4);
  98.                     int temp2 = inverted_Y(i,j);
  99.                     int temp3 = non_inverted_Y(i,j);
  100.                     int temp4 = max(temp2,temp3);
  101.                     ans = max (max (temp1, temp4), ans);
  102.                 }
  103.             }
  104.         }
  105.         cout<<ans<<endl;
  106.     }
  107.     return 0;
  108. }
  109.  
  110. /*
  111. test cases:-
  112. 6
  113. 3 4
  114. 150 450 100 320
  115. 120 130 160 110
  116. 10 60 200 220
  117. 1 4
  118. 10 20 30 40
  119. 3 5
  120. 300 410 150 55 370
  121. 120 185 440 190 450
  122. 165 70 95 420 50
  123. 5 5
  124. 356 55 41 453 12
  125. 401 506 274 506 379
  126. 360 281 421 311 489
  127. 425 74 276 371 164
  128. 138 528 461 477 470
  129. 3 13
  130. 197 51 443 274 47 552 160 96 501 102 469 318 308
  131. 516 128 506 471 381 418 328 517 380 78 569 58 90
  132. 113 238 179 444 541 27 444 62 264 93 245 353 37
  133. 11 7
  134. 292 182 586 607 259 190 239
  135. 511 716 425 367 511 462 714
  136. 593 713 231 60 118 442 82
  137. 626 577 579 682 136 176 681
  138. 240 23 410 193 230 729 109
  139. 453 231 287 383 444 578 409
  140. 729 401 408 330 213 574 54
  141. 684 224 75 62 660 472 227
  142. 606 37 473 487 222 185 476
  143. 84 477 158 94 141 484 122
  144. 616 333 302 626 29 99 674
  145.  
  146. output:-
  147. 1130
  148. 100
  149. 1500
  150. 1936
  151. 1982
  152. 2690
  153. */
Advertisement
Add Comment
Please, Sign In to add comment