uva 716 - Commedia dell' arte(置换)

题目链接:uva 716 - Commedia dell‘ arte

题目大意:给定一个三维的八数码,0表示空的位置,问说是否可以排回有序序列。

解题思路:对于n为奇数的情况,考虑三维八数码对应以为状态下去除0的时候逆序对数,偶数的情况下,考虑将0的位置转移到(n,n,n)位置后对应序列的逆序对数。如果逆序对数为偶数即为可以,奇数不可以。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 1e6+5;

int n, arr[maxn], t[maxn];
int x, y, z;

ll merge_sort (int l, int r, int* a, int* b) {
    if (l == r)
        return 0;

    ll ret = 0;

    int mid = (r + l) / 2;
    int p = l, q = mid+1, mv = l;

    ret = merge_sort(l, mid, a, b) + merge_sort(mid + 1, r, a, b);

    while (p <= mid || q <= r) {
        if (q > r || (p <= mid && a[p] < a[q]))
            b[mv++] = a[p++];
        else {
            ret += mid - p + 1;
            b[mv++] = a[q++];
        }
    }

    for (int i = l; i <= r; i++)
        a[i] = b[i];
    return ret;
}

bool judge () {
    if (n&1) {
        ll ret = merge_sort(0, n*n*n-1, arr, t) - (z * n * n + x * n + y - 1);
        return (ret&1) == 0;
    }

    while (z != n - 1) {
        int p = z * n * n + x * n + y;
        z++;
        int q = z * n * n + x * n + y;
        swap(arr[p], arr[q]);
    }

    while (x != n - 1) {
        int p = z * n * n + x * n + y;
        x++;
        int q = z * n * n + x * n + y;
        swap(arr[p], arr[q]);
    }

    while (y != n - 1) {
        int p = z * n * n + x * n + y;
        y++;
        int q = z * n * n + x * n + y;
        swap(arr[p], arr[q]);
    }
    ll ret = merge_sort(0, n*n*n-2, arr, t);
    return (ret&1) == 0;
}

int main () {
    int cas;
    scanf("%d", &cas);
    while (cas--) {

        scanf("%d", &n);
        for (int k = 0; k < n; k++) {
            int Z = k * n * n;
            for (int i = 0; i < n; i++) {
                int X = i * n;
                for (int j = 0; j < n; j++) {
                    int tmp = Z + X + j;
                    scanf("%d", &arr[tmp]);
                    if (arr[tmp] == 0) {
                        x = i; y = j; z = k;
                    }
                }
            }
        }

        printf("%s\n", judge() ? "Puzzle can be solved." : "Puzzle is unsolvable.");
    }
    return 0;
}

uva 716 - Commedia dell' arte(置换),布布扣,bubuko.com

uva 716 - Commedia dell' arte(置换)

时间: 2024-10-12 22:42:53

uva 716 - Commedia dell' arte(置换)的相关文章

UVA 716 - Commedia dell&#39; arte(三维N数码问题)

UVA 716 - Commedia dell' arte 题目链接 题意:给定一个三维的n数码游戏,要求变换为按顺序,并且最后一个位置是空格,问能否变换成功 思路:和二维的判定方法一样,因为z轴移动,等于交换N^2 - 1次,y轴移动等于交换N - 1次,x轴移动不变,逆序对的奇偶性改变方式不变. 那么n为偶数的时候,逆序对为偶数可以,为奇数不行 n为奇数时候,看空格位置的y轴和z轴分别要走的步数和逆序对的奇偶性相不相同,相同可以,不相同步行 代码: #include <cstdio> #i

UVA - 10733 The Colored Cubes (置换)

All 6 sides of a cube are to becoated with paint. Each side is is coated uniformly with one color. When a selectionof n different colors of paint is available, how many different cubes can youmake? Note that any two cubes are onlyto be called "differ

uva 11330 - Andy&#39;s Shoes(置换)

题目链接:uva 11330 - Andy's Shoes 题目大意:小andy有很多鞋,穿完到处丢,后来他把所有鞋都放回鞋架排成一排,保证了鞋的左右交替,但是颜色混了.问说他至少移动多少次可以将鞋分类好. 解题思路:对应奇数位置为左鞋,偶数位置为右鞋,一双鞋只有一只左鞋和一只右鞋,保证不换左变鞋子,以左鞋的位置为基准换右边鞋子,对应右边鞋子的位置即为一个置换,将置换的循环分解为x个互不相干的循环,ans=n-x #include <cstdio> #include <cstring&g

UVA 11330 - Andy&#39;s Shoes(置换分解)

UVA 11330 - Andy's Shoes 题目链接 题意:andy有很多双鞋子,每双鞋子有一个编号,现在他把鞋子左右左右放回去,可是不能保证所有鞋子左边和右边是同一编号,现在要求用最少的交换次数,使得所有鞋子左右编号相同 思路:置换的分解,固定左边的鞋子,这样右边的鞋子就可以看成是放在哪个位置,然后根据这个求出每个循环的长度,最后每个循环长度-1的总和就是答案 代码: #include <cstdio> #include <cstring> int t, n, vis[10

uva 12103 - Leonardo&#39;s Notebook(置换)

题目链接:uva 12103 - Leonardo's Notebook 题目大意:给出26个字母的置换,问是否存在一个置换A,使得A2=B 解题思路:将给定置换分解成若干个不相干的循环,当循环的长度n为奇数时,可以由两个循环长度为n的循环的乘积得来,也可以由两个循环长度为2n的拆分而来:对于长度n为偶数的,只能由两个循环长度为2n的拆分而来,所以判断是否存在有循环长度为偶数的个数是奇数个即可. #include <cstdio> #include <cstring> #inclu

uva 1156 - Pixel Shuffle(模拟+置换)

题目链接:uva 1156 - Pixel Shuffle 题目大意:给定一个N*N的黑白位图,有7种操作,并且对应在指令后加上'-'即为操作的逆,给定N和一系列操作,(从最后一个开始执行),问说这一套指令需要执行多少次才能形成循环. 解题思路:模拟指令执行后获得一个置换,分解成若干的循环,各个循环长度的最小公倍数即使答案. #include <cstdio> #include <cstring> #include <algorithm> using namespace

UVA - 11077 Find the Permutations (置换)

Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It iswell-known that the lower bound of swap based sorting is nlog(n).It means that the best possible sorting algorithm will take at least W(nlog(n))swaps

UVA 10294 Arif in Dhaka (置换polya)

[题目链接]:click here~~ [题目大意]: 给你一串珠子(连接成了一个环),共有n个珠子组成,你有t种颜色,现在你来给这个珠子染色,问染成项链有多少种方法?染成手镯有多少种方法?在项链里,经过顺时针旋转后相同的算一个,在手镯里,经过顺时针旋转或者沿着对称轴兑换后一样的算一个.即不同之处在于项链不能够反转,而手镯可以反转. [思路]: 首先,我们来看看两个很有用的关于置换的定理,第一个就是Burnside 描述为:对于置换f,一种着色方案s经过一种置换后不变,则称这种着色方案s是f的不

UVA - 1156 Pixel Shuffle (置换+模拟)

Description Shuffling the pixels in a bitmap image sometimes yields random looking images. However, by repeating the shuffling enough times, one finally recovers the original images. This should be no surprise, since ``shuffling" means applying a one