zoukankan      html  css  js  c++  java
  • Just an Old Puzzle(2019多校1007)

    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

     结论:拼图可解问题:当前空白的行距的奇偶性与当前序列的逆序数的奇偶性的抑或值来判断

    代码:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<vector>
    #include<cmath>
    
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    int a[25];
    int cc[25];
    int main()
    {
        int T;
        cin>>T;
        int k=0;
    
        while(T--)
        {
            int cnt=0;
            for(int t=0;t<4;t++)
            {
                for(int j=0;j<4;j++)
                {
                    scanf("%d",&a[t*4+j]);
                    if(a[t*4+j]!=0)
                    {
                        cc[cnt++]=a[t*4+j];
                    }
                    if(a[t*4+j]==0)
                    {
                        k=t;
                    }
                }
            }
        int sum=0;
        for(int t=0;t<15;t++)
        {
            for(int j=t+1;j<15;j++)
            {
                if(cc[j]<cc[t])
                {
                    sum++;
                }
            }
        }
        
        if(((3-k)%2)^(sum%2)==0)
        {
            puts("Yes");
        }
        else
        {
            puts("No");
        }    
        }
        return 0;
    }
  • 相关阅读:
    监控Linux系统性能命令---sar
    用SecureCRT来上传和下载文件 rz sz
    CentOS7 Firewall NAT 及端口映射
    CentOS 修改主机名
    CentOS 6.X如何更改网卡名称
    MySQL数据操作
    mysql如何修改数据表
    Zabbix图形中中文字体显示方块
    Linux虚拟机模板的创建
    Java web项目JXl导出excel,(从eclipse上移动到tomact服务器上,之路径更改)
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11278588.html
Copyright © 2011-2022 走看看