URAL 1970 J - 皇后像廣場 dfs

J - 皇后像廣場

题目连接:

http://acm.hust.edu.cn/vjudge/contest/123332#problem/J

Description

Vova was walking along the Statue Square (皇后像廣場) in Hong Kong, when he noticed that the square was paved with multi-colored square tiles. Vova took a careful look at the tiles and realized that they formed some picture. The tiles were large and he couldn't look at all of them at once. Probably one should look at the tile picture from above.
Vova decided to take a photo of a (10 × 10)-tile part of a picture, but he still couldn't cover them with one shot. Then Vova took nine photos, each of them covered a (4 × 4)-tile area of the picture (see the picture below). If we arrange the nine photos correctly, then we can restore the original 10 × 10 picture.
Problem illustration
Unfortunately, soon after printing out the photos Vova forgot not only the arranging order of them, but the correct rotation as well. He can rotate the image on the photo by an arbitrary angle multiple of 90 degrees. Help Vova use the nine photos to restore the original 10 × 10 pattern.

Input

The input contains the nine photos Vova made. The photos are described by 4 × 4 size matrices containing integers from 0 to 99, representing the colors of the corresponding tiles. The numbers on a line are separated by spaces. Each matrix is separated from the next one by an empty line.

Output

Print the original pattern as a 10 × 10 matrix. The matrix elements in the line should be separated by spaces. If there are multiple solutions, you may print any of them. It is guaranteed that at least one solution exists.

Sample Input

1 1 9 9
1 9 1 1
9 1 1 1
9 1 1 9

9 1 1 9
9 1 1 1
9 1 1 1
9 1 1 9

9 1 1 9
9 1 1 1
1 9 1 1
1 1 9 9

9 1 1 9
1 1 1 1
1 1 1 1
9 1 1 9

9 1 1 9
1 1 1 9
1 1 9 1
9 9 1 1

9 1 1 9
1 1 1 9
1 1 1 9
9 1 1 9

9 9 1 1
1 1 9 1
1 1 1 9
9 1 1 9

9 9 9 9
1 1 1 1
1 1 1 1
9 1 1 9

9 1 1 9
1 9 9 1
1 1 1 1
9 9 9 9

Sample Output

1 1 9 9 9 9 9 9 1 1
1 9 1 1 1 1 1 1 9 1
9 1 1 1 1 1 1 1 1 9
9 1 1 9 1 1 9 1 1 9
9 1 1 1 1 1 1 1 1 9
9 1 1 1 1 1 1 1 1 9
9 1 1 9 1 1 9 1 1 9
9 1 1 1 9 9 1 1 1 9
1 9 1 1 1 1 1 1 9 1
1 1 9 9 9 9 9 9 1 1

Hint

题意

给你9个44的矩阵,然后问你能不能用这9个矩阵拼成一个1010的大矩阵的图案。

可以旋转。

题解:

预处理旋转之后的样子,然后直接dfs就好了

不需要剪枝,我猜状态不会很多。。

代码

#include<bits/stdc++.h>
using namespace std;

struct node
{
    int a[4][4];
}p[9][4];
int flag = 0;
int ans[10][10];
int vis[10];
int xx[9]={0,0,0,3,3,3,6,6,6};
int yy[9]={0,3,6,0,3,6,0,3,6};
void dfs(int x){
    if(flag)return;
    if(x==9){
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                if(j==0)printf("%d",ans[i][j]);
                else printf(" %d",ans[i][j]);
            }
            printf("\n");
        }
        flag = 1;
        return;
    }

    for(int i=0;i<9;i++){
        if(vis[i])continue;
        for(int j=0;j<4;j++){
            int fff = 0;
            for(int t=0;t<4;t++){
                for(int k=0;k<4;k++){
                    if(ans[xx[x]+t][yy[x]+k]==-1)continue;
                    if(ans[xx[x]+t][yy[x]+k]!=p[i][j].a[t][k]){
                        fff = 1;
                        break;
                    }
                }
                if(fff)break;
            }
            if(fff==0){
                for(int t=0;t<4;t++){
                    for(int k=0;k<4;k++){
                        ans[xx[x]+t][yy[x]+k]=p[i][j].a[t][k];
                    }
                }
                vis[i]=1;
                dfs(x+1);
                if(flag)return;
                vis[i]=0;
                for(int t=0;t<4;t++){
                    if(x/3!=0&&t==0)continue;
                    for(int k=0;k<4;k++){
                        if(x%3!=0&&k==0)continue;
                            ans[xx[x]+t][yy[x]+k]=-1;
                    }
                }
            }
        }
    }
}
int main(){
    //freopen("1.in","r",stdin);
    memset(ans,-1,sizeof(ans));
    for(int i=0;i<9;i++){
        for(int j=0;j<4;j++)
            for(int k=0;k<4;k++)
                scanf("%d",&p[i][0].a[j][k]);
        for(int j=0;j<4;j++)
            for(int k=0;k<4;k++)
                p[i][1].a[j][k]=p[i][0].a[k][4-j-1];
        for(int j=0;j<4;j++)
            for(int k=0;k<4;k++)
                p[i][2].a[j][k]=p[i][1].a[k][4-j-1];
        for(int j=0;j<4;j++)
            for(int k=0;k<4;k++)
                p[i][3].a[j][k]=p[i][2].a[k][4-j-1];
    }
    dfs(0);
}
posted @ 2016-07-23 09:34  qscqesze  阅读(347)  评论(0编辑  收藏  举报