Official

D - 画像の縮小 / Scale Image Editorial by kyopro_friends Editorial for printing


問題文の指示通りに \(T_{i,j}\) を求めていきましょう。

多くのプログラミング言語では配列の添字が 0 から始まるため、1 から始まる添字に対応するよう書かれている問題文中の数式をそのまま使うことはできないことに注意してください。

実装例(C++)

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

int main(){
	int h,w;
	cin >> h >> w;
	vector<string>s(3*h);
	for(int i=0;i<3*h;i++)cin >> s[i];

	vector<string>t(h,string(w,' '));
	for(int i=0;i<h;i++)for(int j=0;j<w;j++){
		int count=0;
		for(int ii=0;ii<3;ii++)for(int jj=0;jj<3;jj++){
			if(s[3*i+ii][3*j+jj]=='#'){
				count++;
			}
		}
		if(count>=5){
			t[i][j]='#';
		}else{
			t[i][j]='.';
		}
	}

	for(int i=0;i<h;i++){
		cout << t[i] << endl;
	}
}

実装例(Python)

h,w=map(int,input().split())
s=[input() for _ in range(3*h)]
t=[[' ']*w for _ in range(h)]

for i in range(h):
  for j in range(w):
    count = 0
    for ii in range(3):
      for jj in range(3):
        if s[3*i+ii][3*j+jj] == '#':
          count+=1
    if count>=5:
      t[i][j]='#'
    else:
      t[i][j]='.'

for i in range(h):
  print("".join(t[i]))

posted:
last update: