寝癖頭の解法

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

AtCoder Problems in C++ #C - Matrix Reducing

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

#C - Matrix Reducing

atcoder.jp

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

/*
AtCoder Problems in C++
#C - Matrix Reducing
https://atcoder.jp/contests/abc264/tasks/abc264_c
提出コードの解答例
https://neguse-atama.hatenablog.com
*/
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll h1,w1,h2,w2,a[15][15],b[15][15];
ll v1[15],v2[15];
bool tf;
void f1(ll n){
  if(n>w2){
    for(ll i=1;i<=h2;i++){
      for(ll j=1;j<=w2;j++){
        if(a[v1[i]][v2[j]]!=b[i][j]){
          return;
        }
      }
    }
    tf=true;
  }else{
    for(ll i=v2[n-1]+1;i<=w1;i++){
      v2[n]=i;
      f1(n+1);
    }
  }
}
void f2(ll n){
  if(tf){
    return;
  }
  if(n>h2){
    f1(1);
    return;
  }
  for(ll i=v1[n-1]+1;i<=h1;i++){
    v1[n]=i;
    f2(n+1);
  }
}
int main(void){
  cin>>h1>>w1;
  for(ll i=1;i<=h1;i++){
    for(ll j=1;j<=w1;j++){
      cin>>a[i][j];
    }
  }
  cin>>h2>>w2;
  for(ll i=1;i<=h2;i++){
    for(ll j=1;j<=w2;j++){
      cin>>b[i][j];
    }
  }
  f2(1LL);
  if(tf){
    cout<<"Yes\n";
  }else{
    cout<<"No\n";
  }
  return 0;
}

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