201 - Squares (UVA)

题目链接如下:

Online Judge

我的代码如下(网上一堆人说这道题简单,我也写了挺久的....):

#include 
// #define debug
const int maxN = 10;

struct point{
    int horizontal = 0;
    int vertical = 0;
};

char str[2];
point a[maxN][maxN];
int n, m, kase, u, v, sz, cnt;
bool flag;

bool judge(int size, int s, int t){
    for(int i = 0; i < size; ++i){
        if(!a[s][t + i].horizontal || !a[s + size][t + i].horizontal || !a[s + i][t].vertical || !a[s + i][t + size].vertical){
            return false;
        }
    }
    return true;
}

int main(){
    #ifdef debug
        freopen("0.txt","r",stdin);
        freopen("1.txt","w",stdout);
    #endif
    kase = 0;
    while(scanf("%d %d", &n, &m) == 2){
        if(kase){
            printf("\n**********************************\n\n");
        }
        printf("Problem #%d\n\n", ++kase);
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                a[i][j].horizontal = 0;
                a[i][j].vertical = 0;
            }
        }
        for(int i = 0; i < m; ++i){
            scanf("%s %d %d", str, &u, &v);
            if(str[0] == 'H'){
                a[u][v].horizontal = 1;
            } else{
                a[v][u].vertical = 1;
            }
        }
        flag = false;
        for(sz = 1; sz < n; ++sz){
            cnt = 0;
            for(int i = 1; i <= n - sz; ++i){
                for(int j = 1; j <= n - sz; ++j){
                    if(judge(sz, i, j)){
                        ++cnt;
                    }
                }
            }
            if(cnt){
                flag = true;
                printf("%d square (s) of size %d\n", cnt, sz);
            }
        }
        if(!flag){
            printf("No completed squares can be found.\n");
        }
    }
    #ifdef debug
        fclose(stdin);
        fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,算法)