题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=5386
Cover
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 724 Accepted Submission(s): 242
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.
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
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
这道题一开始没想出来,看了眼标程就会了,一开始给你的矩阵式没用的,只需要把最后给你的矩阵染上色就好,步骤是从后向前找的。
AC代码
#include<stdio.h>
#include<string>
#define N 105
struct node
{
int x,y;
char judg;
};
node p[510];
int a[N][N],ans[510];
int main()
{
int t,n,m;
int i,j;
scanf("%d",&t);
char str[2];
while(t--){
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(int i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=m;i++)
{
scanf("%s%d%d",&str,&p[i].x,&p[i].y);
p[i].judg=str[0];
}
int cnt=m;
while(cnt!=0){
for(int i=1;i<=m;i++)
if(p[i].x!=0){
int k=p[i].x;
if(p[i].judg=='H'){
for(j=1;j<=n;j++)
if((a[k][j]!=0&&a[k][j]!=p[i].y)) break;
if(j==n+1){
for(j=1;j<=n;j++)
a[k][j]=0;
p[i].x=0;
ans[cnt--]=i;
}
}
else{
for(j=1;j<=n;j++)
if((a[j][k]!=0&&a[j][k]!=p[i].y)) break;
if(j==n+1){
for(j=1;j<=n;j++)
a[j][k]=0;
p[i].x=0;
ans[cnt--]=i;
}
}
}
}
for(int i=1;i<=m;i++)
printf("%d ",ans[i]);
printf("\n");
}
}