The Eight Puzzle, among other sliding-tile puzzles, is one of the famous problems in artificial intelligence. Along with chess, tic-tac-toe and backgammon, it has been used to study search algorithms.

The Eight Puzzle can be generalized into an M × N Puzzle where at least one of M and N is odd. The puzzle is constructed with MN − 1 sliding tiles with each a number from 1 to MN − 1 on it packed into a M by N frame with one tile missing. For example, with M = 4 and N = 3, a puzzle may look like:

1 6 2
4 0 3
7 5 9
10 8 11

Let's call missing tile 0. The only legal operation is to exchange 0 and the tile with which it shares an edge. The goal of the puzzle is to find a sequence of legal operations that makes it look like:

1 2 3
4 5 6
7 8 9
10 11 0

The following steps solve the puzzle given above.

START

1 6 2
4 0 3
7 5 9
10 8 11

DOWN

1 0 2
4 6 3
7 5 9
10 8 11
LEFT
1 2 0
4 6 3
7 5 9
10 8 11

UP

1 2 3
4 6 0
7 5 9
10 8 11

 

RIGHT

1 2 3
4 0 6
7 5 9
10 8 11

UP

1 2 3
4 5 6
7 0 9
10 8 11
UP
1 2 3
4 5 6
7 8 9
10 0 11

LEFT

1 2 3
4 5 6
7 8 9
10 11 0

GOAL

Given an M × N puzzle, you are to determine whether it can be solved.

Input

The input consists of multiple test cases. Each test case starts with a line containing M and N (2 ≤ M, N ≤ 999). This line is followed by M lines containing N numbers each describing an M × N puzzle.

The input ends with a pair of zeroes which should not be processed.

Output

Output one line for each test case containing a single word YES if the puzzle can be solved and NO otherwise.

Sample Input

3 3
1 0 3
4 2 5
7 8 6
4 3
1 2 5
4 6 9
11 8 10
3 7 0
0 0

Sample Output

YES
NO 题意:给你一个m*n的矩阵,0代表空,0位置可以和上下左右位置交换,问是否可以变成1~m*n-1顺序排列且0在第m*n的位置的图,看上面例子。
思路:这就是一个奇数码问题的扩展,我们将其看成一条链,将0去除。
①对于n的奇数码问题,我们知道若能从一个图转换成另一张图,只需要比较两个图的逆序对奇偶性是否相同即可。(上下交换,交换n-1个数,n-1为偶数,不影响逆序对奇偶性)
②对于n的偶数码问题,我们左右交换依然不增减逆序对,但是上下交换,将交换n-1个数,n-1为奇数,将改变逆序对奇偶性,我们需要判断 【一个图的逆序对+空位置行的差值】与【另一图的逆序对】
 #include<cstdio>
#include<iostream> using namespace std; const int maxn = 1e6+;
int a[maxn];
typedef long long ll;
int Mergesort(int l,int r)
{
int mid = (l+r)/;
int b[r-l+];
int i=l,j=mid+;
int m=;
int cnt = ;
while(i <= mid && j <= r)
{
if(a[i] > a[j])
b[m++] = a[j++],cnt += mid-i+;
else
b[m++] = a[i++];
}
while(i <= mid)
{
b[m++] = a[i++];
}
while(j <= r)
{
b[m++] = a[j++];
}
m = ;
for(int i=l; i<=r; i++)
{
a[i] = b[m++];
}
return cnt;
} void Merge(int l,int r,ll& ans)
{
if(l >= r)
return;
int mid = (l+r)/;
Merge(l,mid,ans);
Merge(mid+,r,ans);
ans += Mergesort(l,r);
}
int n,m;
int main()
{
while(~scanf("%d%d",&n,&m)&&n&&m)
{
int tmp;
int cnt = ;
int row;
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
scanf("%d",&tmp);
if(tmp)
a[cnt++] = tmp;
else row = i;
}
}
ll ans = ;
Merge(,cnt-,ans);
int flag = ;
if(m&){if((ans & ) == )flag = ;}
else if((ans+n-row) % == )flag = ;
if(flag)printf("YES\n");
else printf("NO\n");
}
}
												

