您的位置:首页 > 其它

Polyomino Composer(UVA12291)

2016-07-10 08:23 246 查看
Description

Polyomino Composer
A polyomino is a plane geometric figure formed by joining one or more equal squares edge to edge.
[align=RIGHT]- Wikipedia[/align]

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 ( 1

m

n

10) 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

1
0
0

思路:暴力枚举下即可;

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<string.h>
char ans[20][20];
char bns[20][20];
char ask[20][20];
char ck[20][20];
bool flag=0;
bool tie(int n,int m,int xx,int yy,int mxx,int myy)//先贴第一个
{
int i,j;
for(i=n; i<=xx; i++)
{
for(j=m; j<=yy; j++)
{
int p=i-n;
int q=j-m;
ck[i][j]=bns[mxx+p][myy+q];
}
}
}
bool check(int n,int m,int xx,int yy,int mxx,int myy,int t)//贴第二个并判断与要求的图形是否相同
{   int i,j;
for(i=n; i<=xx; i++)
{
for(j=n; j<=yy; j++)
{
int p=i-n;
int q=j-m;
if(ask[i][j]=='.'&&bns[mxx+p][myy+q]=='*')
{
ask[i][j]='*';
}
else if(ask[i][j]=='*'&&bns[mxx+p][myy+q]=='*')
{
return false;
}
}
}
for(i=0;i<t;i++)
{
for(j=0;j<t;j++)
{
if(ask[i][j]!=ans[i][j])
return false;
}
}
return true;
}

int main(void)
{
int i,j,k;
int n,m;
while(scanf("%d %d",&n,&m),n!=0&&m!=0)
{
flag=0;
memset(ask,0,sizeof(ask));
for(i=0; i<n; i++)
{
scanf("%s",ans[i]);
}
for(i=0; i<m; i++)
{
scanf("%s",bns[i]);
}
int x,y;
int x1=0;
int x2=m;
int y1=0;
int y2=m;
for(i=0; i<m; i++)
{
for(j=0; j<m; j++)
{
if(bns[i][j]=='*')
{
if(i>x1)
x1=i;
if(j>y1)
y1=j;
if(i<x2)
x2=i;
if(j<y2)
y2=j;
}
}
}
int xx2=x1-x2;
int yy2=y1-y2;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
for(int s=0; s<n; s++)
{
for(int uu=0; uu<n; uu++)
{
ck[s][uu]='.';
}
}
if(i+xx2>n-1||j+yy2>n-1)
continue;
else
{
tie(i,j,i+xx2,j+yy2,x2,y2);
for(x=0; x<n; x++)
{
for(y=0; y<n; y++)
{
for(int s=0; s<n; s++)
{
for(int uu=0; uu<n; uu++)
{
ask[s][uu]=ck[s][uu];
}
}
if(x+xx2>n-1||y+yy2>n-1)
continue;
else
{
flag=check(x,y,x+xx2,y+yy2,x2,y2,n);
if(flag)
{
break;
}
}
}if(flag)break;
}
}
if(flag)break;
}
if(flag)break;
}
if(flag)printf("1\n");
else printf("0\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: