0
0

yukicoder No.82 市松模様 解説

Posted at

問題文

解説

まず、具体例を考える。
なお、実際のコードに合わせるため左上を$(0,0)$とする。

$W=3,H=3,C=$'$B$'のとき
$(1,1)=$'$B$'
$(0,1)=$'$W$'
$(2,2)=$'$B$'

これで分かったかもしれないが、この問題は パリティ(偶奇) について考える問題になっている。
具体的に言うと

$a\hspace{1mm}mod \hspace{1mm}b=0$なら$C$
$a\hspace{1mm}mod \hspace{1mm}b=1$なら$C$じゃないほう

こんな感じに実装すればよい。
なおパリティの問題は競プロ頻出なので覚えておこう。
各$(i,j)$の構築は$O(1)$かかるので、実行時間は$O(HW)$。よって、十分高速。

C++での解答例

#include <bits/stdc++.h>
using namespace std;

int main(){
  int W,H;char C;cin>>W>>H>>C;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if((i+j)%2==0)cout<<C;
      else cout<<(char)('W'+'B'-C);
    }
    cout<<endl;
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0