uva 201 - Squares(自己的方法,同學有更好一點點的方法)

來源:互聯網
上載者:User

標籤:os   io   for   re   c   代碼   

#include<stdio.h>#include<string.h>#include<string>#include<iostream>#include<algorithm>using namespace std;int h[20][20];int v[20][20];int size_[20];int n,m;int flag;void judge(int x,int y)//這是關鍵區段的代碼,用來判讀那是否能構成square;{    int a1=n-x;    int a2=n-y;    int aa=min(a1,a2);    for(int i=0; i<aa; i++)    {        if(h[x][y+i]&&v[x+i][y])        {            int j;            for(j=0; j<=i; j++)            {                if(!(h[x+i+1][y+j])||!(v[x+j][y+i+1]))                {                    break;                }                if(j==i)                {                    flag=1;                    size_[i]++;                }            }        }        else            break;    }}int main(){    int kase=0;    while(scanf("%d",&n)!=EOF)    {        scanf("%d",&m);        kase++;        flag=0;        memset(size_,0,sizeof(size_));        memset(h,0,sizeof(h));        memset(v,0,sizeof(v));        int a1,a2;        string s;        for(int i=0; i<m; i++)        {            cin>>s>>a1>>a2;            if(s=="H")            {                h[a1][a2]=1;            }            if(s=="V")            {                v[a2][a1]=1;            }        }        for(int i=1; i<=n; i++)        {            for(int j=1; j<=n; j++)            {                if(h[i][j]&&v[i][j])                {                    judge(i,j);                }            }        }        if(kase!=1)        printf("\n**********************************\n");            printf("Problem #%d\n\n",kase);        if(flag==1)        {            for(int i=0; i<20; i++)            {                if(size_[i]!=0)                {                    printf("%d square (s) of size %d\n",size_[i],i+1);                }            }        }        else            printf("No completed squares can be found.\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.