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;
}
SGU 139. Help Needed! 逆序数,奇偶性,分析 难度:0的更多相关文章
- sgu 139 Help Needed!
题意:16数码是否有解? 先计算展开成一维后逆序对.如果0在最后一行,那么逆序偶时有解.4*4时(n为偶)0的位置上升一行,逆序对+3或-1(奇偶性变化).(n为奇时+2或+0,不变) #includ ...
- SGU 139.Help Needed!
题意: 判断15数码问题是否有解. 如果0的偏移量和逆序对个数同奇偶则无解. 因为目标状态的偏移量为0,逆序对为15,而0移动的时候偏移量±1,逆序对的改变量为也为奇数. 这就使得偏移量和逆序对数始终 ...
- 51 Nod 1107 斜率小于0的连线数量 (转换为归并求逆序数或者直接树状数组,超级详细题解!!!)
1107 斜率小于0的连线数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 二维平面上N个点之间共有C(n,2)条连线.求这C(n,2)条线中斜率小于0的线 ...
- HDU - 1394 Minimum Inversion Number(线段树求逆序数---点修改)
题意:给定一个序列,求分别将前m个数移到序列最后所得到的序列中,最小的逆序数. 分析:m范围为1~n,可得n个序列,求n个序列中最小的逆序数. 1.将序列从头到尾扫一遍,用query求每个数字之前有多 ...
- hdu1043 经典的八数码问题 逆向bfs打表 + 逆序数
题意: 题意就是八数码,给了一个3 * 3 的矩阵,上面有八个数字,有一个位置是空的,每次空的位置可以和他相邻的数字换位置,给你一些起始状态 ,给了一个最终状态,让你输出怎么变换才能达到目的. 思路: ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- hdu 1394 求一个序列的最小逆序数 单点增 区间求和
题目的意思就好比给出一个序列 如:0 3 4 1 2 设逆序数初始n = 0: 由于0后面没有比它小的,n = 0 3后面有1,2 n = 2 4后面有1,2,n = 2+2 = 4: 所以该序列逆序 ...
- 1.7 逆序数与归并排序[inversion pairs by merge sort]
[本文链接] http://www.cnblogs.com/hellogiser/p/inversion-pairs-by-merge-sort.html [题目] 编程之美1.7光影切割问题可以进一 ...
- hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)
题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...
随机推荐
- 7.1 - CRM系统
一.简介 crm 客户关系管理软件 ( Customer Relationship Management ) ( 详细内容 ) stark组件(仿admin组件)( 详细内容 ) rbac组件(基于角 ...
- andriod(十七)蓝牙profile
1. 蓝牙profile Bluetooth的一个很重要特性,就是所有的Bluetooth产品都无须实现全部的Bluetooth规范.为了更容易的保持Bluetooth设备之间的兼容, Bluetoo ...
- Mybatis三剑客之mybatis-generator配置
mybatis插件在这里: 然后把generatorConfig.xml文件放在resources下: <?xml version="1.0" encoding=" ...
- Python学习开发资源大全列表
1 机器学习和计算机视觉 Crab:灵活.快速的推荐引擎 gensim:人性化的话题建模库 hebel:GPU 加速的深度学习库 NuPIC:智能计算 Numenta 平台 pattern:Pytho ...
- 前端迭代取出 后台map返回的数据
<body> <div th:each="map : ${response}"> <span th:text="${map.key}&quo ...
- spark 调优概述
分为几个部分: 开发调优.资源调优.数据倾斜调优.shuffle调优 开发调优: 主要包括这几个方面 RDD lineage设计.算子的合理使用.特殊操作的优化等 避免创建重复的RDD,尽可能复用同一 ...
- 免费美女视频聊天,多人视频会议功能加强版本(Fms3和Flex开发(附源码))
Flex,Fms3系列文章导航 Flex,Fms3相关文章索引 本篇是视频聊天,会议开发实例系列文章的第4篇,该系列所有文章链接如下: http://www.cnblogs.com/aierong/a ...
- 前端学习笔记之CSS文档流
先引用一段W3C的文档: 9.3 Positioning schemes In CSS 2.1, a box may be laid out according to three positionin ...
- poj3421 X-factor Chains(重复元素的全排列)
poj3421 X-factor Chains 题意:给定正整数$x(x<=2^{20})$,求$x$的因子组成的满足任意前一项都能整除后一项的序列的最大长度,以及满足最大长度的子序列的个数. ...
- Python3.x:open()文件操作
Python3.x:open()文件操作 open/文件操作: #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 f=ope ...