博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj There's Treasure Everywhere!
阅读量:7033 次
发布时间:2019-06-28

本文共 4300 字,大约阅读时间需要 14 分钟。

hot3.png

There's Treasure Everywhere!

描述
    Finding buried treasures is simple: all you need is a map! The pirates in the Caribbean were famous for
     their enormous buried treasures and their elaborate maps. The maps usually read like ``Start at the lone
      palm tree. Take three steps towards the forest, then seventeen step towards the small spring, . . . blahblah
       . . . , finally six steps toward the giant rock. Dig right here, and you will find my treasure!'' Most of
       these directions just boil down to taking the mentioned number of steps in one of the eight principal compass
       directions (depicted in the left of the figure).
    Obviously, following the paths given by these maps may lead to an interesting tour of the local scenery, but if
     one is in a hurry, there is usually a much faster way: just march directly from your starting point to the place
      where the treasure is buried. Instead of taking three steps north, one step east, one step north, three steps
      east, two steps south and one step west (see figure), following the direct route (dashed line in figure) will
       result in a path of about 3.6 steps.
    You are to write a program that computes the location of and distance to a buried treasure, given a 'traditional' map.
输入
    The input contains several strings, each one on a line by itself, and each one consisting of at most 200 characters.
    The last string will be END, signaling the end of the input. All other strings describe one treasure map each, according
     to the following format:
    The description is a comma-separated list of pairs of lengths (positive integers less than 1000) and directions
    (N (north), NE (northeast), E (east), SE (southeast), S (south), SW (southwest), W (west) or NW (northwest)).
    For example, 3W means 3 steps to the west, and 17NE means 17 steps to the northeast. A full stop (.) terminates
    the description, which contains no blanks.
输出
    For every map description in the input, first print the number of the map, as shown in the sample output. Then
    print the absolute coordinates of the treasure, in the format "The treasure is located at (x,y).". The coordinate
    system is oriented such that the x-axis points east, and the y-axis points north. The path always starts at the origin (0,0).
    On the next line print the distance to that position from the point (0,0), in the format "The distance to the
    treasure is d.". The fractional values x, y, d must be printed exact to three digits to the right of the decimal point.
    Print a blank line after each test case.
样例输入
    3N,1E,1N,3E,2S,1W.
    10NW.
    END
样例输出
    Map #1
    The treasure is located at (3.000,2.000).
    The distance to the treasure is 3.606.
    Map #2
    The treasure is located at (-7.071,7.071).
    The distance to the treasure is 10.000.
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    int main()
    {
        int c = 0;
        while (true)
        {
            c++;
            double x = 0, y = 0;
            int times[8] = {0};
            char ord[205];
            int num[205];
            char dir[2];
            int numit = 0;
            int dirit = 0;
            scanf("%s", ord);
            if(ord[0] == 'E') break;
            for(int i = 0; i < strlen(ord); i++)
            {
                if('0' <= ord[i] && ord[i] <= '9') {num[numit] = ord[i] - '0'; numit++;}
                else if('A' <= ord[i] && ord[i] <= 'Z') {dir[dirit] = ord[i]; dirit++;}
                else
                {
                    int n = 0;
                    for(int j = numit - 1; j >= 0; j--)
                    n += num[j] * pow(10.0, numit - j - 1);
                    if(dirit == 1)
                    {
                        if(dir[0] == 'N') times[0] += n;
                        else if(dir[0] == 'E') times[2] += n;
                        else if(dir[0] == 'S') times[4] += n;
                        else if(dir[0] == 'W') times[6] += n;
                    }
                    else if(dirit == 2)
                    {
                        if(dir[0] == 'N' && dir[1] == 'E') times[1] += n;
                        else if(dir[0] == 'S' && dir[1] == 'E') times[3] += n;
                        else if(dir[0] == 'S' && dir[1] == 'W') times[5] += n;
                        else if(dir[0] == 'N' && dir[1] == 'W') times[7] += n;
                    }
                    numit = 0;
                    dirit = 0;
                }
            }
            //for(int i = 0; i < 8; i++)
            //printf("%d ", times[i]);
            for(int i = 0; i < 4; i++)
            times[i] -= times[i + 4];
            x = double(times[1]) * sqrt(2.0) / 2+ double(times[2]) + double(times[3]) * sqrt(2.0) / 2;
            y = double(times[0]) + double(times[1]) * sqrt(2.0) / 2 - double(times[3])  * sqrt(2.0) / 2;
            double dis = sqrt(x*x + y*y);
            if(x == -0.0) x = 0.0;
            if(y == -0.0) y = 0.0;
            if(dis == -0.0) dis = 0.0;
            printf("Map #%d\nThe treasure is located at (%.3f,%.3f).\n" ,c, x,y);
            printf("The distance to the treasure is %.3f.\n", dis);
            printf("\n");
        }
        return 0;
    }

转载于:https://my.oschina.net/locusxt/blog/132036

你可能感兴趣的文章
poj2386(简单dfs)
查看>>
双链表的基本操作
查看>>
走进异步编程的世界 - 剖析异步方法(上)
查看>>
[HAOI2006]受欢迎的牛
查看>>
docker-maven-plugin 完全免Dockerfile 文件
查看>>
day20 Python 装饰器
查看>>
限制性与非限制性定语从句区别
查看>>
fiddler工具的使用
查看>>
jquery源码分析(二)——架构设计
查看>>
javascript深入理解js闭包(转)
查看>>
207. Course Schedule
查看>>
如何优化您的 Android 应用 (Go 版)
查看>>
Trie树实现
查看>>
Opencv无法调用cvCaptureFromCAM无法打开电脑自带摄像头
查看>>
Exception异常处理机制
查看>>
复杂的web---web中B/S网络架构
查看>>
编写文档的时候各种问题
查看>>
Eclipse里maven的project报Unbound classpath variable: 'M2_REPO/**/***/***.jar
查看>>
新旅程CSS 基础篇分享一
查看>>
查看内核函数调用的调试方法【原创】
查看>>