首页 > 代码库 > HDU 5386 Cover(模拟)

HDU 5386 Cover(模拟)

Cover

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 966    Accepted Submission(s): 320
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

1color[i][j]n
T=5
1n100
1m500
 

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
 

Author
SXYZ
 

Source
2015 Multi-University Training Contest 8
 

题意:给出两个n*n的矩阵。一个作为初始矩阵,一个作为目标矩阵。给出m个操作,操作有两种,

         一种是“L。x。y”,代表我们要把x这一行赋成y,还有一种是“H,x,y”,代表要把x这一列赋成y,

          问我们怎样安排这些操作才干把初始矩阵转化成目标矩阵。

输出方案,special judge

题解:最后一个操作肯定是把某一行或者某一列变成x,我们倒过来模拟,每次把最后一个操作找出来。即每次找到某一行

           或者某一列不为0的数都同样的,再找符合操作的。


#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
#define N 110

using namespace std;
int a[N][N];

struct Cao {
    char s[2];
    int x,v;
    bool used;
} b[N*5];
int ans[N*5];
int n,m;

bool is_H(int i,int k) {
    int x=-1;
    int j=1;
    for(; j<=n; j++) {
        if(a[i][j]) {
            x=a[i][j];
            break;
        }
    }
    if(x==-1)return true;
    if(x!=k)return false;
    for(; j<=n; j++) {
        if(a[i][j]&&a[i][j]!=x)return false;
    }
    return true;
}

bool is_L(int i,int k) {
    int x=-1;
    int j=1;
    for(; j<=n; j++) {
        if(a[j][i]) {
            x=a[j][i];
            break;
        }
    }
    if(x==-1)return true;
    if(x!=k)return false;
    for(; j<=n; j++) {
        if(a[j][i]&&a[j][i]!=x)return false;
    }
    return true;
}

int main() {
    // freopen("test.in","r",stdin);
    int t;
    cin>>t;
    while(t--) {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                scanf("%d",&a[i][j]);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                scanf("%d",&a[i][j]);
        for(int i=1; i<=m; i++) {
            scanf("%s%d%d",b[i].s,&b[i].x,&b[i].v);
            b[i].used=0;
        }
        for(int h=m; h>=1; h--) {
            for(int i=1; i<=m; i++) {
                if(b[i].used)continue;
                if(b[i].s[0]=='H'&&is_H(b[i].x,b[i].v)) {
                    ans[h]=i;
                    b[i].used=1;
                    int p=b[i].x;
                    for(int k=1; k<=n; k++)
                        a[p][k]=0;
                    break;
                } else if(b[i].s[0]=='L'&&is_L(b[i].x,b[i].v)) {
                    ans[h]=i;
                    b[i].used=1;
                    int p=b[i].x;
                    for(int k=1; k<=n; k++)
                        a[k][p]=0;
                    break;
                }
            }
        }
        for(int i=1; i<m; i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[m]);
    }
    return 0;
}


HDU 5386 Cover(模拟)