UVA-4.2-正方形-201

来源:互联网 发布:35互联域名证书查询 编辑:程序博客网 时间:2024/05/21 11:37

UVA-4.2-正方形-201
题目描述:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=833&problem=137&mosmsg=Submission+received+with+ID+18630747
这题大体就是通过两种输入表示输入横线或者竖线,然后判断是否能组成正方形。
大体就是这样。
输出能组成多少个多少长度的正方形。
题目分析:
这题=。=弄俩数组,一个用来标记横线,一个用来标记竖线,然后用循环枚举查找就好了。
给出代码:

#include <stdio.h>#include <stdlib.h>#include <string.h>int as(int heng[][12],int lie[][12],int a,int n);int main(){     int hang[12][12];     int lie[12][12];     memset(hang,0,sizeof(hang));     memset(lie,0,sizeof(lie));     int count=0;     int n;     while(scanf("%d",&n)!=EOF)     {   count++;         char space;         space=getchar();         //char ch;         int N;         scanf("%d%*C",&N);         while(N--)         {   char ch;             ch=getchar();             int a,b;             scanf("%d%d%*c",&a,&b);             if(ch=='H')             {                 hang[a][b]=1;             }             else             {                 lie[b][a]=1;             }         }         if(count!=1)         {             printf("\n**********************************\n\n");         }         printf("Problem #%d\n\n",count);         int i,j;         int mark=1;         for(i=1;i<=n;i++)         {             int sum=as(hang,lie,i,n);             if(sum!=0)             {                    mark=0;                    printf("%d square (s) of size %d\n",sum,i);             }         }         if(mark)            printf("No completed squares can be found.\n");        memset(hang,0,sizeof(hang));        memset(lie,0,sizeof(lie));     }}int as(int hang[][12],int lie[][12],int a,int n){    int i,j,x,y;    int count=0;    for(i=1;i+a<=n;i++)    {        for(j=1;j+a<=n;j++)        {            int mark=1;            int x,y;            for(x=i;x<i+a;x++)            {                if(lie[x][j]!=1)                    mark=0;            }            for(y=j;y<j+a;y++)            {                if(hang[i][y]!=1)                    mark=0;            }            for(x=i;x<i+a;x++)            {                if(lie[x][j+a]!=1)                    mark=0;            }            for(y=j;y<j+a;y++)            {                if(hang[i+a][y]!=1)                    mark=0;            }            if(mark)                count++;        }    }    return count;}
0 0
原创粉丝点击