题解:P11942 [KTSC 2025] 重塑矩阵 / grid_encoding
类似的条件:CF1214G。
根据上面这个题,我们得到的结论是:将每一行
该结论在列上也同样成立。由此可以推导出一个性质:该矩阵要么存在一个全
考虑一个用
对于 Alice,我们对上述过程进行模拟。根据上面的结论,一个 1-列 只在删去了某个 0-行 后产生(或一开始就产生),那么若在删去 0-行
对于 Bob,我们将上述的模拟过程进行修改:每次选择一个不存在
这样就解决了这个问题。一个细节是注意全
-1 -1 -1
-1 -1 -1
0 0 0
那么不妨在 Bob 的模拟过程中,先尝试填所有 0-行,再尝试填所有 1-列,以此循环即可。
#define N 510
int n, a[N][N];
bool visx[N], visy[N];
int cntx[N], cnty[N];
void send(std::vector<std::vector<int>> _mp)
{
n = _mp.size();
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) a[i][j] = _mp[i][j];
int nowx = n, nowy = n;
for(int i = 0; i < n; i++) visx[i] = visy[i] = 0;
while(nowx && nowy)
{
for(int i = 0; i < n; i++) cntx[i] = cnty[i] = 0;
for(int i = 0; i < n; i++) if(!visx[i]) for(int j = 0; j < n; j++) if(!visy[j]) cntx[i] += (a[i][j] == 0), cnty[j] += (a[i][j] == 1);
for(int i = 0; i < n; i++) if(!visx[i] && cntx[i] == nowy)
{
visx[i] = 1; --nowx;
for(int j = 0; j < n; j++) if(!visy[j] && cnty[j] == nowx) select(i, j);
}
if(!nowx) break;
for(int i = 0; i < n; i++) cntx[i] = cnty[i] = 0;
for(int i = 0; i < n; i++) if(!visx[i]) for(int j = 0; j < n; j++) if(!visy[j]) cntx[i] += (a[i][j] == 0), cnty[j] += (a[i][j] == 1);
for(int i = 0; i < n; i++) if(!visy[i] && cnty[i] == nowx)
{
visy[i] = 1; --nowy;
for(int j = 0; j < n; j++) if(!visx[j] && cntx[j] == nowy) select(j, i);
}
}
}
std::vector<std::vector<int>> reconstruct(std::vector<std::vector<int>> _mp)
{
n = _mp.size();
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) a[i][j] = _mp[i][j];
int nowx = n, nowy = n;
for(int i = 0; i < n; i++) visx[i] = visy[i] = 0;
while(nowx && nowy)
{
for(int i = 0; i < n; i++) cntx[i] = cnty[i] = 0;
for(int i = 0; i < n; i++) if(!visx[i]) for(int j = 0; j < n; j++) if(!visy[j]) cntx[i] += (a[i][j] != 1), cnty[j] += (a[i][j] != 0);
for(int i = 0; i < n; i++) if(!visx[i] && cntx[i] == nowy)
{
visx[i] = 1; --nowx;
for(int j = 0; j < n; j++) if(!visy[j]) a[i][j] = 0;
}
if(!nowx) break;
for(int i = 0; i < n; i++) cntx[i] = cnty[i] = 0;
for(int i = 0; i < n; i++) if(!visx[i]) for(int j = 0; j < n; j++) if(!visy[j]) cntx[i] += (a[i][j] != 1), cnty[j] += (a[i][j] != 0);
for(int i = 0; i < n; i++) if(!visy[i] && cnty[i] == nowx)
{
visy[i] = 1; --nowy;
for(int j = 0; j < n; j++) if(!visx[j]) a[j][i] = 1;
}
}
std::vector<std::vector <int> > ret;
for(int i = 0; i < n; i++)
{
std::vector <int> now;
for(int j = 0; j < n; j++) now.push_back(a[i][j]);
ret.push_back(now);
}
return ret;
}