hdu5386 棋盘涂色模拟

来源:互联网 发布:淘宝客新手怎么推广 编辑:程序博客网 时间:2024/04/28 08:21

http://acm.hdu.edu.cn/showproblem.php?pid=5386

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
13 52 2 1 2 3 3 2 1 3 3 3 3 3 3 3 3 3 3 H 2 3L 2 2H 3 3H 1 3L 2 3
 

Sample Output
5 2 4 3 1

/** hdu5386  模拟 题目大意:给一个棋盘涂色,每次操作给棋盘的某一列或一行涂上一种颜色,给定初始棋盘状态和最终棋盘状态,和一系列操作,请给这些序列排一个顺序 解题思路:初始棋盘其实并没有什么用,我们从最终棋盘入手,每次找到一行或一列颜色全部一样的,如给定操作中有给这一行的如此操作,那么给此操作入队,           依次下去得到一个顺序,输出即可            */  #include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>#include <vector>using namespace std;const int maxn=105;int a[maxn][maxn],n,m,x,y;int judge[maxn*5],flag[maxn*5],num[maxn*5];vector<pair<int,int> >x_vec[105],y_vec[105];char s[10];int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        int x;        for(int i=0; i<n*n; i++)scanf("%d",&x);        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++)        {            x_vec[i].clear();            y_vec[i].clear();        }        for(int i=1; i<=m; i++)        {            scanf("%s%d%d",s,&x,&y);            if(s[0]=='H')            {                x_vec[x].push_back(make_pair(y,i));            }            else            {                y_vec[x].push_back(make_pair(y,i));            }        }        memset(judge,0,sizeof(judge));        int t=m;        while(t)        {            int ok=0;            for(int i=1; i<=n; i++)            {                int sum=0,cnt=0;                memset(flag,0,sizeof(flag));                for(int j=1; j<=n&&sum<=1; j++)                {                    if(a[i][j]==0)continue;                    if(!flag[a[i][j]])                    {                        flag[a[i][j]]=1;                        sum++;                        cnt=a[i][j];                    }                }                if(sum==1)                {                    int len=x_vec[i].size();                    for(int j=0; j<len; j++)                    {                        if(x_vec[i][j].first==cnt)                        {                            ok=1;                            num[t--]=x_vec[i][j].second;                            judge[x_vec[i][j].second]=1;                            break;                        }                    }                    for(int j=1; j<=n; j++)                        a[i][j]=0;                }                sum=0,cnt=0;                memset(flag,0,sizeof(flag));                for(int j=1; j<=n&&sum<=1; j++)                {                    if(a[j][i]==0)continue;                    if(!flag[a[j][i]])                    {                        flag[a[j][i]]=1;                        sum++;                        cnt=a[j][i];                    }                }                if(sum==1)                {                    int len=y_vec[i].size();                    for(int j=0; j<len; j++)                    {                        if(y_vec[i][j].first==cnt)                        {                            ok=1;                            num[t--]=y_vec[i][j].second;                            judge[y_vec[i][j].second]=1;                            break;                        }                    }                    for(int j=1; j<=n; j++)                        a[j][i]=0;                }            }            if(ok==0)break;        }        for(int i=1; i<=n; i++)        {            int len=x_vec[i].size();            for(int j=0; j<len; j++)            {                if(judge[x_vec[i][j].second]==0)                    num[t--]=x_vec[i][j].second;            }            len=y_vec[i].size();            for(int j=0; j<len; j++)            {                if(judge[y_vec[i][j].second]==0)                    num[t--]=y_vec[i][j].second;            }        }        for(int i=1; i<=m; i++)        {            printf(i==m?"%d\n":"%d ",num[i]);        }    }    return 0;}


0 0
原创粉丝点击