Va 716-Commedia dell & amp; #39; arte (replacement)

Source: Internet
Author: User

Va 716-Commedia dell & #39; arte (replacement)

Link to the question: Ultraviolet A 716-Commedia dell 'arte

Given a three-dimensional octal digital, 0 represents an empty position and asks if it can be sorted back.

Solution: for the case where n is an odd number, consider the reverse logarithm of the three-dimensional octal digital to consider the case where the State goes down, except 0, and the even number, consider transferring the position of 0 to (n, the reverse logarithm of the corresponding sequence after the n, n) position. If the reverse logarithm is an even number, the odd number is not allowed.

#include 
  
   #include 
   
    #include 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;}
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.