ACM (TJU) : 3006


// http://acm.tju.edu.cn/toj/showp3006.html

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

bool binary [405];
int a [23] [23];

void set (int row, int col)
{
    for ( int i = 0; i < 23; i++ ) {
        for ( int j = 0; j < 23; j++ )
            a [i] [j] = -2;
    }

    for ( int i = 0; i < 23; i++ )
        a [i] [0] = -1;

    for ( int i = 0; i < 23; i++ )
        a [0] [i] = -1;

    for ( int i = 1; i <= row + 1; i++ )
        a [i] [col + 1] = -1;

    for ( int i = 1; i <= col + 1; i++ )
        a [row + 1] [i] = -1;
}


void convert (char *str)
{
    for ( int i = 0; i < 405; i++ )
        binary [i] = false;

    int length = strlen (str);

    int index = 0;

    for ( int i = 0; i < length; i++ ) {
        if ( str [i] == ' ' ) {
            binary [index++] = false;
            binary [index++] = false;
            binary [index++] = false;
            binary [index++] = false;
            binary [index++] = false;
        }

        else {
            int code = str [i] - 64;

            binary [index + 4] = code % 2;
            code /= 2;

            binary [index + 3] = code % 2;
            code /= 2;

            binary [index + 2] = code % 2;
            code /= 2;

            binary [index + 1] = code % 2;
            code /= 2;

            binary [index] = code % 2;
            code /= 2;

            index += 5;
        }
    }
}

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

    int testCase = 0;

    while ( dataset-- ) {
        int row;
        int col;

        scanf ("%d %d", &row, &col);
        getchar ();

        char str [100];
        gets (str);

        convert (str);

        set (row, col);

        int count = row * col;
        int i = 1;
        int j = 1;
        int index = 0;

        while ( index < count ) {

            while ( a [i] [j] == -2 )
                a [i] [j++] = binary [index++];

            j--;
            i++;

            while ( a [i] [j] == -2 )
                a [i++] [j] = binary [index++];

            i--;
            j--;

            while ( a [i] [j] == -2 )
                a [i] [j--] = binary [index++];

            j++;
            i--;

            while ( a [i] [j] == -2 )
                a [i--] [j] = binary [index++];

            i++;
            j++;

        }

        printf ("%d ", ++testCase);

        for ( i = 1; i <= row; i++ ) {
            for ( int j = 1; j <= col; j++ )
                printf ("%d", a [i] [j]);
        }

        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