題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6620
思路:
其實題目中的120步以內這個要求可以忽略。
先求一下逆序對(相關題目:https://www.luogu.org/problem/P1908)
然後根據數字華容道必然有解的規律只存在於如下3個細分情況:
若格子列數爲奇數,則逆序數必須爲偶數;
若格子列數爲偶數,且逆序數爲偶數,則當前空格所在行數與初始空格所在行數的差爲偶數;
若格子列數爲偶數,且逆序數爲奇數,則當前空格所在行數與初始空格所在行數的差爲奇數。
AC代碼:
#include<bits/stdc++.h>
using namespace std;
int a[100];
int sum;
int tmp[100];
void psort(int b,int e)//歸併排序
{
if(b==e)
return;
int mid=(b+e)/2,i=b,j=mid+1,k=b;
psort(b,mid),psort(mid+1,e);
while(i<=mid&&j<=e)
if(a[i]<=a[j])
tmp[k++]=a[i++];
else{
tmp[k++]=a[j++];
sum+=mid-i+1;
}
while(i<=mid)
tmp[k++]=a[i++];
while(j<=e)
tmp[k++]=a[j++];
for(int l=b;l<=e;l++)
a[l]=tmp[l];
}
int main()
{
int t;
cin>>t;
while(t--)
{
int cnt=0;
int t;
int flag;
for(int i=1;i<=16;i++){
cin>>t;
if(t){//不計入0
a[++cnt]=t;
}
else
{
flag=(i+3)/4;
}
}
sum=0;
psort(1,15);
if(sum%2==0&&(4-flag)%2==0||sum%2==1&&(4-flag)%2==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
/*
2
1 2 3 4
5 6 7 8
9 10 0 12
13 14 11 15
1 2 3 4
5 6 7 8
9 10 11 12
13 15 14 0
*/
數字華容道規律參考:https://www.jianshu.com/p/1c1849d876b2
類似題目:
http://acm.hdu.edu.cn/showproblem.php?pid=6048
(題解:https://blog.csdn.net/weixin_43499182/article/details/93138300)