Spiral Unwind


General Statement: Given a collection of letters that fill a square matrix, unwind in the given direction. The collection lists the rows of the matrix in order.

Input: The first line of the data set for this problem is an integer that represents the number of strings that follow. Each string is on a separate line. The first letter of the string is the unwinding direction (R = right, L=left).

Output: Output the list of unwound letters on a single line.
All letters are upper case.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: All letters are upper case.
The maximum number of letters is 25.
There may be duplicate letters within the matrix.

Discussion: A square matrix has the same number of rows and columns.
Choose the matrix size based on the number of letters in the data set.
Fill the matrix row by row working from left to right.
Begin unwinding the matrix at the top left corner.

Sample Input:
3
RABCDEFGHIJKLMNOP
LABCDEFGHIJKLMNOP
RQRABATSCA

Sample Output:
ABCDHLPONMIEFGKJ
AEIMNOPLHDCBFJKG
QRATACSBA

Solutions :

#include <stdio.h>
#include <string.h>
#include <math.h>

struct node {
    char ch;
    bool flag;
} a [7] [7];

int length;

void recursive_r (int x, int y)
{
    while ( a [x] [y].flag != true ) {
        printf ("%c", a [x] [y].ch);
        a [x] [y++].flag = true;
        length--;
    }

    x++;
    y--;

    while ( a [x] [y].flag != true ) {
        printf ("%c", a [x] [y].ch);
        a [x++] [y].flag = true;
        length--;
    }

    x--;
    y--;

    while ( a [x] [y].flag != true ) {
        printf ("%c", a [x] [y].ch);
        a [x] [y--].flag = true;
        length--;
    }

    x--;
    y++;

    while ( a [x] [y].flag != true ) {
        printf ("%c", a [x] [y].ch);
        a [x--] [y].flag = true;
        length--;
    }

    if ( length )
        recursive_r (++x, ++y);
}

void recursive_l (int x, int y)
{
    while ( a [x] [y].flag != true ) {
        printf ("%c", a [x] [y].ch);
        a [x++] [y].flag = true;
        length--;
    }

    x--;
    y++;

    while ( a [x] [y].flag != true ) {
        printf ("%c", a [x] [y].ch);
        a [x] [y++].flag = true;
        length--;
    }

    x--;
    y--;

    while ( a [x] [y].flag != true ) {
        printf ("%c", a [x] [y].ch);
        a [x--] [y].flag = true;
        length--;
    }

    x++;
    y--;

    while ( a [x] [y].flag != true ) {
        printf ("%c", a [x] [y].ch);
        a [x] [y--].flag = true;
        length--;
    }

    if ( length )
        recursive_l (++x, ++y);
}

int main ()
{
    int dataset;
    scanf ("%d", &dataset);
    getchar ();

    while ( dataset-- ) {

        for ( int i = 0; i < 7; i++ ) {
            for ( int j = 0; j < 7; j++ )
                a [i] [j].flag = true;
        }

        char input [28];
        gets (input);

        length = floor (sqrt (strlen (input)));
        int index = 1;

        for ( int i = 1; i <= length; i++ ) {
            for ( int j = 1; j <= length; j++ ) {
                a [i] [j].ch = input [index++];
                a [i] [j].flag = false;
            }
        }

        length = strlen (input) - 1;

        if ( input [0] == 'R' )
            recursive_r (1, 1);
        else
            recursive_l (1, 1);

        printf ("\n");
    }

    return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s