Topic link: http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3373&konwledgeId=40
Problem-solving ideas: You can see the relationship between side length and n, L=pow(3,n-1). When n=10, the size of the entire graph is as large as 3^18, which is a bit large for memory. So, we figured out a way to recursively push the case of each row.
We can divide a graph into 9 pieces.
block 1 | block 2 | block 3 |
block 4 | block 5 | block 6 |
block 7 | block 8 | block 9 |
According to the requirements of the title, blocks 2, 4, 5, 6, and 8 are all graphics with L'=pow(3,n-2), and the rest are spaces.
So we can get the recursion formula for each position. Let L=pow(3,n-1), L1=L/3, L2=2*L1;
块2: i<=L1, L1<j<=L2, pos(i,j,n) = pow(i,j-L1,n-1)
块4: L1<i<=L2, j<=L1, pos(i,j,n)=pow(i-L1,j,n-1)
Blocks 5, 6, 8 are similar.
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int MAXN=100005; 6 const LL MON7 = 1e9+7; 7 8 char deduce(int i,int j, int n) 9 { 10 if (n==1 && i==1 && j==1) return 'o'; 11 int L=pow(3,n-1); 12 int L1=L/3; 13 int L2=L1*2; 14 if (i<=L1) 15 { 16 if (j<=L1 || j > L2) return ' '; 17 return deduce(i,j-L1,n-1); 18 } 19 if (i<=L2) 20 { 21 if (j<=L1) return deduce(i-L1,j,n-1); 22 if (j<=L2) return deduce(i-L1,j-L1,n-1); 23 return deduce(i-L1,j-L2,n-1); 24 } 25 if (j<=L1 || j>L2) return ' '; 26 return deduce(i-L2,j-L1,n-1); 27 } 28 29 char s[MAXN]; 30 int n; 31 32 void solve() 33 { 34 int L=pow(3,n-1); 35 for (int i=1;i<=L;++i) 36 { 37 for (int j=1;j<=L;++j) 38 { 39 s[j]=deduce(i,j,n); 40 } 41 s[L+1]='\0'; 42 printf("%s\n",s+1); 43 } 44 } 45 46 int main() 47 { 48 #ifndef ONLINE_JUDGE 49 freopen("test.txt","r",stdin); 50 #endif // ONLINE_JUDGE 51 int Case; 52 int tCase=0; 53 scanf("%d",&Case); 54 while (Case--) 55 { 56 scanf("%d",&n); 57 printf("Case #%d:\n", ++tCase); 58 solve(); 59 } 60 return 0; 61 }