7K12 blog

猫でも分かる何か

二次元の回転 auto vvrot

二次元の90度回転 (vv rotate 90) テンプレート

auto vvrot90=[&](auto& vv, auto vf)
  • vv: 2次元配列など(入力)
  • vf: 入力の先頭要素 (vv[0][0])
using ll=long long;
template<class T>using vec=vector<T>;
auto vvrot90=[&](auto& vv, auto vf){
	ll W=ll(vv.front().size());
	ll H=ll(vv.size());
	vec<vec<decltype(vf)>> res(W); for(auto& e:res) e.resize(H);
	for(ll y=0;y<H;y++) for(ll x=0;x<W;x++) res[x][H-1-y]=vv[y][x];
	vv.clear(), vv=res;
};
本質

90度回転すると (y,x) が (x,H-1-y) へ移る。