【NOIP2017提高A组模拟7.8】为了爱情 八数码拓展

3 篇文章 0 订阅

题意:给你一个n*n的方格,分别填上1-n^2-1,有一个位子空着的,问你是否能通过移动来达到目标状态
1 2 3
4 5 6
7 8 9

n<=100
http://blog.csdn.net/u010398265/article/details/50987577
学习了一波= =挺好的结论。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e5+5;
int a[N],tmp[N],ans,n;
inline void mergesort(int l,int r)
{
    if (l==r)return;
    int mid=(l+r)>>1;
    int cnt=l,h1=l,h2=mid+1;
    mergesort(l,mid);
    mergesort(mid+1,r);
    while (h1<=mid&&h2<=r)
    {
        while (a[h1]>a[h2])
        {
            tmp[cnt++]=a[h2];
            h2++;
            ans+=mid-h1+1;
            if (h2>r)break;
        }
        tmp[cnt++]=a[h1];
        h1++;
    }
    fo(i,h1,mid)tmp[cnt++]=a[i];
    fo(i,h2,r)tmp[cnt++]=a[i];
    fo(i,l,r)a[i]=tmp[i];
}
int main()
{
    int cas;
    scanf("%d",&cas);
    while (cas--)
    {
        int tot=0;
        scanf("%d",&n);
        fo(i,1,n)
        {
            fo(j,1,n)
            {
                scanf("%d",&a[++tot]);
                if (a[tot]==0)tot--;
            }
        }   
        ans=0;
        mergesort(1,n*n-1);
        if (ans%2==0)printf("You still have a chance.\n");
        else printf("You are destined to be single.\n");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值