UVA 12291 Polyomino Composer

來源:互聯網
上載者:User

Description


  Polyomino Composer 


A polyomino is a plane geometric figure formed by joining one or more equal squares edge to edge.

- Wikipedia

Given a large polyomino and a small polyomino, your task is to determine whether you can compose the large one with two copies of the small one. The polyominoes can be translated, but not flipped or rotated.
The two pieces should not overlap. The leftmost picture below is a correct way of composing the large polyomino, but the right two pictures are not. In the middle picture, one of the pieces was rotated. In the rightmost picture, both pieces are exactly identical,
but they're both rotated from the original piece (shown in the lower-right part of the picture).

Input 

There will be at most 20 test cases. Each test case begins with two integers n and m ( 1mn10)
in a single line. The next n lines describe the large polyomino. Each of these lines contains exactly n characters in `*',`.'. A `*' indicates an existing square,
and a `.' indicates an empty square. The next m lines describe the small polyomino, in the same format. These characters are guaranteed to form valid polyominoes (note that a polyomino contains at least one existing
square). The input terminates with n = m = 0, which should not be processed.

Output 

For each case, print `1' if the corresponding composing is possible, print `0' otherwise.

Sample Input 

4 3.**.****.**.....**..**...3 3****.*****..*..**.4 2****............*.*.0 0

Sample Output 

100

The Seventh Hunan Collegiate Programming Contest 
Problemsetter: Rujia Liu, Special Thanks: Yiming Li & Jane Alam Jan

這個題目的意思就是給你兩圖形,問你用兩張第二種圖能否組成第一張第一種張圖,方向不能改變。

看到這個題,我首先想到的就是枚舉(n-height)*(n-width)個點,因為m和n都很小,就枚舉法了,由於我水平很水,調了好久好久。

#include<iostream>#include<cstring>#include<cstdio>using namespace std;int n,m;bool cover(char a[12][12],int px,int py,char b[12][12],int sx,int sy){    //功能:把子圖b從(sx,sy)到(m,m)中的‘*’貼到a圖中。    for(int i=sx,j=px;i<m;i++,j++)    {        for(int k=sy,l=py;k<m;k++,l++)        {            //如果能賦值就賦值            if(b[i][k]=='*'&&a[j][l]=='.')                a[j][l]=b[i][k];            else if(b[i][k]=='*'&&a[j][l]=='*')                return false;            //如果不能賦值則返回cover失敗。        }    }    return true;}void init(char a[12][12]){//把圖初始化為全是‘.’的圖。    for(int i=0; i<n; i++)    {        for(int j=0; j<n; j++)        {            a[i][j]='.';        }        a[i][n]='\0';    }}void copy(char a[12][12],char b[12][12]){//如命名    for(int i=0; i<n; i++)        strcpy(a[i],b[i]);}bool issame(const char a[12][12],const char b[12][12]){//判斷兩圖是否相同。    for(int i=0; i<n; i++)        if(strcmp(a[i],b[i])!=0)            return false;    return true;}int main(){    //freopen("in.txt","r",stdin);    char big[12][12],part[12][12];    char blank[12][12],tmp[12][12];    int minx,miny,maxx,maxy;    bool flag;    while(scanf("%d%d",&n,&m),m||n)    {        minx=m,miny=m,flag=0;        maxx=0,maxy=0;        for(int i=0; i<n; i++)            scanf("%s",big[i]);        for(int j=0; j<m; j++)            scanf("%s",part[j]);        for(int i=0; i<m; i++)            for(int j=0; j<m; j++)            {                if(part[i][j]=='*')                {                    if(minx>i)minx=i;if(miny>j)miny=j;                    if(maxx<i)maxx=i;if(maxy<j)maxy=j;                }            }        int height=maxx-minx,width=maxy-miny;        for(int i=0; i<n-height; i++)        {            for(int j=0; j<n-width; j++)            {                init(blank);                cover(blank,i,j,part,minx,miny);                for(int l=0; l<n-height; l++)                {                    for(int k=0; k<n-width; k++)                    {                        copy(tmp,blank);                        if(tmp[l][k]=='.'&&cover(tmp,l,k,part,minx,miny))                        {                            if(issame(big,tmp))                            {                                flag=1;                                i=100,j=100,l=100,k=100;                                //這裡給i,j,l,k賦值是用來退出多重迴圈用的。                            }                        }                    }                }            }        }        printf("%d\n",flag);    }    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.