寝癖頭の解法

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

AtCoder Problems in C++ #C - Belt Conveyor

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

#C - Belt Conveyor

atcoder.jp

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

/*
AtCoder Problems in C++
#C - Belt Conveyor
https://atcoder.jp/contests/abc265/tasks/abc265_c
提出コードの解答例
https://neguse-atama.hatenablog.com
*/
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(void){
  ll x=1,y=1;
  ll h,w;
  cin>>h>>w;
  char g[h+1][w+1];
  for(ll i=1;i<=h;i++){
    for(ll j=1;j<=w;j++){
      cin>>g[i][j];
    }
  }
  vector<vector<bool>> tf(h+1,vector<bool>(w+1,false));
  while(1){
    if(tf[x][y]){
      cout<<"-1\n";
      return 0;
    }
    tf[x][y]=true;
    if(g[x][y]=='U' && x!=1){
      x--;
    }else if(g[x][y]=='D' && x!=h){
      x++;
    }else if(g[x][y]=='L' && y!=1){
      y--;
    }else if(g[x][y]=='R' && y!=w){
      y++;
    }else{
      cout<<x<<" "<<y<<"\n";
      return 0;
    }
  }
}

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