[乱搞 暴力] BZOJ 1142 [POI2009]Tab

题解:http://blog.csdn.net/vmurder/article/details/45688219

无论如何变换,该在一行的还是会在一行,该在一列的还是会在一列。

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(int &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

struct array{
	int len,a[1005];
	void sort(){
		::sort(a+1,a+len+1);
	}
	bool operator < (const array &B) const{
		for (int i=1;i<=len;i++)	
			if (a[i]!=B.a[i])
				return a[i]<B.a[i];
		return 0;
	}
	bool operator == (const array &B) const{
		if (len!=B.len) return 0;
		for (int i=1;i<=len;i++)
			if (a[i]!=B.a[i])
				return 0;
		return 1;
	}
}A[1005],B[1005];

int n,m;
int a[1005][1005],b[1005][1005];

inline bool Check(){
	for (int i=1;i<=n;i++)
	{
		A[i].len=m;
		for (int j=1;j<=m;j++) A[i].a[j]=a[i][j];
		A[i].sort();
	}
	sort(A+1,A+n+1);
	for (int i=1;i<=n;i++)
	{
		B[i].len=m;
		for (int j=1;j<=m;j++) B[i].a[j]=b[i][j];
		B[i].sort();
	}
	sort(B+1,B+n+1);
	for (int i=1;i<=n;i++)
		if (!(A[i]==B[i]))
			return 0;
	return 1;
}

int tmp[1005][1005];

inline void GT(){
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			tmp[j][i]=a[i][j];
	for (int i=1;i<=m;i++)
		for (int j=1;j<=n;j++)
			a[i][j]=tmp[i][j];
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			tmp[j][i]=b[i][j];
	for (int i=1;i<=m;i++)
		for (int j=1;j<=n;j++)
			b[i][j]=tmp[i][j];
	swap(n,m);
}

int main()
{
	int Q;
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(Q);
	while (Q--)
	{
		read(n); read(m);
		for (int i=1;i<=n;i++)
			for (int j=1;j<=m;j++)
				read(a[i][j]);
		for (int i=1;i<=n;i++)
			for (int j=1;j<=m;j++)
				read(b[i][j]);
		if (!Check()) 
			printf("NIE\n");
		else
		{
			GT();
			if (!Check()) 
				printf("NIE\n");
			else
				printf("TAK\n");
		}
	}
	return 0;
}


你可能感兴趣的:([乱搞 暴力] BZOJ 1142 [POI2009]Tab)