Just an Old Puzzle【数学(逆序对)】

Just an Old Puzzle
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2749 Accepted Submission(s): 875

Problem Description
You are given a 4 × 4 grid, which consists of 15 number cells and an empty cell.
All numbers are unique and ranged from 1 to 15.
In this board, the cells which are adjacent with the empty cell can move to the empty cell.
Your task is to make the input grid to the target grid shown in the figure below.
In the following example (sample input), you can get the target grid in two moves.
在这里插入图片描述
Input
The first line contains an integer T (1 <= T <= 10^5) denoting the number of test cases.
Each test case consists of four lines each containing four space-separated integers, denoting the input grid. 0 indicates the empty cell.

Output
For each test case, you have to print the answer in one line.
If you can’t get the target grid within 120 moves, then print ‘No’, else print ‘Yes’.

Sample Input
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

Sample Output
Yes
No

题目大意:
先输入一个数 T T T,代表有 T T T组测试数据,对于每组测试数据输入一个16宫格,问其在120步内是否能达到目标状态。

解题思路:
由于问的是是否能在120步内达到目标状态,对于任意一个能够完成的16宫格,其在120步内均能完成,所以可将其转换为输入的16宫格是否能够完成的问题,此时我们需要知道两个性质:
性质一:
对于任何两个16宫格,当其0所在的位置相同时,我们可以将两个16宫格分别铺开,分别计算其逆序对数,当两个宫格逆序对数奇偶性相同时,则两个宫格能相互转换。
性质二:
对于任何一个16宫格,其移动奇数步会改变其逆序对数的奇偶性,偶数步不会改变
因此对于此题,我们只需先计算原16宫格的逆序对数,以及其点0移动到右下角所需的步数,判断其奇偶性即可

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int arr[20];
int step[17]={0,6,5,4,3,5,4,3,2,4,3,2,1,3,2,1,0};
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    //ios::sync_with_stdio(0),cin.tie(0);
    int T;
    scanf("%d",&T);
    while(T--) {
      	int nape;
    	for(int i=1;i<=16;i++) {
    		scanf("%d",&arr[i]);
    		if(arr[i]==0) nape=step[i];
    	}
    	int ans=0;
    	for(int i=1;i<16;i++) {
    		for(int j=i+1;j<=16;j++) {
    			if(arr[i]>arr[j]) ans++;
    		}
    	}
    	if(ans%2==1&&nape%2==0) printf("Yes\n");
    	else if(ans%2==0&&nape%2==1) printf("Yes\n");
    	else printf("No\n");
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值