# Untitled

unknown
plain_text
2 months ago
1.8 kB
1
Indexable
Never
```#include &lt;iostream&gt;
#include &lt;fstream&gt;
using namespace std;

int N, K, board[100][10], region[100][10], regsizes[1001];

void gravity(void)
{
for (int j=0; j&lt;10; j++) {
int top = N-1, bottom = N-1;
while (top &gt;= 0) {
while (top &gt;= 0 &amp;&amp; board[top][j] == 0) top--;
if (top &gt;= 0)
board[bottom--][j] = board[top--][j];
}
while (bottom &gt;= 0) board[bottom--][j] = 0;
}
}

void visit(int i, int j, int r, int c)
{
if (i&lt;0 || i&gt;=N || j&lt;0 || j&gt;9 || board[i][j]!=c || region[i][j]!=0) return;
region[i][j] = r;
regsizes[r]++;
visit(i-1,j,r,c);
visit(i+1,j,r,c);
visit(i,j-1,r,c);
visit(i,j+1,r,c);
}

bool iterate(void)
{
int r = 1;
for (int i=0; i&lt;N; i++)
for (int j=0; j&lt;10; j++)
region[i][j] = 0;
for (int i=0; i&lt;N; i++)
for (int j=0; j&lt;10; j++)
if (board[i][j] &amp;&amp; !region[i][j]) visit(i,j,r++,board[i][j]);
bool progress = false;
for (int i=0; i&lt;N; i++)
for (int j=0; j&lt;10; j++)
if (board[i][j] &amp;&amp; regsizes[region[i][j]]&gt;=K) {
board[i][j] = 0;
progress = true;
}
gravity();
while (r) regsizes[r--] = 0;
return progress;
}

int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);

cin &gt;&gt; N &gt;&gt; K;
string s;

for (int i=0; i&lt;N; i++) {
cin &gt;&gt; s;
for (int j=0; j&lt;10; j++) board[i][j] = s[j]-'0';
}

while (iterate());

for (int i=0; i&lt;N; i++) {
for (int j=0; j&lt;10; j++) cout &lt;&lt; board[i][j];
cout &lt;&lt; "\n";
}
return 0;
}
```