G - There's Treasure Everywhere!解题报告(熊禾强)

来源:互联网 发布:阿里云优惠口令看不懂 编辑:程序博客网 时间:2024/04/27 18:04

G - There's Treasure Everywhere!
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 1473

Description

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. 


Input

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.

Output

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. 

Sample Input

3N,1E,1N,3E,2S,1W.10NW.END

Sample Output

Map #1The treasure is located at (3.000,2.000).The distance to the treasure is 3.606.Map #2The treasure is located at (-7.071,7.071).The distance to the treasure is 10.000.

题目大意:给出路径,求出终点坐标,和终点到起点的距离,其中路径有两部分组成,数字和字符,每个字符代表一个方向,数字代表在这个方向走的步数,每部长度为1

解题思路:模拟走法

代码:


#include<stdio.h>#include<string.h>#include<math.h>const double PI=3.14159265359;int main(){int map=0;char a[10005];while(scanf("%s",a)!=EOF){ if(a[0]=='E' && a[1]=='N' && a[2]=='D') break;int i;double x=0.0,y=0.0;//定义起点for(i=0;i<strlen(a);i++){double s=0,c=1;if(a[i+1]=='.')break;while(a[i]<=57 &&a[i]>=48)//计算步数{s=s*c+(a[i++]-48);c=10;}if(a[i]=='N'&&a[i+1]=='E')//判断方向{x+=s*cos(PI/4);y+=s*sin(PI/4);}else if(a[i]=='N'&&a[i+1]=='W'){x+=s*cos(3*PI/4);y+=s*sin(3*PI/4);}else if(a[i]=='S'&&a[i+1]=='E'){x+=s*cos((-1)*PI/4);y+=s*sin((-1)*PI/4);}else if(a[i]=='S'&&a[i+1]=='W'){x+=s*cos(5*PI/4);y+=s*sin(5*PI/4);}    else if(a[i]=='N')y+=s;else if(a[i]=='E')x+=s;else if(a[i]=='S')y-=s;else if(a[i]=='W')x-=s;}map++;printf("Map #%d\n",map);printf("The treasure is located at (%.3lf,%.3lf).\n",x+0.00001,y+0.00001);//加了那句+.000001才过的printf("The distance to the treasure is %.3lf.\n\n",sqrt(x*x+y*y));}return 0;}





原创粉丝点击