#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int row, col;
char grid[1003][1003];
void dfs(int i, int j, char src){
if(i < 0 || i >= row || j < 0 || j >= col)return;
if(grid[i][j] != src)return;
if(grid[i][j] == '.')return;
if(grid[i][j] >= 'a' && grid[i][j] <= 'z')grid[i][j] = '.';
dfs(i, j+1, src);
dfs(i+1, j, src);
dfs(i, j-1, src);
dfs(i-1, j, src);
}
int main(){
int cnt[27], t, cs = 0;
cin >> t;
while(t--){
cin >> row >> col;
memset(cnt, 0, sizeof(cnt));
getchar();
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
cin >> grid[i][j];
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++){
if(grid[i][j] >= 'a' && grid[i][j] <= 'z'){
char src = grid[i][j];
cnt[grid[i][j]-97]++;
dfs(i, j, src);
}
}
cout << "World #" << ++cs << endl;
int mx = -1, mp;
for(int i = 0; i < 27 ; i++){
mx = -1;
for(int j = 0; j < 27; j++)
if(cnt[j] > mx)mx = cnt[j], mp = j;
cnt[mp] = 0;
if(mx)printf("%c: %d\n", mp+97, mx);
if(mx == 0)break;
}
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int row, col;
char grid[1003][1003];
void dfs(int i, int j, char src){
if(i < 0 || i >= row || j < 0 || j >= col)return;
if(grid[i][j] != src)return;
if(grid[i][j] == '.')return;
if(grid[i][j] >= 'a' && grid[i][j] <= 'z')grid[i][j] = '.';
dfs(i, j+1, src);
dfs(i+1, j, src);
dfs(i, j-1, src);
dfs(i-1, j, src);
}
int main(){
int cnt[27], t, cs = 0;
cin >> t;
while(t--){
cin >> row >> col;
memset(cnt, 0, sizeof(cnt));
getchar();
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
cin >> grid[i][j];
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++){
if(grid[i][j] >= 'a' && grid[i][j] <= 'z'){
char src = grid[i][j];
cnt[grid[i][j]-97]++;
dfs(i, j, src);
}
}
cout << "World #" << ++cs << endl;
int mx = -1, mp;
for(int i = 0; i < 27 ; i++){
mx = -1;
for(int j = 0; j < 27; j++)
if(cnt[j] > mx)mx = cnt[j], mp = j;
cnt[mp] = 0;
if(mx)printf("%c: %d\n", mp+97, mx);
if(mx == 0)break;
}
}
return 0;
}
No comments:
Post a Comment