There's Treasure Everywhere! poj1473题目解析

题目:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

总时间限制: 
1000ms

内存限制: 
65536kB
描述
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.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

题目不需要设计算法,关键在于怎么控制输入以及如何计算方向。

可以先将一行数据输入到一个字符数组或者string对象里面,然后逐个字符解释。

计算方向的时候有“N”与“NW”的区分,要考虑两个字符。用if或者switch的话罗列的情况比较多,这种问题一般先开个相关的数组,再利用索引统一计算。

 1 #include <iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 using namespace std;
 5 int main()
 6 {
 7     string str;
 8     int st[90][2]={0},c=1;                  //st数组索引x与y增长的方向
 9     double g=sqrt(2.0)/2;
10     st[‘N‘][1]=st[‘E‘][0]=1;
11     st[‘S‘][1]=st[‘W‘][0]=-1;
12     while(cin>>str)
13     {
14         if(str=="END")
15             break;
16         int num=0,len=str.length()-1;
17         double x=0,y=0,r=0;
18         for(int i=0;i<len;i++)
19         {
20             if(str[i]>=‘0‘&&str[i]<=‘9‘)
21                 num=num*10+str[i]-‘0‘;
22             if(str[i]>‘A‘&&str[i]<‘Z‘)
23             {
24                 if(str[i+1]>‘A‘&&str[i+1]<‘Z‘)
25                 {
26                     x+=num*g*(st[str[i]][0]+st[str[i+1]][0]);
27                     y+=num*g*(st[str[i]][1]+st[str[i+1]][1]);
28                 }
29                 else
30                 {
31                     x+=num*st[str[i]][0];
32                     y+=num*st[str[i]][1];
33                 }
34                 num=0;                                                 //num在这置零
35             }
36         }
37         r=sqrt(x*x+y*y);
38         cout<<"Map #"<<c<<endl;
39         cout<<"The treasure is located at ("<<fixed<<setprecision(3)<<x<<","<<y<<")."<<endl;
40         cout<<"The distance to the treasure is "<<r<<"."<<endl<<endl;
41         c++;
42     }
43     return 0;
44 }

There's Treasure Everywhere! poj1473题目解析

时间: 2024-10-10 11:10:54

There's Treasure Everywhere! poj1473题目解析的相关文章

增量备份,11g052题目解析

增量备份分为差异备份(differential incremental backup)和累积备份(cumulative incremental backup),这是两种执行增量备份操作的不同方法. 80. You perform differential incremental level 1 backups of your database on each working day and level 0 backup on Sundays, to tape. Which two stateme

部分单调队列优化 DP 题目解析

这里专门放一些单调队列优化 DP 的题目,并加上简要解析. Luogu P1725 琪露诺 易得转移方程为 $$f_i=\max_{j\,=\,\max(i-R,\;0)}^{i-L}f{_ j}+a_i\;(L \le i \le n)$$ 那么,其中 $\max$ 部分可以看成一段区间的最大值,用单调队列维护. 然后答案是 $$\max_{i\,=\,n-r+1}^{n} f_i$$ 时间复杂度 $O(n)$. 部分单调队列优化 DP 题目解析 原文地址:https://www.cnblog

第七届蓝桥杯C/C++B组省赛题目解析

题目1:煤球数目 有一堆煤球,堆成三角棱锥形.具体:第一层放1个,第二层3个(排列成三角形),第三层6个(排列成三角形),第四层10个(排列成三角形),....如果一共有100层,共有多少个煤球?请填表示煤球总数目的数字.注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字. 解析: 第一层:1个. 第二层:2*3-3=3个. 第三层:3*3-3=6个. 第四层:4*3-3=10个. 代码如下: int s=0,a=0; for(int i=1;i<=100;i++){ a=a+i;

英雄会题目解析- 第五届在线编程大赛月赛第三题:石子游戏

题目: 甲乙两人面对若干堆石子,其中每一堆石子的数目可以任意确定.两人轮流按下列规则取走一些石子,游戏的规则如下:1.每一步应取走至少一枚石子:2.每一步只能从某一堆中取走部分或全部石子:3.如果谁无法按规则取子,谁就是输家.如果甲乙两人都采取最优的策略,甲先拿,请问,是甲必胜还是乙必胜.输入格式:多组数据,每组数据两行,第一行是一个整数N, 2<=N<=10000下一行是N个正整数,代表每堆的石子数,石子数在32位整数内.输出格式:每组测试数据输出一行,如果甲存在必胜策略,输出"W

Sun考试认证题目解析(强力推荐,巩固基础)

转载请注明出处:http://www.ming-yue.cn/java-basic/. 1,给定以下代码,求j的值. public class Test { public static void main(String[] args) throws Exception { int i = 0xFFFFFFF1; int j = ~i; } } A. 0 B. 1 C. 14 D. –15 E. An error at line 3 causes compilation to fail. F. A

JS经典题目解析

此次列举出一些觉得有意思的JS题目(来源于出了名的44题),相信有非常多关于这些题目的博客,写这篇博客的目的在于巩固一些知识点,希望能和读者共同进步. 1. map函数执行过程 ["1", "2", "3"].map(parseInt) 答案: [1, NaN, NaN] 解析: map(function callback(current, index, array)), map 回掉提供三个参数,current value, index of

re 模块, 正则表达式 \w+\d+ 的重复问题引发的题目解析

题目 计算以下代码的结果 s = "?!.18)dajslj$12.15613sdadw.123sdasda35615.168sndsda$15.6sdasd.sdfsdgw123.156s" p1 = re.compile("\w+\.\w+\d+\.+\d+") print(p1.findall(s)) p1 = re.compile("(\w+)\.(\w+)((\d+)\.+\d+)") print(p1.findall(s)) 答案 #

四则运算小题目解析

小题目四则运算下面是源代码 #include <iostream> using namespace std; void main() {  int X0[1000],Y0[1000],Z0[1000],X1[1000],Y1[1000],Z1[1000],X2[1000],Y2[1000],Z2[1000],X3[1000],Y3[1000],Z3[1000],A[1000],a,b,c;  int i,j,A0,A1,A2,A3,B0,B1,B2,B3;  int number1,numbe

郑州商品交易所笔试题目解析

记录下这次郑商所笔试遇到的题目: 1.多态 一道选择题,一道填空题,要求实现编译时的多态,具体知识可以参考http://blog.csdn.net/hackbuteer1/article/details/7475622 C++支持两种多态性:       编译时多态:程序运行前发生的事件 —— 函数重载.运算符重载 ——静态绑定                运行时多态:程序运行时发生的事件 —— 虚函数机制           ——动态绑定       多态性是面向对象程序设计的重要特征之一.