https://atcoder.jp/contests/abc244/tasks/abc244_b
位置と方向をセットにすると簡単です。
// Go Straight and Turn Right #![allow(non_snake_case)] use std::ops::Add; fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } #[derive(Clone, Copy)] struct Point { x: i32, y: i32, } impl Add for Point { type Output = Self; fn add(self, other: Self) -> Self { Self { x: self.x + other.x, y: self.y + other.y } } } impl Point { fn rotate(&self) -> Point { Point { x: self.y, y: -self.x } } } struct State { position: Point, direction: Point } impl State { fn init() -> State { State { position: Point {x: 0, y: 0}, direction: Point {x: 1, y: 0} } } fn straight(&self) -> State { State { position: self.position + self.direction, direction: self.direction } } fn rotate(&self) -> State { State { position: self.position, direction: self.direction.rotate() } } } fn main() { let _N: usize = read(); let T: String = read(); let mut s: State = State::init(); for c in T.chars() { match c { 'S' => s = s.straight(), 'R' => s = s.rotate(), _ => () } } println!("{} {}", s.position.x, s.position.y) }