POJ 1473 There's Treasure Everywhere!

题目链接

小小的模拟一下。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 char str[1000];
 8 double a[10];
 9 double b[10];
10 void CL()
11 {
12     a[0] = a[2] = 0.0;
13     a[4] = a[5] = sqrt(2.0)/2.0;
14     a[1] = 1.0;
15     a[3] = -1.0;
16     a[6] = a[7] = -sqrt(2.0)/2.0;
17     b[0] = 1.0;
18     b[2] = -1.0;
19     b[4] = b[7] = sqrt(2.0)/2.0;
20     b[3] = b[1] = 0.0;
21     b[5] = b[6] = -sqrt(2.0)/2.0;
22 }
23 int judge(int x)
24 {
25     if(str[x+1] == ','||str[x+1] == '.')
26     {
27         if(str[x] == 'N')
28             return 0;
29         else if(str[x] == 'E')
30             return 1;
31         else if(str[x] == 'S')
32             return 2;
33         else if(str[x] == 'W')
34             return 3;
35     }
36     else
37     {
38         if(str[x] == 'N'&&str[x+1] == 'E')
39             return 4;
40         else if(str[x] == 'S'&&str[x+1] == 'E')
41             return 5;
42         else if(str[x] == 'S'&&str[x+1] == 'W')
43             return 6;
44         else if(str[x] == 'N'&&str[x+1] == 'W')
45             return 7;
46     }
47     return 0;
48 }
49 int main()
50 {
51     int len,i,cas = 1,pre,j,temp;
52     double x,y;
53     CL();
54     while(scanf("%s",str)!=EOF)
55     {
56         if(strcmp(str,"END") == 0) break;
57         len = strlen(str);
58         pre = 0;
59         x = y = 0;
60         for(i = 0; i < len; i ++)
61         {
62             if(str[i] == ','||str[i] == '.')
63             {
64                 temp = 0;
65                 for(j = pre; j < i; j ++)
66                 {
67                     if(str[j] <= '9'&&str[j] >= '0')
68                         temp = temp*10 + str[j] - '0';
69                     else
70                         break;
71                 }
72                 x += temp*a[judge(j)];
73                 y += temp*b[judge(j)];
74                 pre = i+1;
75             }
76         }
77         printf("Map #%d\n",cas++);
78         printf("The treasure is located at (%.3f,%.3f).\n",x,y);
79         printf("The distance to the treasure is %.3f.\n\n",sqrt(x*x+y*y));
80     }
81     return 0;
82 }

 

posted @ 2013-10-15 16:32  Naix_x  阅读(227)  评论(0编辑  收藏  举报