ん~、いまいち。
なんか遅いし。
Siv3DApp.cpp
#include <Siv3D.hpp>
#include <map>
void Main()
{
const unsigned int WIDTH = 32 ;
const unsigned int HEIGHT = 24 ;
std::vector<std::vector<int>> field(WIDTH, std::vector<int>(HEIGHT, 0)) ;
std::map<int, Rect> clusters ;
for (int y = 0, i = 0; y < HEIGHT; ++y) for (int x = 0; x < WIDTH; ++x) {
clusters[i] = Rect{x, y, 1, 1} ;
field[x][y] = i++ ;
}
for (std::vector<Point> p; clusters.size() > 1; p.clear()) {
for (auto d : clusters) {
if ((d.second.x + d.second.w) < WIDTH) {
auto t = field[d.second.x + d.second.w][d.second.y] ;
if (d.second.y == clusters[t].y && d.second.h == clusters[t].h) p.emplace_back(Point{d.first, t}) ;
}
if ((d.second.y + d.second.h) < HEIGHT) {
auto t = field[d.second.x][d.second.y + d.second.h] ;
if (d.second.x == clusters[t].x && d.second.w == clusters[t].w) p.emplace_back(Point{d.first, t}) ;
}
}
if (p.empty()) break ;
Point t = p[Random<int>(0, p.size()-1)] ;
if ((Max(clusters[t.x].w, clusters[t.x].h) / Min(clusters[t.x].w, clusters[t.x].h)) >= 3) t = p[Random<int>(0, p.size()-1)] ;
if (clusters[t.x].x + clusters[t.x].w < WIDTH && t.y == field[clusters[t.x].x + clusters[t.x].w][clusters[t.x].y]) clusters[t.x].w += clusters[t.y].w ;
else {
clusters[t.x].h += clusters[t.y].h ;
}
for (unsigned int y = 0; y < HEIGHT; ++y) for (unsigned int x = 0; x < WIDTH; ++x) if (field[x][y] == t.y) field[x][y] = t.x ;
clusters.erase(clusters.find(t.y)) ;
}
int i = 0 ;
for (auto c : clusters) {
for (unsigned int y = 0; y < HEIGHT; ++y) for (unsigned int x = 0; x < WIDTH; ++x) if (field[x][y] == c.first) field[x][y] = i ;
++i ;
}
const int CSIZE = 20 ;
while (System::Update()) {
for (int y = 0; y < HEIGHT; ++y) for (int x = 0; x < WIDTH; ++x) {
if (x == (WIDTH - 1) || field[x][y] != field[x+1][y]) Line(CSIZE * (x + 1), CSIZE * y, CSIZE * (x + 1), CSIZE * (y + 1)).draw() ;
if (y == (HEIGHT - 1) || field[x][y] != field[x][y+1]) Line(CSIZE * x, CSIZE * (y + 1), CSIZE * (x + 1), CSIZE * (y + 1)).draw() ;
}
}
}