您的位置:首页 > 其它

hdu5386 Cover(暴力,观察)

2015-08-28 21:45 148 查看
题目:

Cover

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1449 Accepted Submission(s): 498

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

1≤color[i][j]≤n

T=5

1≤n≤100

1≤m≤500


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

题意:给一个初始矩阵和目标矩阵,以及m个操作,让你把m个操作排序使得按照这些操作可以从初始矩阵变换为目标矩阵。

思路:由于操作是把整行或整列替换成一个新的值,所以之前对该行(列)的操作都会被后面的操作覆盖掉,我们只要根据目标矩阵的值不断确定最后的操作就可以了,直到把m个操作确定完。至于确定最后的操作,只要目标矩阵中该行(列)的值和覆盖的值相同,那么它就是最后的操作。

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/

int a[105][105];
struct op
{
    char o;
    int x;
    int y;
};
op p[505];
int ans[505];
int vis[505];
int main()
{
int T;
//freopen("in.txt","r",stdin);
RI(T);
while(T--)
{
    int n,m;
    RII(n,m);
    MS0(vis);
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        RI(a[i][j]);
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        RI(a[i][j]);
        for(int i=0;i<m;i++)
        {
            getchar();
            char str[10];
            scanf("%s%d%d",str,&p[i].x,&p[i].y);
            p[i].o=str[0];

        }
    int t=0;
    while(t<m)
    {
        for(int i=0;i<m;i++)
         if(!vis[i])
        {
            if(p[i].o=='L')
            {
                int x=p[i].x-1;
                int y=p[i].y;
                int ok=1;
                for(int j=0;j<n;j++)
                    if(a[j][x]&&a[j][x]!=y)
                    {
                        ok=0;
                        break;

                    }
                    if(ok)
                    {
                        ans[t++]=i+1;
                        for(int j=0;j<n;j++)
                            a[j][x]=0;
                        vis[i]=1;
                    }
            }
            else
            {
                int x=p[i].x-1;
                int y=p[i].y;
                int ok=1;
                for(int j=0;j<n;j++)
                {
                    if(a[x][j]&&a[x][j]!=y)
                    {
                        ok=0;
                        break;
                    }
                }
                if(ok)
                {
                    ans[t++]=i+1;
                    for(int j=0;j<n;j++)
                            a[x][j]=0;
                        vis[i]=1;

                }
            }

        }

    }
    for(int i=m-1;i>0;i--)
        printf("%d ",ans[i]);
    printf("%d\n",ans[0]);

}

        return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: