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;
}

  

SGU 139. Help Needed! 逆序数,奇偶性,分析 难度:0的更多相关文章

  1. sgu 139 Help Needed!

    题意:16数码是否有解? 先计算展开成一维后逆序对.如果0在最后一行,那么逆序偶时有解.4*4时(n为偶)0的位置上升一行,逆序对+3或-1(奇偶性变化).(n为奇时+2或+0,不变) #includ ...

  2. SGU 139.Help Needed!

    题意: 判断15数码问题是否有解. 如果0的偏移量和逆序对个数同奇偶则无解. 因为目标状态的偏移量为0,逆序对为15,而0移动的时候偏移量±1,逆序对的改变量为也为奇数. 这就使得偏移量和逆序对数始终 ...

  3. 51 Nod 1107 斜率小于0的连线数量 (转换为归并求逆序数或者直接树状数组,超级详细题解!!!)

    1107 斜率小于0的连线数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题   二维平面上N个点之间共有C(n,2)条连线.求这C(n,2)条线中斜率小于0的线 ...

  4. HDU - 1394 Minimum Inversion Number(线段树求逆序数---点修改)

    题意:给定一个序列,求分别将前m个数移到序列最后所得到的序列中,最小的逆序数. 分析:m范围为1~n,可得n个序列,求n个序列中最小的逆序数. 1.将序列从头到尾扫一遍,用query求每个数字之前有多 ...

  5. hdu1043 经典的八数码问题 逆向bfs打表 + 逆序数

    题意: 题意就是八数码,给了一个3 * 3 的矩阵,上面有八个数字,有一个位置是空的,每次空的位置可以和他相邻的数字换位置,给你一些起始状态 ,给了一个最终状态,让你输出怎么变换才能达到目的. 思路: ...

  6. POJ2299 Ultra-QuickSort(归并排序求逆序数)

    归并排序求逆序数   Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  7. hdu 1394 求一个序列的最小逆序数 单点增 区间求和

    题目的意思就好比给出一个序列 如:0 3 4 1 2 设逆序数初始n = 0: 由于0后面没有比它小的,n = 0 3后面有1,2 n = 2 4后面有1,2,n = 2+2 = 4: 所以该序列逆序 ...

  8. 1.7 逆序数与归并排序[inversion pairs by merge sort]

    [本文链接] http://www.cnblogs.com/hellogiser/p/inversion-pairs-by-merge-sort.html [题目] 编程之美1.7光影切割问题可以进一 ...

  9. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)

    题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...

随机推荐

  1. vue - 计算属性、表单输入绑定

    计算属性 computed:{} <!DOCTYPE html> <html> <head> <title></title> </he ...

  2. 解决Eclipse中新建jsp文件总是以ISO8859-1编码问题

    eclipse --> window -->Preferences-->web-->jsp-->utf-8

  3. Mirror--镜像用户同步

    --=========================================--在镜像搭建后,在主库服务器上创建登录,并在数据库上建立对应用户,--数据库中用户被同步到镜像数据库中,但登录是 ...

  4. JAVA 线程状态转换

    Thread类中State枚举定义: public enum State { /** * Thread state for a thread which has not yet started. */ ...

  5. 使用递归打印二叉树的左视图 java

    使用递归打印二叉树的左视图 java package com.li.jinRiTouTiao; public class PrintLeftView { static class TreeNode{ ...

  6. ABP常见问题

    System.Data.SqlClient.SqlException (0x80131904): 'OFFSET' 附近有语法错误 解决方案:最新的ABP默认支持的是sql2012以上的版本,对于之前 ...

  7. zw版【转发·台湾nvp系列Delphi例程】HALCON SetIcon2

    zw版[转发·台湾nvp系列Delphi例程]HALCON SetIcon2 procedure TForm1.Button1Click(Sender: TObject);var img : HUnt ...

  8. Python:导入numpy报错 No module named numpy

    Numpy是python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比python自身的嵌套列表结构要高效的多.但是在使用numpy时可能会出错(如上图). 解决办法:下载安装对应版本 ...

  9. CentOS7搭建Gitlab详细过程

    1.参见Gitlab官网说明 原文地址:https://about.gitlab.com/install/#centos-7   1.安装并配置必要的依赖项 在CentOS 7(和RedHat / O ...

  10. 面试官问:JS的this指向

    前言 面试官出很多考题,基本都会变着方式来考察this指向,看候选人对JS基础知识是否扎实.读者可以先拉到底部看总结,再谷歌(或各技术平台)搜索几篇类似文章,看笔者写的文章和别人有什么不同(欢迎在评论 ...