zoukankan      html  css  js  c++  java
  • SGU 139. Help Needed! 逆序数,奇偶性,分析 难度:0

    139. Help Needed!

    time limit per test: 0.25 sec. 
    memory limit per test: 4096 KB

     

    Little Johnny likes puzzles a lot! Just a few days ago, he found out about the 'traditional' 4x4 puzzle. For this puzzle, you have all the numbers from 0 to 15 arranged in 4 rows and 4 columns. You are allowed to switch two adjacent elements (horizontally or vertically), only if one of them has the value 0. The purpose of the puzzle is to reach the following final state:

                                 1  2  3  4 
                                 5  6  7  8 
                                 9 10 11 12 
                                13 14 15  0

    Given the initial state of the puzzle, you have to decide whether there exists a sequence of moves which brings the puzzle into the final state.

     

    Input

    The input will consist of  4 lines, each of them containing 4 integers, describing the initial state of the puzzle.

     

    Output

    For every initial state, you should print "YES" if the final state can be reached after several moves or "NO", if such a thing is impossible.

     

    Sample Input #1

    1 2 3 4
    5 6 7 8
    9 10 11 0
    13 14 15 12
    

    Sample Output #1

    YES
    

    Sample Input #2

    2 1 3 4
    5 6 7 8
    9 10 11 12
    0 13 14 15
    

    Sample Output #2

    NO
    首先得到逆序数状态,移动0可以+1,-1逆序对或者+3,-3逆序对.只要逆序数奇偶性和必须走的步数相同,那么就一定能走到逆序对为0的情况(设0为16)
    #include <cstdio>
    using namespace std;
    int des[4][4];
    int main(){
        int aim=0;
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                scanf("%d",des[i]+j);
                if(des[i][j]==0){
                    aim=6-i-j;
                    des[i][j]=16;
                }
            }
        }
        for(int i=0;i<16;i++){
            for(int j=0;j<i;j++){
                if(*((int *)des+i)<*((int *)des+j)){
                    aim++;
                }
            }
        }
        if(aim&1)puts("NO");
        else puts("YES");
        return 0;
    }
    

      

  • 相关阅读:
    谷粒商城学习——P122 es分词&安装ik分词
    谷粒商城学习——P125 springboot 整合es
    谷粒商城学习——P70 概念SPU&SKU&规格参数&销售属性
    某大学渗透实录
    第五届强网杯全国网络安全挑战赛writeup
    首届北京大学信息安全综合能力竞赛writeup
    深入诈骗团队
    首届"鹤城杯"CTF网络安全挑战赛 初赛writeup
    陇原战"疫"2021网络安全大赛writeup
    2021第二届“天翼杯”网络安全攻防大赛writeup
  • 原文地址:https://www.cnblogs.com/xuesu/p/4041953.html
Copyright © 2011-2022 走看看