寝癖頭の解法

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

AtCoder Problems in C++ #D - Number of Amidakuji

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

#D - Number of Amidakuji

atcoder.jp

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

/*
AtCoder Problems in C++
#D - Number of Amidakuji
https://atcoder.jp/contests/abc113/tasks/abc113_d
提出コードの解答例 
https://neguse-atama.hatenablog.com
*/
#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using mi=modint1000000007; //ACL-modintより
int main(void){
  ll h,w,k;
  cin>>h>>w>>k;
  vector<vector<mi>> dp(h+1,vector<mi>(w,0));
  dp[0][0]=1;
  for(ll i=0;i<h;i++){
    for(ll j=0;j<w;j++){
      for(ll k=0;k<(1<<(w-1));k++){
        if(k&(k>>1)){
          continue;
        }
        if(j>=1 && ((k>>(j-1))&1)){
          dp[i+1][j-1]+=dp[i][j];
        }else if(j<=w-2 && ((k>>j)&1)){
          dp[i+1][j+1]+=dp[i][j];
        }else{
          dp[i+1][j]+=dp[i][j];
        }
      }
    }
  }
  cout<<dp[h][k-1].val()<<endl;
  return 0;
}

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