hdu 5386 Cover 2015多校联合训练赛#8 枚举

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  nn  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
 

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;
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值