寝癖頭の解法

学習中の覚え書きを投稿、更新していきます。

AtCoder Problems in C++ #B - Go Straight and Turn Right

AtCoder Beginner Contestの過去問から、その提出コードの解答例です。
AtCoderとは、コンテストを通じて、プログラミングやアルゴリズムを学習するサービスです。
atcoder.jp
プログラミングコンテストとは、「与えられた問題をいかに素早く、正確に」解くことができるかを競うものです。
競技プログラミング」を略して、「競プロ」などと呼ばれています。

#B - Go Straight and Turn Right

atcoder.jp

僕が作成、提出したコードは、以下のとおりです。

/*
AtCoder Problems in C++
#B - Go Straight and Turn Right
https://atcoder.jp/contests/abc244/tasks/abc244_b
提出コードの解答例
https://neguse-atama.hatenablog.com
*/
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll x=0,y=0;
char c='e';
void f1(void){
  if(c=='e'){
    x++;
  }else if(c=='w'){
    x--;
  }else if(c=='n'){
    y++;
  }else{
    y--;
  }
}
void f2(void){
  if(c=='e'){
    c='s';
  }else if(c=='w'){
    c='n';
  }else if(c=='n'){
    c='e';
  }else{
    c='w';
  }
}
int main(void){
  ll n;
  string t;
  cin>>n>>t;
  for(ll i=0;i<n;i++){
    if(t[i]=='S'){
      f1();
    }else{
      f2();
    }
  }
  cout<<x<<" "<<y<<endl;
  return 0;
}

AtCoder Beginner Contestは、オンラインジャッジによるプログラミングコンテストです。
日本語と英語に対応していて、週末ごとに実施されているみたいです。
https://practice.contest.atcoder.jp/tutorial
アカウントを登録すれば、誰でも参加できます。