I think 587 is an easy problem. But I got WA. Could anyone help me? The following is my source code.
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int main()
{
char map[300],*dir,*m;
double sq2,x,y;
int s,caseno=1;
sq2=sqrt(0.5);
while (scanf("%s",map) && strcmp(map,"END"))
{
x=y=0.0;
m=map;
while (*m)
{ s=0;
while (isdigit(*m)) s=10*s+(*m++ - '0');
for (dir=m;isalpha(*m);m++);
if (*m==',') *m++=0; else *m=0;
if (strcmp(dir,"N")==0) y+=s;
if (strcmp(dir,"S")==0) y-=s;
if (strcmp(dir,"E")==0) x+=s;
if (strcmp(dir,"W")==0) x-=s;
if (strcmp(dir,"NE")==0) {x+=sq2*s; y+=sq2*s;}
if (strcmp(dir,"NW")==0) {x-=sq2*s; y+=sq2*s;}
if (strcmp(dir,"SE")==0) {x+=sq2*s; y-=sq2*s;}
if (strcmp(dir,"SW")==0) {x-=sq2*s; y-=sq2*s;}
}
printf("Map #%dn",caseno++);
printf("The treasure is located at (%.3f,%.3f).n",x,y);
printf("The distance to the treasure is %.3f.nn",sqrt(x*x+y*y));
}
return 0;
}
