题意:两种操作:
L x y: for(int i=1;i<=n;i++)color[i][x]=y;
H x y:for(int i=1;i<=n;i++)color[x][i]=y;
给出初始矩阵和目标矩阵,给出m种操作,将其排个序,使得初始矩阵经过排序后的操作能够变成目标矩阵。
从目标矩阵直接反着模拟回来就行了,而且题目保证有解
//#include <bits/stdc++.h>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <time.h>
#include <vector>
#include <cstdio>
#include <string>
#include <iomanip>
///cout << fixed << setprecision(13) << (double) x << endl;
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define Mp(a, b) make_pair(a, b)
#define asd puts("asdasdasdasdasdf");
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef pair <int, int> pl;
//typedef __int64 LL;
const int inf = 0x3f3f3f3f;
const int N = 550;
struct node{
int x, y, op;
}a[N];
int n, m;
int b[N][N], c[N][N];
vector <int> v;
int vis[N];
int main()
{
int tot;
scanf("%d", &tot);
while( tot-- ) {
scanf("%d%d", &n, &m);
v.clear();
memset( vis, 0, sizeof( vis ) );
for( int i = 1; i <= n; ++i ) {
for( int j = 1; j <= n; ++j )
scanf("%d", &b[i][j]);
}
for( int i = 1; i <= n; ++i ) {
for( int j = 1; j <= n; ++j )
scanf("%d", &c[i][j]);
}
for( int i = 1; i <= m; ++i ) {
char op[10];
int x, y;
scanf("%s%d%d", op, &x, &y);
if( op[0] == 'H' )
a[i].op = 1;
else
a[i].op = 0;
a[i].x = x, a[i].y = y;
}
int cnt = 0;
while( cnt < m ) {
for( int i = 1; i <= m; ++i ) {
if( vis[i] )
continue;
int op = a[i].op, x = a[i].x, y = a[i].y;
if( op == 1 ) {
bool OK = 1;
for( int j = 1; j <= n; ++j ) {
if( c[x][j] != y && c[x][j] != -1 ) {
OK = 0;
break;
}
}
if( OK ) {
vis[i] = 1;
for( int j = 1; j <= n; ++j )
c[x][j] = -1;
v.push_back( i );
cnt++;
}
}
else {
bool OK = 1;
for( int j = 1; j <= n; ++j ) {
if( c[j][x] != -1 && c[j][x] != y ) {
OK = 0;
break;
}
}
if( OK ) {
vis[i] = 1;
for( int j = 1; j <= n; ++j )
c[j][x] = -1;
v.push_back( i );
cnt++;
}
}
}
}
for( int i = m-1; i >= 0; -- i )
printf("%d%c", v[i], i == 0? '\n': ' ');
}
return 0;
}