UVALive - 4324 Ugly Windows

题意:就所给的图有几个完整的矩形,按字典序输出

思路:按字典序搜索,细节是:严格的一个矩形

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 110;

char arr[MAXN][MAXN];
int n,m;

int main(){
    while (scanf("%d%d",&n,&m) != EOF && n+m){
        for (int i = 0; i < n; i++)
            scanf("%s",arr[i]);
        for (int k = 'A'; k <= 'Z'; k++){
            int minx = 1000,maxx = -1;
            int miny = 1000,maxy = -1;
            for (int i = 0; i < n; i++){
                for (int j = 0; j < m; j++){
                    if (arr[i][j] == k){
                        minx = min(minx,i);
                        maxx = max(maxx,i);
                        miny = min(miny,j);
                        maxy = max(maxy,j);
                    }
                }
            }
            int ok = 1;
            for (int i = minx; i <= maxx; i++){
                for (int j = miny; j <= maxy; j++){
                    if ((i == minx || i == maxx || j == miny || j == maxy) && arr[i][j] != k){
                        ok = 0;
                        break;
                    }
                    else if (i != minx && i != maxx && j != miny && j != maxy && arr[i][j] != '.'){
                        ok = 0;
                        break;
                    }
                } 
                if (!ok)
                    break;
            }
            if (ok && minx+1 < maxx && miny+1 < maxy)
                printf("%c",k);
        }
        printf("\n");
    }
    return 0;
}



你可能感兴趣的:(UVALive - 4324 Ugly Windows)