I♥TLE

Java、オンラインジャッジなど

0016-Treasure Hunt

問題(外部リンク)

Treasure Hunt | Aizu Online Judge

実装の概要

中途半端な角度の移動も有り得るため、移動後の座標の計算に三角関数を用いる必要があります。与えられる角度は今向いている向きからの相対的な角度なので、現在向いている角度も常に保持しておく必要があります。
なお、移動してから向きを変えるので注意が必要です。

public class Main {
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
        Hunter hunter = new Hunter();
        while(true){
            String[] tmpStr = br.readLine().split(",");
            int inputDir = Integer.parseInt(tmpStr[0]);
            int inputAngle = Integer.parseInt(tmpStr[1]);
             
            if(inputDir == 0&&inputAngle == 0){
                break;
            }
             
            hunter.move(inputDir, inputAngle);
        }
         
        System.out.println((int)hunter.x);
        System.out.println((int)hunter.y);
    }
 
}
 
class Hunter {
    double x = 0;
    double y = 0;
    int angle = 90;
     
    public void move(int distance, int angle){
        int newAngle = this.angle - angle;
        this.x += distance*Math.cos(Math.toRadians(this.angle));
        this.y += distance*Math.sin(Math.toRadians(this.angle));
        this.angle = newAngle;
         
    }
}