Friday, November 12, 2010

ACM - UVA 118 - Mutant Flatworld Explorers

Problem: 118 - Mutant Flatworld Explorers
Solution: C++
Hints:http://www.algorithmist.com/index.php/UVa_118

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <utility>
#include <set>
#include <math.h>
using namespace std;

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;
}

      

No comments:

Post a Comment