Just an Old Puzzle(2019杭电多校训练赛第四场-找规律题)

Just an Old Puzzle(2019杭电多校训练赛第四场-找规律题)

judge:hduoj 6620
source:2019 Multi-University Training Contest 4
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 262144/262144 K (Java/Others)

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 &lt; = T &lt; = 1 0 5 ) T (1 &lt;= T &lt;= 10^5) T(1<=T<=105) 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

题意

类似于小时候玩的一种玩具,就是有个4*4的方格,其中填有1~15的数字,还有一个空格。问你能否通过让格子不断地利用那个空格重新排列数字,使之达到题中所给的有序排列。

这题一开始以为是个搜索题,后来听大佬说有规律……

代码

#include <bits/stdc++.h>
#define _for(i, a) for(int i = 0; i < (a); i++)
using namespace std;

int main() {
    //freopen("in.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a[20];
        int num = 0;
        _for(i, 16) {
            scanf("%d", &a[i]);
            if (!a[i]) num += (6 - i / 4 - i % 4);
            _for(j, i) {
                if (a[i] < a[j]) {
                    num++;
                }
            }
        }
        printf("%s\n", num % 2 ? "Yes" : "No");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值