hdu 6048 Puzzle 思维(8数码问题

传送门

Puzzle

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 92    Accepted Submission(s): 48


Problem Description
A Jigsaw puzzle contains N*M-1 pieces of jigsaws in a N rows*M columns rectangular board.Each jigsaw has a distinct number from 1 to N*M-1.Li is a naughty boy,he wants to arrange the board in his unique way.At the beginning,he picks all N*M-1 jigsaws out and put them on the table and then he will put them back to the board respecting the following steps:
1.Sorting all the remaining jigsaws on the table in ascending order.
2.Picking out the 1st ,the P+1 th ,the 2*P+1 th,......the n*P+1 th jigsaws and put them back to the blank area in the board one by one from the top row to the bottom row,from the left column to the right column.
3.if there are jigsaws remained on the table,back to step 1.
After he arranging the board,it’s obvious that there’s only one blank area located at the bottom-right corner.
Your task is to make the numbers on jigsaws sorted with every row and every column in ascending order(From left to right,top to bottom),and the blank area should be located at the bottom-right corner in the end.Each step you can move the blank area’s neighboring jigsaws(which share a common side with the blank area) towards the blank area.It’s really a difficult question,so you need to write a program to judge whether it is possible to complete the task.



 

Input
The first line contains an integer T(T<=100),which represents the number of test cases.
Following T lines,each line contains three integers N,M,P(2<=N,M<=1000;1<=P<=N*M-2).
 

Output
For each test case,print “YES” in a separate line if it is possible to complete the task ,otherwise please print “NO”.
 

Sample Input
  
  
3 3 2 3 3 2 4 999 999 1
 

Sample Output
  
  
YES NO YES
 

Source
 

Recommend
liuyiding

给出一个n*m的方格,以及1到n*m-1的数字,一开始按照一定的规则选择每个数,从上到下,从左到右填入方格中,最后留一个空格子。 

选数的规则为,每次选择剩余数中,第1个,第p+1个,第p*2+1个…,选完一轮后,去除选择的数,剩下的数继续按照这个规则来选。 

如果方格中的空格子可以通过和相邻的数字交换位置来移动,问最后是否可以得到1到n*m-1这些数字按照从左到右从上到下的顺序放在方格中(右下角是空格)。

   这个题,哇,不看题解根本就是一头雾水。题意很简单,就是小时候玩的拼图游戏,有一块空白。
   然后问你最后能不能拼成12345```n顺序的图,如果可以就YES,不行就NO。题解给的是逆序数问 
   题。可以发现,之前我们的任何操作都不会改变逆序对的个数,因为考虑:
   1.空格的横向移动:排列不发生改变,逆序对奇偶性不改变;
   2.空格的竖向移动:相当于做了(m-1)次交换,由于最后空格回到了右下角,即竖向交换的次数为
   偶数次,设为2K,则总体交换次数为2K*(m-1)次,也不改变逆序对的奇偶性。
   最后就变成了2*2的最后空格的逆序数的比较。也就是说看整个构造串的逆序数是否为奇偶。如果
   为偶就能为逆序数为0也就是顺序递增的情况,反之则不能。另外根据观察。1-p+1-2p+1 ```那么
   2-p 一定在p+1的后面所以有p-1个贡献 但是当后面出现2-p+2-2p+2 则会少一个p+1 因为之前已经
   出现了p+1了,也就是只有p-2的贡献。然后得出结论:

#include <bits/stdc++.h>
using namespace std;
int T;  
long long res,n,m,P;  
int main()  
{  
    scanf("%d",&T);  
    while (T--) 
    {  
        res=0;  
        scanf("%I64d%I64d%I64d",&n,&m,&P);  
        n=n*m-1;  
        while (n>P) 
        {  
            long long t=(n+P-1)/P;  
            res+=t*(t-1)*(P-1)/2LL;  
            n-=t;  
        }  
        puts(res%2?"NO":"YES");  
    }  
    return 0;  
}  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值