Advertisement
jw910731

ZJ-d365

Jun 13th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #ifdef TEST
  3.     #define debug(...) printf(__VA_ARGS__)
  4. #else
  5.     #define debug(...) (void)0
  6. #endif
  7. #define EB emplace_back
  8. #define MP make_pair
  9. #define ST first
  10. #define ND second
  11. using namespace std;
  12. using ll = long long;
  13. using llu = long long unsigned;
  14. using pii = pair<int,int>;
  15. /************************/
  16. #define MAX 1000
  17. int dx[4]={0,0,1,-1}, dy[4]={1,-1,0,0};
  18. char mp[MAX+5][MAX+5];
  19. bool vis[MAX+5][MAX+5];
  20. int h,w;
  21. inline bool check(int x, int y){
  22.     return (x>=0) && (y >= 0) && (x < w) && (y < h);
  23. }
  24. int bfs(pii start){
  25.     queue<pii> q;
  26.     char targ = mp[start.ND][start.ST];
  27.     int size = 0;
  28.     if(!vis[start.ND][start.ST]){
  29.         q.emplace(start);
  30.         vis[start.ND][start.ST] = true;
  31.     }
  32.     while(!q.empty()){
  33.         pii now = q.front(); q.pop();
  34.         size++;
  35.         for(int i=0;i<4;i++){
  36.             pii fut = MP(now.ST+dx[i], now.ND+dy[i]);
  37.             if(check(fut.ST, fut.ND) && !vis[fut.ND][fut.ST] && mp[fut.ND][fut.ST] == targ){
  38.                 vis[fut.ND][fut.ST] = true;
  39.                 q.emplace(fut);
  40.             }
  41.         }
  42.     }
  43.     return size;
  44. }
  45. int main(){
  46.     int n;
  47.     scanf("%d",&n);
  48.     for(int e=1;e<=n;e++){
  49.         pii stat[26];
  50.         for(int i=0;i<26;i++){
  51.             stat[i].ND = i;
  52.         }
  53.         memset(mp,0x00,sizeof(mp));
  54.         memset(vis,0x00,sizeof(vis));
  55.         scanf("%d%d", &h, &w);
  56.         for(int i=0;i<h;i++){
  57.             scanf("%s",mp[i]);
  58.         }
  59.         for(int i=0;i<w;i++){
  60.             for(int j=0;j<h;j++){
  61.                 debug("%c", mp[j][i]);
  62.                 stat[ mp[j][i]-'a' ].ST += (bfs(MP(i,j)) > 0);
  63.             }
  64.             debug("\n");
  65.         }
  66.         debug("DEBUG\n");
  67.         for(int i=0;i<26;i++){
  68.             debug("%c : %d\n", 'a'+i, stat[i].ST);
  69.         }
  70.         debug("=====SEPERATE=====\n");
  71.         printf("World #%d\n", e);
  72.         sort(stat,stat+26,[](const pii &a, const pii &b){
  73.             return (a.ST == b.ST)? a.ND < b.ND : a.ST > b.ST;
  74.         });
  75.         for(int i=0;i<26;i++){
  76.             if(stat[i].ST > 0)
  77.                 printf("%c: %d\n", 'a'+stat[i].ND, stat[i].ST);
  78.         }
  79.     }
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement