bzoj1142:[POI2009]Tab

传送门

考虑每次交换都不会改变每个数所在的行和列(不是指编号,而是指和它在同一行或者同一列的数不会发生变化)
由于每个数互不相同,所以记录下每个数所在的行和列,暴力判断就好了
代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
void read(int &x) {
    char ch; bool ok;
    for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
    for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int p=1e6;
int T,n,m,x[p*2+10],y[p*2+10],a[1010][1010];
int main()
{
    read(T);
    while(T--)
    {
        read(n),read(m);
        memset(x,0,sizeof x),memset(y,0,sizeof y); 
        for(rg int i=1;i<=n;i++)
            for(rg int j=1,v;j<=m;j++)
                read(v),v+=p,x[v]=i,y[v]=j;
        bool flag=0;
        for(rg int i=1;i<=n;i++)
            for(rg int j=1;j<=m;j++)
            {
                read(a[i][j]);a[i][j]+=p;
                if(x[a[i][j]]!=x[a[i][1]]||!x[a[i][j]])flag=1;
                if(y[a[i][j]]!=y[a[1][j]]||!y[a[i][j]])flag=1;
            }
        if(flag)printf("NIE\n");
        else printf("TAK\n");
    }
}

猜你喜欢

转载自www.cnblogs.com/lcxer/p/10522508.html