UVa : 118


#include <stdio.h>

bool flag [52] [52];
int current_x;
int current_y;
char orientation;
bool destination;
int row;
int col;

void update (char x)
{
    if ( orientation == 'S' && x == 'R' )
        orientation = 'W';
    else if ( orientation == 'S' && x == 'L' )
        orientation = 'E';

    else if ( orientation == 'N' && x == 'R' )
        orientation = 'E';
    else if ( orientation == 'N' && x == 'L' )
        orientation = 'W';

    else if ( orientation == 'E' && x == 'R' )
        orientation = 'S';
    else if ( orientation == 'E' && x == 'L' )
        orientation = 'N';

    else if ( orientation == 'W' && x == 'R' )
        orientation = 'N';
    else if ( orientation == 'W' && x == 'L' )
        orientation = 'S';

    if ( x == 'F' ) {
        switch ( orientation ) {
            case 'N' :
            if ( row == current_y && flag [current_x] [current_y] == true )
                break;
            else if ( row == current_y && flag [current_x] [current_y] == false ) {
                flag [current_x] [current_y] = true;
                printf ("%d %d %c LOST\n", current_x, current_y, orientation);
                destination = true;
                break;
            }
            current_y++;
            break;

            case 'S' :
            if ( current_y == 0 && flag [current_x] [current_y] == true )
                break;
            else if ( current_y == 0 && flag [current_x] [current_y] == false ) {
                flag [current_x] [current_y] = true;
                printf ("%d %d %c LOST\n", current_x, current_y, orientation);
                destination = true;
                break;
            }
            current_y--;
            break;

            case 'E' :
            if ( col == current_x && flag [current_x] [current_y] == true )
                break;
            else if ( col == current_x && flag [current_x] [current_y] == false ) {
                flag [current_x] [current_y] = true;
                printf ("%d %d %c LOST\n", current_x, current_y, orientation);
                destination = true;
                break;
            }
            current_x++;
            break;

            case 'W' :
            if ( current_x == 0 && flag [current_x] [current_y] == true )
                break;
            else if ( current_x == 0 && flag [current_x] [current_y] == false ) {
                flag [current_x] [current_y] = true;
                printf ("%d %d %c LOST\n", current_x, current_y, orientation);
                destination = true;
                break;
            }
            current_x--;
        }
    }
}

int main ()
{
    for ( int i = 0; i < 52; i++ ) {
        for ( int j = 0; j < 52; j++ )
            flag [i] [j] = false;
    }

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

    while ( scanf ("%d %d %c", &current_x, &current_y, &orientation) != EOF ) {
        char command [105];
        scanf ("%s", command);

        destination = false;

        for ( int i = 0; command [i] && !destination; i++ )
            update (command [i]);

        if ( !destination )
            printf ("%d %d %c\n", current_x, current_y, orientation);
    }
    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