• M × N Puzzle POJ


    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

    1 6 2
    4 0 3
    7 5 9
    10 8 11

    DOWN

    1 0 2
    4 6 3
    7 5 9
    10 8 11
    LEFT
    1 2 0
    4 6 3
    7 5 9
    10 8 11

    UP

    1 2 3
    4 6 0
    7 5 9
    10 8 11

     

    RIGHT

    1 2 3
    4 0 6
    7 5 9
    10 8 11

    UP

    1 2 3
    4 5 6
    7 0 9
    10 8 11
    UP
    1 2 3
    4 5 6
    7 8 9
    10 0 11

    LEFT

    1 2 3
    4 5 6
    7 8 9
    10 11 0

    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


    题意:给你一个m*n的矩阵,0代表空,0位置可以和上下左右位置交换,问是否可以变成1~m*n-1顺序排列且0在第m*n的位置的图,看上面例子。
    思路:这就是一个奇数码问题的扩展,我们将其看成一条链,将0去除。
    对于n的奇数码问题,我们知道若能从一个图转换成另一张图,只需要比较两个图的逆序对奇偶性是否相同即可。(上下交换,交换n-1个数,n-1为偶数,不影响逆序对奇偶性)
    对于n的偶数码问题,我们左右交换依然不增减逆序对,但是上下交换,将交换n-1个数,n-1为奇数,将改变逆序对奇偶性,我们需要判断 【一个图的逆序对+空位置行的差值】与【另一图的逆序对】
     1 #include<cstdio>
     2 #include<iostream>
     3 
     4 using namespace std;
     5 
     6 const int maxn = 1e6+6;
     7 int a[maxn];
     8 typedef long long ll;
     9 int Mergesort(int l,int r)
    10 {
    11     int mid = (l+r)/2;
    12     int b[r-l+5];
    13     int i=l,j=mid+1;
    14     int m=0;
    15     int cnt = 0;
    16     while(i <= mid && j <= r)
    17     {
    18         if(a[i] > a[j])
    19             b[m++] = a[j++],cnt += mid-i+1;
    20         else
    21             b[m++] = a[i++];
    22     }
    23     while(i <= mid)
    24     {
    25         b[m++] = a[i++];
    26     }
    27     while(j <= r)
    28     {
    29         b[m++] = a[j++];
    30     }
    31     m = 0;
    32     for(int i=l; i<=r; i++)
    33     {
    34         a[i] = b[m++];
    35     }
    36     return cnt;
    37 }
    38 
    39 void Merge(int l,int r,ll& ans)
    40 {
    41     if(l >= r)
    42         return;
    43     int mid = (l+r)/2;
    44     Merge(l,mid,ans);
    45     Merge(mid+1,r,ans);
    46     ans += Mergesort(l,r);
    47 }
    48 int n,m;
    49 int main()
    50 {
    51     while(~scanf("%d%d",&n,&m)&&n&&m)
    52     {
    53         int tmp;
    54         int cnt = 1;
    55         int row;
    56         for(int i=1; i<=n; i++)
    57         {
    58             for(int j=1; j<=m; j++)
    59             {
    60                 scanf("%d",&tmp);
    61                 if(tmp)
    62                     a[cnt++] = tmp;
    63                 else row = i;
    64             }
    65         }
    66         ll ans = 0;
    67         Merge(1,cnt-1,ans);
    68         int flag = 0;
    69         if(m&1){if((ans & 1) == 0)flag = 1;}
    70         else if((ans+n-row) % 2 == 0)flag = 1;
    71         if(flag)printf("YES
    ");
    72         else printf("NO
    ");
    73     }
    74 }
    View Code


  • 相关阅读:
    部署阿里云服务器流程小结
    爬虫中代理的设置问题介绍
    简析 __init__、__new__、__call__ 方法
    Python3.* 和Python2.*的区别
    解决k8s svcat ServiceInstance 删除不掉问题
    k8s dashboard部署,使用traefik暴露问题记录
    python中all函数得用法
    使用Hexo+github搭建个人博客大坑
    使用scrapy爬取百度招聘
    还是爬虫,使用的是selenium,爬取的是智联,爬取速度灰常慢...
  • 原文地址:https://www.cnblogs.com/iwannabe/p/10168994.html
Copyright © 2020-2023  润新知