绝境求生(八数码有解无解的问题,求逆序)

问题 E: 【排序】绝境求生

时间限制: 1 Sec  内存限制: 64 MB
提交: 19  解决: 12

题目描述

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:

 


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:

 


The following steps solve the puzzle given above.
 



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

 

样例输入

复制样例数据

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

样例输出

YES
NO

 

结论:N*M矩阵中,若M为奇数->计算给出矩阵的逆序数和与初始状态的逆序数看奇偶性:奇偶相同则存在,反之不存在

                             若M为偶数->计算给出矩阵的逆序数为ans1,初始0所在行与给出的0所在行之间的行间距ans2,判断:

                             (ans1%2) ==(ans2%2),成立则存在,反之不存在

用树状数组维护

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

int n, m;
int cnt;
int c[1000005], a[1000005];
int ans;

int lowbit(int x){
	return x & (-x);
}

void add(int x){
	while(x <= cnt){
		c[x]++;
		x += lowbit(x);
	}
}

int sum(int x){
	int su = 0;
	while(x){
		su += c[x];
		x -= lowbit(x);
	}
	return su;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	while(scanf("%d %d", &n, &m) == 2){
		if(!n && !m) break;
		memset(c, 0, sizeof(c));
		int p, x, ans = 0;
		cnt = 0;
		for (int i = 1; i <= n; i++){
			for (int j = 1; j <= m; j++){
				scanf("%d", &x);
				if(x == 0) p = n - i;
				else{
					a[cnt++] = x;
				}
			}
		}
		for (int i = cnt - 1; i >= 0; i--){
			ans += sum(a[i]);
			//cout << a[i] << " " <<sum(a[i]) << endl;
			add(a[i]);
		}
		if(m & 1){
			if(ans % 2 == 0){
				printf("YES\n");
			}else{
				printf("NO\n");
			}
			continue;
		}
		if(ans % 2 == p % 2){
			printf("YES\n");
		}else{
			printf("NO\n");
		}
	}

	return 0;
}
/**/

 

全部评论

相关推荐

首先讲三个故事,关于牛客的事件一:2024年,牛客上有一对高学历情侣,求职方向与我当时一致,都是嵌入式方向。他们恰好是我的朋友,专业能力和学历背景都很扎实,也因此拿到了不少优质offer。和很多求职者一样,他们把offer情况整理后发在平台上,本意是记录与交流,但很快引发了争议。有声音指责他们“集邮”“不释放名额”,认为这种展示本身就是一种炫耀。最终讨论失控,当事人删除内容,事件也很快被遗忘。事件二:小红书评论区,一条评价获得了不少共鸣:“感觉牛客就是当年那群做题区毕业了开始找工作还收不住那股味,颇有一种从年级第一掉到年纪第二后抱怨考不上大学的味道”,这条评论被水印里这个同学转发到牛客后,评论...
小型域名服务器:当看到别人比自己强的时候,即便这是对方应得的,很多人会也下意识的歪曲解构对方的意图,来消解自己在这本就不存在的比较中输掉的自信,从而平白制造出很多无谓的争论。比如你会在空余时间来写优质好文,而我回家只会暗区突围,那么我就可以作为键盘侠在这里评论你是不是XXXXXXXX。即便我自己都知道这是假的,但只要这没那么容易证伪,那么当你开始回应的时候,脏水就已经泼出去了,后面可能会有更多的人带着情绪来给我点赞,而毫不关注你写的文章内容本身是啥了。
SAGIMA牛马咖啡
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务