BZOJ 1142 [POI2009]Tab 模拟

题意:链接略

方法:模拟

解析:

首先如果您做过高三那么您一定清楚,一个矩阵通过任意行列变换后得到的矩阵满足一种性质:原来在哪一行的元素还在那一行,原来在哪一列的元素还在那一列。

并且行列内部元素可以任意交换。

所以暴力验证即可。

代码附带5K读入优化,怒艹BZOJrnk1

代码:

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdarg>
#include <iostream>
#include <algorithm>
#ifdef __GNUC__
#include<unistd.h>
#endif
//#define UsingIOException
static class Scanner
{
    typedef double D;
    typedef unsigned int long II;
    typedef signed int long SI;
    typedef unsigned char S;
    typedef char C;
    typedef bool B;
    static II const ScL =16384,Eof =0xff;
#ifdef __GNUC__
    II const buffer;
#else
    FILE *const buffer;
#endif
    S ram[ScL],*begin,*end;
public:
#ifdef UsingIOException
    enum IOException
    {
        FileEof,TypeError
    };
#endif
#ifdef __GNUC__
#ifdef Fin
    Scanner(II const iobuffer=open(Fin,1)):buffer(iobuffer),begin(ram),end(ram)
#else
    Scanner(II const iobuffer=0):buffer(iobuffer),begin(ram),end(ram)
#endif
#else
#ifdef Fin
    Scanner(FILE *const iobuffer=fopen(Fin,"rb")):buffer(iobuffer),begin(ram),end(ram)
#else
    Scanner(FILE *const iobuffer=stdin):buffer(iobuffer),begin(ram),end(ram)
#endif
#endif
    {
    }
    ~Scanner(void)
    {
#ifdef __GNUC__
        close(buffer);
#else
        fclose(buffer);
#endif
    }
    II get(void)
    {
        if(begin==end)
        {
            begin=ram;
#ifdef __GNUC__
            if((end=(begin=ram)+read(buffer,ram,ScL))==ram)
#else
            if((end=(begin=ram)+fread(ram,1,ScL,buffer))==ram)
#endif
                return Eof;
        }
        return *begin++;
    }
    D getSD(void)
    {
        II j;
        while(isspace(j=get()));
        B neg=false;
        if(j=='-')
        {
            neg=true;
            j=get();
        }
#ifdef UsingIOException
        if(!isdigit(j))
            throw j==Eof?FileEof:TypeError;
#endif
        SI i=j-'0';
        while(isdigit(j=get()))
            i=(i<<3)+(i<<1)+(j-'0');
        if(j=='.')
        {
            D val=D(i),level=1.;
            while(isdigit(j=get()))
                val+=(level*=.1)*D(j-'0');
            return neg?-val:val;
        }
        return neg?-i:i;
    }
    D getD(void)
    {
        II j;
        while(isspace(j=get()));
#ifdef UsingIOException
        if(!isdigit(j))
            throw j==Eof?FileEof:TypeError;
#endif
        II i=j-'0';
        while(isdigit(j=get()))
            i=(i<<3)+(i<<1)+(j-'0');
        if(j=='.')
        {
            D val=D(i),level=1.;
            while(isdigit(j=get()))
                val+=(level*=.1)*D(j-'0');
            return val;
        }
        return i;
    }
    SI getSI(void)
    {
        II j;
        while(isspace(j=get()));
        B neg=false;
        if(j=='-')
        {
            neg=true;
            j=get();
        }
#ifdef UsingIOException
        if(!isdigit(j))
            throw j==Eof?FileEof:TypeError;
#endif
        SI i=j-'0';
        while(isdigit(j=get()))
            i=(i<<3)+(i<<1)+(j-'0');
        return neg?-i:i;
    }
    II getII(void)
    {
        II j;
        while(isspace(j=get()));
#ifdef UsingIOException
        if(!isdigit(j))
            throw j==Eof?FileEof:TypeError;
#endif
        II i=j-'0';
        while(isdigit(j=get()))
            i=(i<<3)+(i<<1)+(j-'0');
        return i;
    }
    II getS(void)
    {
        II j;
        while(isspace(j=get()));
#ifdef UsingIOException
        if(!isascii(j))
            throw j==Eof?FileEof:TypeError;
#endif
        return j;
    }
    C * getCS(C *p)
    {
        skip();
        while(!isspace(*p++=get()));
        *--p='\0';
        return p;
    }
    void skip(int (*const func)(int)=isspace)
    {
        while(func(get()));
        --begin;
    }
}scan;
static class Printer
{
    typedef unsigned int long II;
    typedef char C;
    static II const ScL =16384,Ral =100;
    C self[ScL+Ral],*sp,*const slimit;
    FILE *const buffer;
public:
#ifdef Fout
    Printer(FILE *const iobuffer=fopen(Fout,"wb")):sp(self),slimit(self+ScL),buffer(iobuffer)
#else
    Printer(FILE *const iobuffer=stdout):sp(self),slimit(self+ScL),buffer(iobuffer)
#endif
    {
    }
    ~Printer(void)
    {
        flush();
        fclose(buffer);
    }
    void flush(void)
    {
        fwrite(self,1,sp-self,buffer);
        sp=self;
    }
    void print(C const *const s,...)
    {
        va_list h;
        va_start(h,s);
        if((sp+=vsprintf(sp,s,h))>=slimit)
            flush();
        va_end(h);
    }
    void puts(C const *const s)
    {
        if((sp+=strlen(strcpy(sp,s)))>=slimit)
            flush();
    }
    void putchar(II const x)
    {
        *sp++=(C)x;
        if(sp>=slimit)
            flush();
    }
}sysout;

#define N 1000100
using namespace std;
struct node
{
    int x,y;
}a[N<<1];
int t;
int n,m;
int b[1010][1010];
int main()
{
    t=scan.getSI();
    while(t--)
    {
        n=scan.getSI();
        m=scan.getSI();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                int x;
                x=scan.getSI();
                x+=1000000;
                a[x].x=i,a[x].y=j;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                b[i][j]=scan.getSI();
            }
        }
        int flag=0;
        if(!flag)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=2;j<=m;j++)
                {
                    if(a[b[i][j]+1000000].x!=a[b[i][j-1]+1000000].x)
                    {flag=1;break;}
                }
                if(flag)break;
            }
        }
        if(flag){puts("NIE");continue;}
        for(int i=1;i<=m;i++)
        {
            for(int j=2;j<=n;j++)
            {
                if(a[b[j][i]+1000000].y!=a[b[j-1][i]+1000000].y)
                {flag=1;break;}
            }
            if(flag)break;
        }
        if(flag)puts("NIE");
        else puts("TAK");
    }
}

你可能感兴趣的:(代码,优化,poi,2009)