暴力解。
1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 105 5 6 char map[MAXN][MAXN]; 7 char visit[27]; 8 int n, m; 9 10 bool check(char c) { 11 int i, j; 12 int x1=MAXN, y1=MAXN, x2=-1, y2=-1; 13 14 for (i=0; i<n; ++i) { 15 for (j=0; j<m; ++j) { 16 if (map[i][j] == c) { 17 if (i<x1) x1 = i; 18 if (j<y1) y1 = j; 19 if (i>x2) x2 = i; 20 if (j>y2) y2 = j; 21 } 22 } 23 } 24 if (x2-x1<2 || y2-y1<2) 25 return false; 26 for (i=x1; i<=x2; ++i) 27 if (map[i][y1]!=c || map[i][y2]!=c) 28 return false; 29 for (j=y1; j<=y2; ++j) 30 if (map[x1][j]!=c || map[x2][j]!=c) 31 return false; 32 for (i=x1+1; i<x2; ++i) 33 for (j=y1+1; j<y2; ++j) 34 if (map[i][j] != ‘.‘) 35 return false; 36 return true; 37 } 38 39 int main() { 40 int i; 41 int j; 42 43 while (scanf("%d %d", &n, &m)!=EOF && (n||m)) { 44 memset(visit, false, sizeof(visit)); 45 for (i=0; i<n; ++i) { 46 scanf("%s", map[i]); 47 for (j=0; j<m; ++j) { 48 if (map[i][j] != ‘.‘) { 49 visit[map[i][j]-‘A‘] = true; 50 } 51 } 52 } 53 for (i=0; i<26; ++i) { 54 if (visit[i] && check(i+‘A‘)) 55 printf("%c", i+‘A‘); 56 } 57 printf("\n"); 58 } 59 60 return 0; 61 }