#include <iostream>
#include <vector>
#include <cstdint>
#include <algorithm>
#include <random>
#include <tuple>
typedef std::vector<std::vector<std::uint64_t>>DType;
typedef std::vector<std::int64_t> ivec;
static const std::uint64_t EmptyValue = 0;
std::tuple<std::uint64_t, std::uint64_t> FindEmpty(DType& D){
for (std::size_t i = 0; i < D.size(); i++){
for (std::size_t j = 0; j < D[i].size(); j++)
{
if (D[i][j] == EmptyValue){
return std::make_tuple(j, i);
}
}
}
return std::make_tuple(3, 3);//ひどいバグだ。仕様通りなら到達しないパス。
}
DType MakeHoge(DType D, std::uint32_t Seed = 0,std::int32_t Suffle=48){
ivec XP{ 0, 1, 0, -1 };
ivec YP{ 1, 0, -1, 0 };
std::uint64_t Dir = 0;
std::int64_t X = 0;
std::int64_t Y = 0;
std::mt19937 mt(Seed);
std::uniform_int_distribution<> uid(Suffle-Suffle/2, Suffle+Suffle/2);//最長手数-初期位置前回数-壁衝突回数
std::uniform_int_distribution<> DR(0, 3);
std::uint64_t R = uid(mt);
std::tie(X, Y) = FindEmpty(D);
for (std::size_t i = 0; i < R; i++){
Dir = DR(mt);
if (X + XP[Dir] < 0) continue;
if (Y + YP[Dir] < 0) continue;
if (X + XP[Dir] >= D[0].size()) continue;
if (Y + YP[Dir] >= D.size()) continue;
std::swap(D[Y][X], D[Y + YP[Dir]][X + XP[Dir]]);
X += XP[Dir];
Y += YP[Dir];
}
std::tie(X, Y) = FindEmpty(D);
for (std::size_t i=X+1,j=X; i < D[0].size(); i++){
std::swap(D[Y][j], D[Y][i]);
j = i;
}
for (std::size_t i=Y+1,j=Y; i < D.size(); i++){
std::swap(D[j][D[0].size()-1], D[i][D[0].size()-1]);
j = i;
}
return D;
}
bool Show(DType& D){
for (auto& oo:D){
for (auto& o:oo){
if(o==EmptyValue){
std::cout << 'E' << ' ';
}else{
std::cout<<o<<' ';
}
}
std::cout << std::endl;
}
return true;
}
int main(){
DType D{ { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15,EmptyValue },
};
D = MakeHoge(D);
Show(D);
std::cout << std::endl;
D = MakeHoge(D,1);
Show(D);
std::cout << std::endl;
D = MakeHoge(D,2);
Show(D);
std::cout << std::endl;
D = MakeHoge(D,3);
Show(D);
std::cout << std::endl;
D = MakeHoge(D,4);
Show(D);
std::cout << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y3N0ZGludD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHJhbmRvbT4KI2luY2x1ZGUgPHR1cGxlPgoKdHlwZWRlZiBzdGQ6OnZlY3RvcjxzdGQ6OnZlY3RvcjxzdGQ6OnVpbnQ2NF90Pj5EVHlwZTsKdHlwZWRlZiBzdGQ6OnZlY3RvcjxzdGQ6OmludDY0X3Q+IGl2ZWM7CgpzdGF0aWMgY29uc3Qgc3RkOjp1aW50NjRfdCBFbXB0eVZhbHVlID0gMDsKCnN0ZDo6dHVwbGU8c3RkOjp1aW50NjRfdCwgc3RkOjp1aW50NjRfdD4gRmluZEVtcHR5KERUeXBlJiBEKXsKCWZvciAoc3RkOjpzaXplX3QgaSA9IDA7IGkgPCBELnNpemUoKTsgaSsrKXsKCQlmb3IgKHN0ZDo6c2l6ZV90IGogPSAwOyBqIDwgRFtpXS5zaXplKCk7IGorKykKCQl7CgkJCWlmIChEW2ldW2pdID09IEVtcHR5VmFsdWUpewoJCQkJcmV0dXJuIHN0ZDo6bWFrZV90dXBsZShqLCBpKTsKCQkJfQoJCX0KCX0KCXJldHVybiAgc3RkOjptYWtlX3R1cGxlKDMsIDMpOy8v44Gy44Gp44GE44OQ44Kw44Gg44CC5LuV5qeY6YCa44KK44Gq44KJ5Yiw6YGU44GX44Gq44GE44OR44K544CCCn0KCgpEVHlwZSBNYWtlSG9nZShEVHlwZSBELCBzdGQ6OnVpbnQzMl90IFNlZWQgPSAwLHN0ZDo6aW50MzJfdCBTdW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