hdu 5386 Cover(暴搞+队列)

Cover

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

solution:

给你一个初始矩阵和最终矩阵,给你m种操作,可以把第x行都变成y,或可以把第x列都变成y,确保有一种顺序是可行的,要你把这操作的顺序输出。

此题直接暴力即可,每次检查当前操作能否当成当前最后一步,即该行或该列的元素全都符合y或之前标记过,如果可以,就标记,若不行,把该操作继续放到队列中,循环即可。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int inf = 1e9;
int ans[505];
int c2[105][105];
int n, m,nl,nh,cnt;
struct node{
    int x,y,id;
    int flag;
}now,nex;
int main()
{
    int t,x,y;
    char op[10];
    scanf("%d", &t);
    while (t--)
    {
        cnt = 1;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                scanf("%d", &x);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                scanf("%d", &c2[i][j]);
        queue<node>que;
        for (int i = 1; i <= m; i++)
        {
            scanf("%s%d%d", op, &now.x, &now.y);
            now.id = i;
            if (op[0] == 'H')now.flag = 0;
            else now.flag = 1;
            que.push(now);
        }
        while (!que.empty())
        {
            nex = que.front(); que.pop();
            x = nex.x; y = nex.y;
            if (nex.flag == 0)
            {
                int f = 1;
                for (int j = 1; j <= n; j++)
                if (c2[x][j] == inf || c2[x][j] == y);
                else { f = 0; break; }
                if (f)
                {
                    for (int j = 1; j <= n; j++)
                        c2[x][j] = inf;
                    ans[cnt++] = nex.id;
                }
                else que.push(nex);
            }
            else 
            {
                int f = 1;
                for (int j = 1; j <= n; j++)
                    if (c2[j][x] == inf || c2[j][x] == y);
                    else { f = 0; break; }
                if (f)
                {
                    for (int j = 1; j <= n; j++)
                        c2[j][x] = inf;
                    ans[cnt++] = nex.id;
                }
                else que.push(nex);
            }
        }
        for (int i = m; i >= 1; i--)
            printf("%d%c", ans[i], i == 1 ? '\n' : ' ');
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_22522375/article/details/51524771
今日推荐