题意:对于每一行最多只能交换2个数也可以不交换,或者交换某2列操作。最多只能有n+1次操作。问能不能使得最后每行的数都是1-m递增的。
#include<bits/stdc++.h>
using namespace std;
int n,m,a[21][21];
bool check()
{
for(int i=1;i<=n;i++)
{
int cnt=0;
for(int j=1;j<=m;j++)
{
if(a[i][j]!=j)cnt++;
}
if(cnt>2)return false;
}
return true;
}
int main()
{
cin>>n>>m;
bool flag=true;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
if(check())
{
cout<<"YES"<<endl;
return 0;
}
for(int i=1;i<=m;i++)
{
for(int j=i+1;j<=m;j++)
{
for(int k=1;k<=n;k++)
swap(a[k][i],a[k][j]);
if(check())
{
cout<<"YES"<<endl;
return 0;
}
for(int k=1;k<=n;k++)
swap(a[k][i],a[k][j]);
}
}
cout<<"NO"<<endl;
return 0;
}