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 MySQL 问题解决

    如果 Confluence 提示没有 class 文件,你可能将你的 JDBC 驱动放置到了错误的文件夹. 如果你收到了下面的错误信息,请确认 confluenceuser 用户已经具有了所有需求的数 ...

  2. Confluence 6 使用 Velocity 宏

    当编辑自定义 Decorator 模板文件的时候,有一些宏可被用来定义页面中复杂或者多变的内容,例如菜单,链接等.你可以插入这些宏到你的模板中.更多的信息,请参考Working With Decora ...

  3. swift项目初始化并添加忽略文件Swift.ignore

    1 先去GitHub上去把最新的忽略文件下载下载 https://github.com/github/gitignore 2 然后找到Swift.gitignore  把里面的 pod 前面的# 删除 ...

  4. gnuradio 使用eclipse 编辑器记录

    第1步 - 首先安装eclipse 先去官网下载,然后解压  --->下载版本是C++/C 版---->解压--->打开--->help->eclipse marketp ...

  5. Windows Internals 笔记——线程局部存储区

    1.由于C/C++运行库是在多线程应用程序出现的许多年前设计的,因此运行库中的大多数函数是为单线程应用程序设计的. 2.当应用程序第一次调用_tcstok_s的时候该函数将传入的字符串地址保存在它自己 ...

  6. Command 'ifconfig' not found, but can be installed with: sudo apt install net-tools

    然后按照错误信息安安装网络工具: sudo apt install net-tools shl@shl-tx:~$ sudo apt install net-tools正在读取软件包列表... 完成正 ...

  7. ubuntu 手动更新源 以及使用sudo update与upgrade的作用及区别

    一.今天更新一下我的ubuntu系统,用了几个源发现不怎么好用 上网查了一下发现有说阿里云的源挺好用 然后我试了一下 下载速度还挺快,下面分享一下怎么手动添加源列表 1.最好先做一下备份 sudo c ...

  8. python pip install mysql-python报错

    报错: 下载地址: https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

  9. C# 把ABCD转换成数字

    每倒题得选项可能是多选或者单选. public static string LetterTransformationNum(string answer, int type) { string num ...

  10. MYSQL数据仓库infobright【备忘】

    Infobright是一个基于MySQL开发的开源数据仓库(Data Warehouse)软件,可作为MySQL的一个存储引擎来使用,SELECT查询与普通MySQL无区别. 优点:查询性能高:百万. ...