Friday, April 28, 2017

Solution of Dev Skill DCP-237: What a problem!

See the problem:  DCP-237: What a problem!

/*  Bismillahir Rahmanir Rahim
    Its better to try yourself than follow others code.
    My solution Idea: 
    To store the array use one dimensional array 
    Now print according to their system. 
 */
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll i, j, n, k, cs=1, m, t, x, sz,  ar[1000006];

int main(){
     cin>>t;
     while(t--){
        cin>>n;
        k=1;
        for(i=1; i<=n; i++){
            for(j=1; j<=n; j++) cin>>ar[k++];
        } 
        cout<<"Case #"<<cs++<<": ";
        m=n+1;
        x=n*n-(n-1);
        if(n==1) cout<<ar[1];
        else{
            cout<<ar[x];
            for(i=1; ; i++){
                x++;
                cout<<" "<<ar[x];
                for(j=1; j<=i; j++){
                    x=x-m;
                    cout<<" "<<ar[x];
                }
                if(x==1||x==n*n) break;
                i++;
                x=x-n;
                cout<<" "<<ar[x];
                for(j=1; j<=i; j++){
                    x=x+m;
                    cout<<" "<<ar[x];
                }
                if(x==1||x==n*n) break;
            }
            if(x==1){
                for(i=2; ; i++){
                    x++;
                    cout<<" "<<ar[x];
                    if(x==n) break;
                    for(j=i; j<n; j++){
                        x=x+m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                    x=x-n;
                    cout<<" "<<ar[x];
                    if(x==n) break;
                    i++;
                    for(j=i; j<n; j++){
                        x=x-m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                }
            }
            else{
                for(i=2; i<n; i++){
                    if(x==n) break;
                    x=x-n;
                    cout<<" "<<ar[x];
                    for(j=i; j<n; j++){
                        x=x-m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                    x++;
                    cout<<" "<<ar[x];
                    if(x==n) break;
                    i++;
                    for(j=i; j<n; j++){
                        x=x+m;
                        cout<<" "<<ar[x];
                    }
                    if(x==n) break;
                }
            }
        }
        cout<<endl;
     }
    return 0;
}

No comments:

Post a Comment