Uva_201 Squares 直接模拟

这个题直接模拟 每次连接 就分别用‘1’或者‘-’符号连接没有连接的部分就用符号’0’代替 很直观 可以在连接后输出整个图看看
最后直接循环找正方形 我是用count 记录连接的符号数量 如果是4的倍数 那么是正方形

#include<stdio.h>
#include<string.h>
int main()
{
    int n,m;
    int ddd=0;
    while(scanf("%d",&n)!=EOF)
    {  
        scanf("%d",&m);
        ddd++;
        char  num[n*2+1][n*2+1];
        int h[11];
        memset(num,'0',sizeof(num));
        memset(h,0,sizeof(h));
        int i,j;
        char ch;
        int a,b;
        getchar();
        for(i=0;i<m;i++)
        {
            scanf("%c %d %d",&ch,&a,&b);
            if(ch=='H')
            {
                num[a*2-1][b*2]='-';
            }
            if(ch=='V')
            {
                num[b*2][a*2-1]='1';
            }
            getchar();
        }
//        for(a=1;a<=n*2-1;a++)
//            for(b=1;b<=n*2-1;b++)
//        {
//            printf(" %c",num[a][b]);
//            if(b==n*2-1) printf("\n");
//        }
        int count;
        for(i=1;i<=n-1;i++)
        {
            for(a=1;a<=n-i;a++)
            {
                for(b=1;b<=n-i;b++)
                {
                    int x,y;
                    count=0;
                    for(x=a*2-1,y=b*2-1;x<=a*2-1+2*i;x++)
                        if(num[x][y]=='1') count++;
                    for(x=a*2-1+2*i,y=b*2-1;y<=b*2-1+2*i;y++)
                        if(num[x][y]=='-') count++;
                    for(x=a*2-1+2*i,y=b*2-1+2*i;x>=a*2-1;x--)
                        if(num[x][y]=='1') count++;
                    for(x=a*2-1,y=b*2-1+2*i;y>=b*2-1;y--)
                        if(num[x][y]=='-') count++;
                    if(count==4*i) h[i]++;
                   // printf("%d,",i);
                   // printf("%d. ",count);

                }
            }
        }
       // printf("%d\n",h[2]);
       if(ddd!=1) printf("\n**********************************\n\n");
       printf("Problem #%d\n\n",ddd);
       int r;
       int flag=0;
       for(r=1;r<=8;r++)
       {
           if(h[r]!=0)
           {
               printf("%d square (s) of size %d\n",h[r],r);
               flag=1;
           }
       }
       if(flag==0) printf("No completed squares can be found.\n");

    }
}
//Problem #1
//2 square (s) of size 1
//1 square (s) of size 2
//**********************************
//Problem #2
//No completed squares can be found.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值