M × N Puzzle POJ - 2893(奇数码)的更多相关文章

  1. CH Round #72 奇数码问题[逆序对 观察]

    描述 你一定玩过八数码游戏,它实际上是在一个3*3的网格中进行的,1个空格和1~8这8个数字恰好不重不漏地分布在这3*3的网格中. 例如:5 2 81 3 _4 6 7 在游戏过程中,可以把空格与其上 ...

  2. poj2893 M*N puzzle 【n*m数码问题小结】By cellur925

    题目传送门 这个问题是来源于lydrainbowcat老师书上讲排序的一个扩展.当时讲的是奇数码问题,其实这种问题有两种问法:一种局面能否到另一种局面.到达目标局面的最小步数. 本文部分内容引用于ly ...

  3. AcWing:108. 奇数码问题(归并排序 + 逆序数)

    你一定玩过八数码游戏,它实际上是在一个3×3的网格中进行的,1个空格和1~8这8个数字恰好不重不漏地分布在这3×3的网格中. 例如: 5 2 8 1 3 _ 4 6 7 在游戏过程中,可以把空格与其上 ...

  4. 区间dp E - Multiplication Puzzle POJ - 1651

    E - Multiplication Puzzle  POJ - 1651 这个题目没有特别简单,但是也没有我想象之中的那么难,这个题目时区间dp,因为我们是要对区间进行考虑的. 但是呢,这个也和动态 ...

  5. POJ 2893 M × N Puzzle——八数码有解条件

    题意:给定M*N的数码图,问能否移动到最终状态 分析 有解的判定条件可见 八数码有解条件 值得一提的是,这道题求逆序对卡树状数组,只能用归并排序. #include<cstdio> #in ...

  6. POJ 2893 M × N Puzzle(树状数组求逆序对)

                                                               M × N Puzzle Time Limit: 4000MS   Memory ...

  7. POJ 2893 M × N Puzzle

    逆序对 n 数码问题的扩展 对于一个n * m 的问题来说,结论和 列数 m 奇偶有关 对于 m 是奇数来说 , 两个局面互相可达,当且仅当这两个局面按顺序写成一个数列,这个数列的逆序对数的奇偶性相同 ...

  8. HDU 3600 Simple Puzzle 归并排序 N*N数码问题

    先介绍八数码问题: 我们首先从经典的八数码问题入手,即对于八数码问题的任意一个排列是否有解?有解的条件是什么? 我在网上搜了半天,找到一个十分简洁的结论.八数码问题原始状态如下: 1 2 3 4 5 ...

  9. poj 1077-Eight(八数码+逆向bfs打表)

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

随机推荐

  1. Confluence 6 配置索引语言

    修改你 Confluence 的索引语言将有助于你提高搜索的准确性,如果你网站使用的主要语言是除了英语以外的其他语言. Confluence 可以支持下面语言的的内容索引: Arabic Brazil ...

  2. 学习Spring Boot:(一)入门

    微服务 现在微服务越来越火了,Spring Boot热度蹭蹭直升,自学下. 微服务其实是服务化思路的一种最佳实践方向,遵循SOA(面向服务的架构)的思路,各个企业在服务化治理上面的道路已经走得很远了, ...

  3. 前端javascript

    前端 JavaScript   javaScript----数据库jquery $(function(){ 执行代码   });  基本语法:$(selector).action() $(" ...

  4. LeetCode(99):恢复二叉搜索树

    Hard! 题目描述: 二叉搜索树中的两个节点被错误地交换. 请在不改变其结构的情况下,恢复这棵树. 示例 1: 输入: [1,3,null,null,2]   1   /  3   \   2 输出 ...

  5. PDF文件如何标注,怎么使用PDF标注工具

    我们在使用文件的时候需要给文件的部分添加标注,能够更加直观的了解文件,但是有很多小伙伴们对于PDF文件怎么添加标注都不知道,也不知道PDF标注工具要怎么使用,那么下面就跟大家分享一下怎么使用PDF标注 ...

  6. C语言实现split以某个字符分割一个字符串

    方式一: 使用strtok # include <string.h> # include <stdio.h> void split(char *src,const char * ...

  7. MySQL架构及SQL语句

    MySQL基础: 单进程多线程: 用户连接:连接线程 官方组件架构: MySQL的数据文件类型: 数据文件.索引文件 重做日志.撤销日志.二进制日志.错误日志.查询日志.慢查询日志.中继日志 MySQ ...

  8. C++ Primer 笔记——顺序容器

    1.标准库中定义了一些顺序容器,所有顺序容器都提供了快速顺序访问元素的能力. 2.如果容器的元素类型没有默认构造函数,那么在构造这个容器的时候不能只指定这个容器的数目,因为没有办法默认构造这些元素. ...

  9. 目标检测算法SSD之训练自己的数据集

    目标检测算法SSD之训练自己的数据集 prerequesties 预备知识/前提条件 下载和配置了最新SSD代码 git clone https://github.com/weiliu89/caffe ...

  10. 编写UEditor插件

    UE.registerUI('beijing', function (editor, uiName) { // 注册按钮执行时的command命令 editor.registerCommand(uiN ...