Pattern-2 Problem of Eagle Coding Contest
The main logic is that we have to right-down-left-up and this will be bounded by the counter variable.
The base condition can also be seen as... when the size of the pattern is odd, for ex 3 then we will be going right just 2 times that is size-1 times. For size 5 will be going right 4 times. And for even sizes like, 2 will go left 1 time( means when condCheck will reach 2 then will return from function after printing) and for size 4 condCheck iter will go from 1 to 3( and then just print and return ).
#include<bits/stdc++.h>
using namespace std;
void disTwoDVec(vector<vector<int>> vect){
for(auto i:vect){
for(auto j:i){
cout<<j<<" ";
}
cout<<"\n";
}
}
// Main Function for the Pattern
void genPattern(int num){
// These below initialisations will be same for all cases except 1, which is
// being handled in the first if condition
vector<int> sRight={0,0},sDown={1,num-1},sLeft={num-1,num-2},sUp={num-2,0};
vector<int> eRight={0,num-1},eDown={num-1,num-1},eLeft={num-1,0},eUp={1,0};
// For secondary condition to return from func
int condition=num-1,condCheck=1;
// cout<<"Are we here\n";
if(num==1){
cout<<1<<"\n";
return;
}
// Counter will be printing this everytime and then updating it
int counter=1;
// This will be the final value of counter, endRes= End Result
int endRes=num*num;
// Firstly initialising the array
vector<vector<int>> ans;
for(int i=0;i<num;i++){
vector<int> temp(num,0);
ans.push_back(temp);
}
// Ignore the cout statements, were just for Debugging
// disTwoDVec(ans);
while(counter<=endRes){
// cout<<"Yes, I am here.... :)\n";
int it1=sRight[0],it2=sRight[1];
// firstly right
while(it1<=eRight[0] && it2<=eRight[1]){
// cout<<counter;
ans[it1][it2++]=counter++;
}
/* Left for even and right for odd */
if(num%2!=0){
condCheck++;
// cout<<"condCheck became "<<condCheck<<endl;
if(condCheck>condition){
disTwoDVec(ans);
return;
}
}
// cout<<"\n";
// Updation for next right movement for the next time,
// Carefully see the boundation of the right movement's starting and ending
// Indices
sRight[0]+=1;
sRight[1]+=1;
eRight[0]+=1;
eRight[1]-=1;
if(counter>endRes){
break;
}
// Everytime getting the it1 and it2 to the desired positions
it1=sDown[0];
it2=sDown[1];
// now down
while(it1<=eDown[0] && it2<=eDown[1]){
// cout<<counter;
ans[it1++][it2]=counter++;
}
// cout<<"\n";
// Check this everytime after making a movement
if(counter>endRes){
break;
}
// Updation
sDown[0]+=1;
sDown[1]-=1;
eDown[0]-=1;
eDown[1]-=1;
it1=sLeft[0];
it2=sLeft[1];
// Now left
while(it1<=eLeft[0] && it2>=eLeft[1]){
// cout<<counter;
ans[it1][it2--]=counter++;
}
if(num%2==0){
condCheck++;
if(condCheck>condition){
disTwoDVec(ans);
return;
}
}
// cout<<"\n";
if(counter>endRes){
break;
}
// Updation
sLeft[0]-=1;
sLeft[1]-=1;
eLeft[0]-=1;
eLeft[1]+=1;
it1=sUp[0];
it2=sUp[1];
// Going Up now
while(it1>=eUp[0] && it2<=eUp[1]){
// cout<<counter;
ans[it1--][it2]=counter++;
}
// cout<<"\n";
if(counter>endRes){
break;
}
// Updation
sUp[0]-=1;
sUp[1]+=1;
eUp[0]+=1;
eUp[1]+=1;
}
disTwoDVec(ans);
}
int main(){
cout<<"Hola? Todo el Mundo??\n";
int size;
cout<<"Size? ";
cin>>size;
cout<<endl;
genPattern(size);
return 0;
}
Comments
Post a Comment