首页 > 代码库 > 洛谷OJ 1498 南蛮图腾 分治打印

洛谷OJ 1498 南蛮图腾 分治打印

https://www.luogu.org/problem/show?pid=1498

//给出cur
//一共2^cur层 起点为x,y
//分治print ,先对(x,y)的2^(cur-1)层赋值,在以第(x+2^(cur-1),Y)为起点对剩下2^(cur-1)层赋值

#include <bits/stdc++.h> 
using namespace std;
typedef long long ll;
const int N=5e3+20;
const double inf=1e7;
int n;
char s[N][N];
/*
       /      /__     /\  /    /__\/__   /\      /  /__\    /__ /\  /\  /\  //__\/__\/__\/__*/
void dfs(int cur,int x,int y)
{
    if(cur==1)//unit
    {
        s[x][y]=/;
        s[x][y+1]=\\;
        s[x+1][y-1]=/;
        s[x+1][y]=s[x+1][y+1]=_;
        s[x+1][y+2]=\\;
        return;
    }
    int s=1<<(cur-1);
    dfs(cur-1,x,y);//2^(cur-1)
    dfs(cur-1,x+s,y-s);//从x+s行开始 构造2^(cur-1)层 
    dfs(cur-1,x+s,y+s);
}
int main()
{
    while(cin>>n)
    {
        memset(s, ,sizeof(s));
        //输入为n 有2^n层,最后一行有2^(n+1)个点,第一个点坐标为(1,2^n)
        int y=1<<(n+1);
        dfs(n,1,y/2); 
    
        int x=1<<n;
        for(int i=1;i<=x;i++)
            s[i][y+i]=\0;
        
        for(int i=1;i<=x;i++)
            printf("%s\n",s[i]+1);
        printf("\n");
    }
    return 0;    
} 

 

洛谷OJ 1498 南蛮图腾 分治打印