码迷,mamicode.com
首页 > 其他好文 > 详细

There's Treasure Everywhere! poj1473题目解析

时间:2015-06-24 23:53:53      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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

总时间限制: 
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题目解析

标签:

原文地址:http://www.cnblogs.com/asingingbird/p/4598791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!