Cover
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1049 Accepted Submission(s): 217
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
我们只要每次找一行或一列颜色除了0都相同的,然后如果有对应的操作,就把这行这列都赋值成0即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<set>
#include<vector>
#include<cmath>
using namespace std;
int INF =0x3f3f3f3f;
int cur[105][105], goal[105][105];
bool vis[105][105];
int n, m;
struct nde
{
char c;
int x, y, id;
int sortlist;
} node[555];
bool cmp(nde a, nde b)
{
return a.sortlist<b.sortlist;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
INF = 0x3f3f3f3f;
memset(node,0,sizeof node);
memset(vis,0,sizeof vis);
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
scanf("%d",&cur[i][j]);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
scanf("%d",&goal[i][j]);
for(int i=0; i<m; i++)
{
char s[10];
scanf("%s%d%d",s,&node[i].x,&node[i].y);
node[i].c=s[0];
node[i].id=i+1;
}
int temp = m;
while(temp--)
{
for(int i=0; i<m; i++)
{
if(node[i].sortlist>0) continue;
bool flag=true;
if(node[i].c=='L')
{
//如果修改过了该值或者该值就是原来的 那就不用再改了
for(int j=1; j<=n; j++)
if(!(goal[j][node[i].x] == node[i].y || vis[j][node[i].x]))
flag=false;
if(flag)
{
for(int j=1; j<=n; j++)
vis[j][node[i].x]=true; //访问过了
node[i].sortlist=temp;
break;
}
}
else if (node[i].c =='H')
{
for(int j=1; j<=n; j++)
{
if(!(goal[node[i].x][j] == node[i].y || vis[node[i].x][j]))
flag=false;
}
if(flag)
{
for(int j=1; j<=n; j++)
vis[node[i].x][j]=true;
node[i].sortlist=temp;
break;
}
}
}
}
sort(node,node+m,cmp);
int i = 0;
for(i=0; i<m-1; i++)
printf("%d ",node[i].id);
printf("%d\n",node[i].id);
}
return 0;
}