hdu 5386 Cover (暴力)

26 篇文章 0 订阅
10 篇文章 0 订阅

hdu 5386 Cover

Description
You have an 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 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
For each case:
First line has two integer ,
Then lines,every line has integers,describe the initial matrix
Then lines,every line has integers,describe the goal matrix
Then lines,every line describe an operating

Output
For each case,print a line include integers.The i-th integer x show that the rank of x-th operating is

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

题目大意:给你n×n的初始图形,和目标图形,还有m个操作,操作和一把一行或一列变成一种颜色,现在问使初始图形变成目标图形的操作的顺序。每个操作都要用上,且一定有解。
解题思路:初始图形没有用。直接从目标图形开始进行操作。对于一个操作,判断该操作对应的那一行或一列的颜色,是否除0之外全部都是与该操作变换的颜色相同,是的话,将那一行或一列全部变为0,并且记录该操作,最后逆序输出。注意,把所有操作遍历一遍是不够的,因为有一些操作是要在别的操作已进行过的基础上才能进行,所以在遍历操作的外层要再加一层循环(cnt
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int N = 205;
const int M = 2005;
typedef long long ll;
int ans[M];
int G1[N][N], G2[N][N];
struct Node{
    int pos, val, id;
}H[M], L[M];
int n, m, cntH, cntL;

void init() {
    memset(L, 0, sizeof(L));
    memset(H, 0, sizeof(H));
    memset(G2, 0, sizeof(G2));
    cntH = cntL = 0;
}

void input() {
    scanf("%d %d", &n, &m);
    int a, b;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            scanf("%d", &G1[i][j]);
        }   
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            scanf("%d", &G2[i][j]); 
        }   
    }
    char s[20];
    for (int i = 1; i <= m; i++) {
        scanf("%s", s);
        scanf("%d %d", &a, &b);
        if (strcmp(s, "H") == 0) {
            H[cntH].pos = a;    
            H[cntH].val = b;
            H[cntH++].id = i;
        } else {
            L[cntL].pos = a;
            L[cntL].val = b;
            L[cntL++].id = i;
        }
    }
}


void solve() {
    int cnt = 0;
    while (cnt < m) {
        for (int i = 0; i < cntH; i++) {
            int k = H[i].pos, flag = 1;
            if (!k) continue; 
            for (int j = 1; j <= n; j++) {
                if(G2[k][j] && G2[k][j] != H[i].val) {
                    flag = 0; 
                    break;  
                }
            }
            if(flag) {  
                ans[cnt++] = H[i].id;  
                for(int j = 1; j <= n; j++) G2[k][j] = 0;  
                H[i].pos = 0;
            }  
        }
        for (int i = 0; i < cntL; i++) {
            int k = L[i].pos, flag = 1;  
            if (!k) continue;
            for(int j = 1; j <= n; j++) {
                if(G2[j][k] && G2[j][k] != L[i].val) {
                    flag = 0; 
                    break;  
                }
            }
            if(flag) {  
                ans[cnt++] = L[i].id;  
                for(int j = 1; j <= n; j++) G2[j][k] = 0;  
                L[i].pos = 0;
            }  
        }
    }
    for (int i = m - 1; i >= 1; i--) printf("%d ", ans[i]);
    printf("%d\n", ans[0]);
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        init();
        input();    
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值