salehin_076923's blog

By salehin_076923, history, 10 months ago, In English

While solving Skibidi Table problem, (problem link: https://mirror.codeforces.com/contest/2093/problem/D ) I was wondering How to Generate the Pattern->

Number of Rows: $$$2^n$$$

Number of Columns: $$$2^n$$$

Number of Cells: $$$2^{2n}$$$

$$$n=1:$$$

1  4

3  2

$$$n=2:$$$

1   4   13  16 

3   2   15  14 

9   12  5   8 

11  10  7   6

$$$n=3:$$$

1   4    13   16  49  52  61  64 

3   2    15   14  51  50  63  62 

9   12   5    8   57  60  53  56 

11  10   7    6   59  58  55  54 

33  36   45   48  17  20  29  32 

35  34   47   46  19  18  31  30 

41  44   37   40  25  28  21  24 

43  42   39   38  27  26  23  22

And so on......

An idea of implementing it by Recursion came to my mind Alhamdulillah!

As for each case, we are first entering the $$$1st$$$ cell, then the $$$4th$$$ cell, then the $$$3rd$$$ cell, then $$$2nd$$$ cell. Recursively calling it, we can store the pattern in an array & print it to get the pattern!

Code:

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

const int n=1000;
int arr[n][n];

void fillArray(int x,int y,int p,int val){
	if(p==1){
		arr[x][y]=val;
		return;
	}

	fillArray(x,y,p/2,val);
	fillArray(x+p/2,y+p/2,p/2,val+p*p/4);
	fillArray(x+p/2,y,p/2,val+2*p*p/4);
	fillArray(x,y+p/2,p/2,val+3*p*p/4);
}

int main(){
	int n;
	cin>>n;
	int p=pow(2,n);
	fillArray(0,0,p,1);
	for(int i=0;i<p;i++){
		for(int j=0;j<p;j++){
			cout<<arr[i][j]<<" ";
		}
		cout<<endl;
	}
}

You can check it by running the code!!!

  • Vote: I like it
  • -5
  • Vote: I do not like it