(2)搜索
题目链接https://blog.csdn.net/ayf1988/article/details/101418391
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=25;
inline int read() {
int x=0;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
return x;
}
int T,num,n,m;
char mp[N][N];
bool hang[N][N],lie[N][N];
bool tg(int l,int r,int n) {
int ans1=0,ans2=0;
char a[N],b[N];
for(int i=1;i<=n;i++)
a[i]=a[i+n]=mp[l][i],b[i]=b[i+n]=mp[r][i];
for(int i=1,j=2;i<=n&&j<=n;) {
int k=0;
for(;k<n&&a[i+k]==a[j+k];k++);
if(k==n) break;
if(a[j+k]>a[i+k]) swap(i,j);
i=i+k+1;
i+=i==j?1:0;
ans1=j;
}
for(int i=1,j=2;i<=n&&j<=n;) {
int k=0;
for(;k<n&&b[i+k]==b[j+k];k++);
if(k==n) break;
if(b[j+k]>b[i+k]) swap(i,j);
i=i+k+1;
i+=i==j?1:0;
ans2=j;
}
for(int i=0;i<n;i++)
if(a[i+ans1]!=b[i+ans2]) return 0;
return 1;
}
char s1[N],s2[N];
bool vis1[N],vis2[N];
bool flag;
inline void check() {
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (mp[s2[i]][s1[j]]!=mp[s2[n-i+1]][s1[m-j+1]]) return;
printf("YES
");
flag=1;
}
void solve_lie(int pos,int op) {
if(flag) return;
if(pos==0) {check();return;}
if(op) {
for(int i=1;i<=m;i++) {
vis1[i]=1;
s1[pos]=i;
solve_lie(pos-1,0);
vis1[i]=0;
}
} else {
for(int i=1;i<=m;i++) {
if(vis1[i]) continue;
for(int j=i+1;j<=m;j++)
if(!vis1[j]&&lie[i][j]) {
vis1[i]=1;vis1[j]=1;
s1[pos]=i;s1[m-pos+1]=j;
solve_lie(pos-1,0);
vis1[i]=0;vis1[j]=0;
}
return;
}
}
}
void solve_hang(int pos,int op) {
if(flag) return;
if(pos==0) {solve_lie((m+1)/2,m&1);return;}
if(op) {
for(int i=1;i<=n;i++) {
vis2[i]=1;
s2[pos]=i;
solve_hang(pos-1,0);
vis2[i]=0;
}
} else {
for(int i=1;i<=n;i++) {
if(vis2[i]) continue;
for(int j=i+1;j<=n;j++)
if(!vis2[j]&&hang[i][j]) {
vis2[i]=1;vis2[j]=1;
s2[pos]=i;s2[n-pos+1]=j;
solve_hang(pos-1,0);
vis2[i]=0;vis2[j]=0;
}
return;
}
}
}
int main() {
num=read();T=read();
while(T--) {
n=read();m=read();
for(int i=1;i<=n;i++)
scanf("%s",mp[i]+1);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) {
static char x[N],y[N];
for (int k=1;k<=m;k++)
x[k]=mp[i][k],
y[k]=mp[j][k];
sort(x+1,x+m+1);
sort(y+1,y+m+1);
hang[i][j]=1;
for (int k=1;k<=m;k++)
if(x[k]!=y[k]) hang[i][j]=0;
}
for (int i=1;i<=m;i++)
for (int j=1;j<=m;j++) {
static char x[N],y[N];
for (int k=1;k<=n;k++)
x[k]=mp[k][i],
y[k]=mp[k][j];
sort(x+1,x+n+1);
sort(y+1,y+n+1);
lie[i][j]=1;
for (int k=1;k<=n;k++)
if (x[k]!=y[k]) lie[i][j]=0//排序判断字符串重构
}
flag=0;
solve_hang((n+1)/2,n&1);
if(!flag) puts("NO");
}
return 0;
}