Cover
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 640 Accepted Submission(s): 204
Special Judge
Problem Description
You have an
n∗n
matrix.Every grid has a color.Now there are two types of operating:
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;
Now give you the initial matrix and the goal matrix.There are m operatings.Put in order to arrange operatings,so that the initial matrix will be the goal matrix after doing these operatings
It's guaranteed that there exists solution.
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;
Now give you the initial matrix and the goal matrix.There are m operatings.Put in order to arrange operatings,so that the initial matrix will be the goal matrix after doing these operatings
It's guaranteed that there exists solution.
Input
There are multiple test cases,first line has an integer
T
For each case:
First line has two integer n , m
Then n lines,every line has n integers,describe the initial matrix
Then n lines,every line has n integers,describe the goal matrix
Then m lines,every line describe an operating
1≤color[i][j]≤n
T=5
1≤n≤100
1≤m≤500
For each case:
First line has two integer n , m
Then n lines,every line has n integers,describe the initial matrix
Then n lines,every line has n integers,describe the goal matrix
Then m lines,every line describe an operating
1≤color[i][j]≤n
T=5
1≤n≤100
1≤m≤500
Output
For each case,print a line include
m
integers.The i-th integer x show that the rank of x-th operating is
i
Sample Input
1 3 5 2 2 1 2 3 3 2 1 3 3 3 3 3 3 3 3 3 3 H 2 3 L 2 2 H 3 3 H 1 3 L 2 3
Sample Output
5 2 4 3 1
Source
原始矩阵没有用。
枚举当前的最后一步。找到一行,或者一列,如果颜色是一样的,并且这一行或者一列有染成这种颜色的操作
那么就可以选择这个操作了。因为这个操作后,颜色就规定了,之前的任何操作都无法影响。
选择这个操作后,属于这一行或者这一列的其他操作就没用了,随便在这个操作之前放一个位置即可。
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;
#define maxn 507
int ini[200][200],gol[200][200];
char op[maxn][2];
int cx[maxn];
int cy[maxn];
vector<int> col[maxn];
vector<int> row[maxn];
vector<int>ans;
int main(){
int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
scanf("%d",&ini[i][j]);
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
scanf("%d",&gol[i][j]);
for(int i = 0;i <= n; i++){
col[i].clear();
row[i].clear();
}
for(int i = 1;i <= m; i++){
scanf("%s%d%d",op[i],&cx[i],&cy[i]);
if(op[i][0] =='L')
col[cx[i]].push_back(i);
else row[cx[i]].push_back(i);
}
ans.clear();
while(1){
int flag = 0;
for(int i = 1;i <= n; i++){
if(row[i].size() == 0) continue;
int k = -1,f = 1;
for(int j = 1;j <= n && f; j++){
if(gol[i][j] == 0 ) continue;
if(k == -1) k = gol[i][j];
if(k != gol[i][j]) f = 0;
}
if(f == 1){
for(int j = 0;j < row[i].size(); j++){
int v = row[i][j];
if(cy[v] == k || k == -1){
ans.push_back(v);
while(row[i].size() > 0){
if(row[i][row[i].size()-1] == v){
row[i].pop_back();
continue;
}
ans.push_back(row[i][row[i].size()-1]);
row[i].pop_back();
}
flag = 1;
for(int l = 1;l <= n; l++)
gol[i][l] = 0;
break;
}
}
}
}
for(int i = 1;i <= n; i++){
if(col[i].size() == 0) continue;
int k = -1, f = 1;
for(int j = 1;j <= n && f; j++){
if(gol[j][i] == 0) continue;
if(k == -1) k = gol[j][i];
if(k != gol[j][i]) f = 0;
}
if(f == 1){
for(int j = 0;j < col[i].size(); j++){
int v = col[i][j];
if(cy[v] == k || k == -1){
ans.push_back(v);
while(col[i].size() > 0){
if(col[i][col[i].size()-1] == v) {
col[i].pop_back();
continue;
}
ans.push_back(col[i][col[i].size()-1]);
col[i].pop_back();
}
flag = 1;
for(int l = 1;l <= n; l++)
gol[l][i] = 0;
break;
}
}
}
}
if(flag == 0) break;
}
for(int i = ans.size()-1; i >= 0; i--){
if(i != 0) printf("%d ",ans[i]);
else printf("%d",ans[i]);
}
printf("\n");
}
return 0;
}