poj 2893 M × N Puzzle(八数码可行解)树状数组求解
来源:互联网 发布:淘宝假面骑士铠武腰带 编辑:程序博客网 时间:2024/06/05 08:14
某状态的奇偶性定义为逆序对(不包括0的)总数的奇偶性。
此题目终态为偶数
首先,0的左右移动不改变奇偶性。
N为奇数,上下移动不改变奇偶性,故逆序数为偶的YES
N为偶数,上下移动逆序数变化为±1,此时还要考虑0的竖直距离,逆序数%2 == 距离%2 时YES
此题目终态为偶数
首先,0的左右移动不改变奇偶性。
N为奇数,上下移动不改变奇偶性,故逆序数为偶的YES
N为偶数,上下移动逆序数变化为±1,此时还要考虑0的竖直距离,逆序数%2 == 距离%2 时YES
/*********************************************** * Author: fisty * Created Time: 2015/1/31 16:15:51 * File Name : poj2893.cpp *********************************************** */#include <iostream>#include <cstring>#include <deque>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <vector>#include <cstdio>#include <bitset>#include <algorithm>using namespace std;#define Debug(x) cout << #x << " " << x <<endl#define MAX_N 1000010const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int> P;int a[MAX_N];int bit[MAX_N];int m,n,k;void add(int i, int x){ while(i <= k){ bit[i] += x; i += i & (-i); }}int sum(int i){ int sum = 0; while(i > 0){ sum += bit[i]; i -= i & (-i); } return sum;}int solve(){ int ans = 0; for(int i = 0;i < k; i++){ add(a[i], 1); ans += i + 1 - sum(a[i]); } return ans;}int main() { //freopen("in.txt", "r", stdin); cin.tie(0); ios::sync_with_stdio(false); int n, m; while(cin >> m >> n){ int step = 0; k = 0; if(!n && !m) break; memset(bit, 0, sizeof(bit)); //memset(a, 0, sizeof(a)); for(int i = 0;i < m; i++){ for(int j = 0;j < n; j++){ int s; cin >> s; if(s == 0){ step = m - i - 1; }else{ a[k++] = s; } } } int ans = solve(); if(n & 1) step = 0; if(step % 2 == ans % 2) cout << "YES" << endl; else cout << "NO" << endl; } return 0;}
0 0
- poj 2893 M × N Puzzle(八数码可行解)树状数组求解
- POJ 2893 M × N Puzzle(逆序对-BIT)
- [逆序对] poj 2893 M × N Puzzle
- POJ 2893 M × N Puzzle 笔记
- poj 2893 hdu 3600 M*N数码问题
- 八数码问题,Puzzle
- poj 3131 Cubic Eight-Puzzle (双向bfs+深度控制 立体八数码)
- BFS求解N数码(python)
- poj2893 M × N Puzzle
- hdu 3600 Simple Puzzle 判断N 数码是否有解
- POJ 3221:Diamond Puzzle(六数码 反向bfs)
- IDA* 解八数码问题: POJ 1077
- poj 1077 八数码
- poj 1077 八数码
- poj 1077 八数码
- CDOJ 414 Eight Puzzle 八数码
- poj 1036 n!/(n-m)!*m!
- 暴力枚举之八皇后问题(可行解个数,打印可行解纵坐标,棋盘有洞,n皇后)
- JavaScript判断浏览器类型及版本
- 学习
- jquery中的empty()与remove()区别
- Windows下使用GNU编译器
- [转]解决Codeforces访问慢的本地方案
- poj 2893 M × N Puzzle(八数码可行解)树状数组求解
- php文件下载函数
- jquery 的focusin跟focusout方法的使用
- BP神经网络的数据分类
- poj1753
- gson的@Expose注解和@SerializedName注解
- 配置jk_mod注意事项
- 二分法求根
- 混响音效