contest 0820 graph [找规律]

contest 0820 graph [找规律]

正难则反

当时在这道题上面卡了好久,结果最后倒着来就秒过了…

一个小的正方形经过系列旋转得到了大正方形,那么一个格子的黑白状态是一定确定了的。

如果正着来找,我们会得到很多没有用的格子的信息;但是倒着来找(一个格子一定是由上一张图的某一个格子经过旋转得到的)就可以精确地找出该格子的黑白状态。

所以这道题倒着DFS就可以了。

代码

#include<iostream>
#include<cstdio>
using namespace std;
int Get(int x,int y,int n){
    int nn=(1<<n)/2;
    if(x>nn)return 1;
    if(y<=nn)return 3;
    return 2;
}
int DFS(int x,int y,int n){
    if(n==1){
        if(x==1 && y==2)return 1;
        return 0;
    }
    int g=Get(x,y,n),t=1<<(n-1);
    if(x>t)x-=t;if(y>t)y-=t;
    if(g==1)return DFS(x,y,n-1);
    if(g==3)return DFS(y,t-x+1,n-1);
    return DFS(t-y+1,x,n-1);
}
int main(){
    freopen("graph.in","r",stdin);
    freopen("graph.out","w",stdout);
    int n,a,b,c,d;
    scanf("%d%d%d%d%d",&n,&a,&b,&c,&d);
    for(int i=a;i<=c;i++){
        for(int j=b;j<=d;j++)
            printf("%d",DFS(i,j,n));
        printf("\n");
    }
}

猜你喜欢

转载自blog.csdn.net/ArliaStark/article/details/81903936