YEZAELP

PROG-1064: Cromatie School

Jun 19th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int INF=1e9;
  4. int n,m;
  5. char ar[74][74];
  6. int keep[74][74],color[74][74];
  7. int dx[]={1,-1,0,0};
  8. int dy[]={0,0,1,-1};
  9. bool position(int i,int j){
  10.     return i>=1 and j>=1 and i<=n and j<=m;
  11. }
  12. bool found_T(int ui,int i,int uj,int j){
  13.     for(int x=i;x<=ui;x++){
  14.         for(int y=j;y<=uj;y++){
  15.             if(ar[x][y]=='T') return false;
  16.         }
  17.     }
  18.     return true;
  19. }
  20. int f(int ui,int uj){
  21.     int cnt=0,i=ui,j=uj;
  22.     while(found_T(ui,i,uj,j)){
  23.         cnt++;
  24.         i--;
  25.         j--;
  26.         if(!position(i,j)) break;
  27.     }
  28.     return cnt;
  29. }
  30. void dfs(int i,int j,int c){
  31.     if(color[i][j]!=0 or ar[i][j]!='P')
  32.         return ;
  33.     color[i][j]=c;
  34.     for(int d=0;d<4;d++){
  35.         int vi,vj;
  36.         vi=i+dx[d];
  37.         vj=j+dy[d];
  38.         if(position(vi,vj)) dfs(vi,vj,c);
  39.     }
  40. }
  41.  
  42. int main(){
  43.  
  44.     scanf("%d%d",&m,&n);
  45.     int mx=0;
  46.     for(int i=1;i<=n;i++){
  47.         for(int j=1;j<=m;j++){
  48.             scanf(" %c",&ar[i][j]);
  49.             if(ar[i][j]=='T') continue;
  50.             keep[i][j]=f(i,j);
  51.             mx = max(mx , keep[i][j] );
  52.         }
  53.     }
  54.     printf("%d ",mx*mx);
  55.  
  56.     int c=1;
  57.     for(int i=1;i<=n;i++){
  58.         for(int j=1;j<=m;j++){
  59.             if(color[i][j]==0 && ar[i][j]=='P') {
  60.                 dfs(i,j,c);
  61.                 c++;
  62.             }
  63.         }
  64.     }
  65.  
  66.     int mn=INF;
  67.     for(int i=1;i<=n;i++){
  68.         for(int j=1;j<=m;j++){
  69.             if(keep[i][j]==mx){
  70.                 map <int,int> cnt;
  71.                 for(int x=i;x>=i-mx+1;x--){
  72.                     for(int y=j;y>=j-mx+1;y--){
  73.                         if(ar[x][y]=='P') cnt[ color[x][y] ]++;
  74.                     }
  75.                 }
  76.                 int sz=cnt.size();
  77.                 mn=min(mn,sz);
  78.             }
  79.         }
  80.     }
  81.  
  82.     printf("%d ",mn);
  83.     return 0;
  84. }
Add Comment
Please, Sign In to add comment