UVa : 587 / Tju : 1597


// @BEGIN_OF_SOURCE_CODE

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


int main ()
{
    char input [200 + 10];
    int cases = 0;

    while ( scanf ("%s", input) && strcmp (input, "END") ) {
        char *token;
        token = strtok (input, ",.");

        double x = 10e-6; // for precision error
        double y = 10e-6;

        while ( token != NULL ) {
            int number = atoi (token);
            double temp;

            if ( strstr (token, "NE" ) != NULL ) {
                temp = number * sqrt (2);
                x += temp / 2;
                y += temp / 2;
            }
            else if ( strstr (token, "SE" ) != NULL ) {
                temp = number * sqrt (2);
                x += temp / 2;
                y -= temp / 2;
            }
            else if ( strstr (token, "SW" ) != NULL ) {
                temp = number * sqrt (2);
                x -= temp / 2;
                y -= temp / 2;
            }
            else if ( strstr (token, "NW" ) != NULL ) {
                temp = number * sqrt (2);
                x -= temp / 2;
                y += temp / 2;
            }
            else if ( strstr (token, "N" ) != NULL ) {
                y += number;
            }
            else if ( strstr (token, "E" ) != NULL ) {
                x += number;
            }
            else if ( strstr (token, "S" ) != NULL ) {
                y -= number;
            }
            else {
                x -= number;
            }

            token = strtok (NULL, ",.");
        }

        printf ("Map #%d\n", ++cases);
        printf ("The treasure is located at (%.3lf,%.3lf).\n", x, y);
        printf ("The distance to the treasure is %.3lf.\n\n", sqrt(x * x + y* y));
    }

    return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

One thought on “UVa : 587 / Tju : 1597

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