POJ 2893 M × N Puzzle(树状数组求逆序对)
| Time Limit: 4000MS | Memory Limit: 131072K | |
| Total Submissions: 4112 | Accepted: 1140 |
Description
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 |
|
DOWN |
|
LEFT ⇒ |
|
UP |
|
… |
||||||||||||||||||||||||||||||||||||||||||||||||
|
RIGHT |
|
UP |
|
UP ⇒ |
|
LEFT |
|
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
题意:8数码问题的升级,就是通过移动空格(用0代替)使得原来状态变成有序的1234......0,不过,这题是N*M数码。
题解:N*M都挺大的,搜索必然不行。考虑终态,实际就是逆序数为0的状态,然后四种操作方式分为:左右移动,对原序列的逆序数不影响;上下移动,如下:
-------------0***********
***********x-------------
x是任意数,现在要把x移上去,那么***********中,假设有a个大于x,b个小于x,那么移动之后逆序数就会加上一个b-a,x所能影响的也就是这些罢了,除此之外,其他都不变。
接着,如果列数为偶数,那么******的个数就是奇数,b,a奇偶性互异,b-a为奇数,所以移动一次后,原序列的逆序数的奇偶性变了。
考虑到最后0会移动到最后一行,所以奇偶性会改变n-i次(i为0的行数),只需判断最后是否是偶数即可。
反之,如果列数为奇数,那么******的个数就是偶数,b,a奇偶性相同,b-a为偶数,所以移动一次后,原序列的逆序数的奇偶性没变。
因为无论怎么移,奇偶性都不变,所以说一开始初态的奇偶性就必须与末态一致。
题意来自http://blog.csdn.net/tmeteorj/article/details/8530105
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 2e9
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = *+;
const int M = 4e5+;
int n,m,tot=,cnt=;
int head[N],ans[N];
int tree[N];
int a[N];
void add(int k,int num) {
while(k<=*-) {
tree[k]+=num;
k+=k&(-k);
}
}
int Sum(int k) {
int sum=;
while(k>) {
sum+=tree[k];
k-=k&(-k);
}
return sum;
}
int main() { while(scanf("%d%d",&n,&m),n||m) {
int x,y,t,s=,nu=;
for(int i=; i<=n; i++)
for(int j=; j<=m; j++) {
scanf("%d",&t);
if(t==)
x=i,y=j;
else
a[nu++]=t;
}
met(tree,);
for(int i=nu-; i>=; i--) {
s+=Sum(a[i]-);
add(a[i],);
}
if(m&)
if(s&)puts("NO");
else puts("YES");
else if(((n-x)^s)&) puts("NO");
else puts("YES");
}
return ;
}
POJ 2893 M × N Puzzle(树状数组求逆序对)的更多相关文章
- POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对
http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- [NOIP2013提高&洛谷P1966]火柴排队 题解(树状数组求逆序对)
[NOIP2013提高&洛谷P1966]火柴排队 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相 ...
- [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)
[NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...
- 【bzoj2789】[Poi2012]Letters 树状数组求逆序对
题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...
- “浪潮杯”第九届山东省ACM大学生程序设计竞赛(重现赛)E.sequence(树状数组求逆序对(划掉))
传送门 E.sequence •题意 定义序列 p 中的 "good",只要 i 之前存在 pj < pi,那么,pi就是 "good": 求删除一个数, ...
- 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)
2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...
- NOIP 2013 洛谷P1966 火柴排队 (树状数组求逆序对)
对于a[],b[]两个数组,我们应选取其中一个为基准,再运用树状数组求逆序对的方法就行了. 大佬博客:https://www.cnblogs.com/luckyblock/p/11482130.htm ...
- poj3067 Japan 树状数组求逆序对
题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...
- 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)
链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...
随机推荐
- python中Scikit-Learn机器学习模块
Scikit-Learn是基于python的机器学习模块,基于BSD开源许可证.这个项目最早由DavidCournapeau 在2007 年发起的,目前也是由社区自愿者进行维护. Scikit-Lea ...
- html中button自动提交表单?
在ie中,button默认的type是button,而其他浏览器和W3C标准中button默认的属性都是submit
- win7文件夹图标中多了一把小锁打不开文件夹怎么办?
win7文件夹图标中多了一把小锁打不开文件夹怎么办?解决办法一:右击目录→取得管理员权限!该方法适用于win7旗舰版.解决办法二:右击目录→属性→安全→高级→选择everyone→更改权限→勾上完全访 ...
- CSS和Js样式属性的对照关系
注意:1.在使用js时,如newTD.style.paddingLeft="200";(错误),正确生效的写法是newTD.style.paddingLeft="200p ...
- unity3d Light Probe Group图解超详细使用方法
原创文章如需转载请注明:转载自http://blog.csdn.net/qq617119142 第一步,创建一个Panel 和 三个 cube,搭建成如下图形状 第二步,创建2个点光源,一个为绿灯,一 ...
- html知识2
1.超链接 语法:<a href "" target="打开方式" name="页面锚点名称">链接文字或者图片</a&g ...
- 使用Python创建简单的HTTP和FTP服务
不管工作中还是其他场合,经常会有文件分享的需求,比如自己下了一个4GB的游戏,同事下了一个800MB的软件,其他人如果也需要这些文件,显然直接分享是最快捷.最方便.最环保的方式了,如果再重新下,既浪费 ...
- js调用父窗口中的方法
window.open调用父窗口中的方法 回调函数: function fun9(ex){ alert(ex); } 调用语句: window.open("RoomSelecter.htm? ...
- Shannon entropy
Shannon entropy is one of the most important metrics in information theory. Entropy measures the unc ...
- 【仿真】【modelsim】:verilog功能仿真流程
一.编写verilog源文件,在diamond中编译.编写testbench文件.在diamond设置中将仿真工具设置为modelsim,运行仿真向导 二.自动进入modelsim, 编译全部 运行仿 ...