Squares

Uva201


开始想图保存,后面打印节点就发现错了,所以只能保存

每个点的方向,方向只需要两个就行了,然后对每个有

向右的点枚举一次,统计个数就行了,每个点只与下面的

点匹配就行了。


#include <stdio.h>
#include <string.h>
const int maxn = 15;
int cnt[maxn];
int dx[2] = { 0, 1 }, dy[2] = { 1, 0 }; //只有向下和向右
struct node
{
    int dir[2];
} map[maxn][maxn];
int is_ok ( int x, int y, int n )
{
    for ( int i = x; i < x+n; i ++ )    //看是否边
        if ( map[i][y].dir[0] == 0 || map[i][y+n].dir[0] == 0 )
            return 0;
    for ( int i = y; i < y+n; i ++ )
        if ( map[x][i].dir[1] == 0 || map[x+n][i].dir[1] == 0 )
            return 0;
    return 1;
}
void search_square ( int x, int y )
{
    for ( int i = x; i < maxn; i ++ )
    {
        if ( map[i][y].dir[0] == 0 )
            break ;
        int size = i-x+1;   //边长
        //printf ( "**%d %d %d %d \n", i, y, size, is_ok ( x, y, size ) );
        if ( is_ok ( x, y, size ) )
            cnt[size] ++;   //统计此正方形边长的个数
    }
}
void print ( int n )
{
    for ( int i = 1; i <= n; i ++ )
    {
        for ( int j = 1; j <= n; j ++ )
        {
            printf ( "%d %d\n", i, j );
            printf ( "dir:%d %d\n", map[i][j].dir[0], map[i][j].dir[1] );
        }
    }
}
int main ( )
{
    int n, m, x, y, flag, cas = 0;
    char op[2];
    while ( ~ scanf ( "%d%d", &n, &m ) )
    {
        memset ( map, 0, sizeof ( map ) );
        memset ( cnt, 0, sizeof ( cnt ) );
        while ( m -- )
        {
            scanf ( "%s%d%d", op, &x, &y );
            if ( op[0] == 'H' )
                map[x][y].dir[1] = 1;
            else
                map[y][x].dir[0] = 1;   //注意这里是yx,看清题目,被坑在这里了
        }
        for ( int i = 0; i < maxn; i ++ )   //枚举每一点就好
        {
            for ( int j = 0; j < maxn; j ++ )
            {
                if ( map[i][j].dir[0] == 0 )
                    continue ;
                search_square ( i, j );
            }
        }
        //print ( n );
        flag = 0;
        if ( cas ++ )   //打印结果
            printf ( "\n**********************************\n\n" );
        printf ( "Problem #%d\n\n", cas );
        for ( int i = 0; i < maxn; i ++ )
        {
            if ( cnt[i] )
            {
                printf ( "%d square (s) of size %d\n", cnt[i], i );
                flag ++;
            }
        }
        if ( flag == 0 )
            printf ( "No completed squares can be found.\n" );
    }
    return 0;
}


你可能感兴趣的:(Squares)