康托展开:
X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0!
ai为整数,并且0<=ai<i(1<=i<=n)
应用实例:
{1,2,3,4,...,n}的排列总共有n!种,将它们从小到大排序,怎样知道其中一种排列是有序序列中的第几个?
如 {1,2,3} 按从小到大排列一共6个:123 132 213 231 312 321。想知道321是{1,2,3}中第几个大的数。
这样考虑:第一位是3,小于3的数有1、2 。所以有2*2!个。再看小于第二位,小于2的数只有一个就是1 ,所以有1*1!=1 所以小于32
的{1,2,3}排列数有2*2!+1*1!=5个。所以321是第6个大的数。2*2!+1*1!是康托展开。
康托展开的逆运算:
{1,2,3,4,5}的全排列已经从小到大排序,要找出第16个数:
1. 首先用16-1得到15
2. 用15去除4! 得到0余15
3. 用15去除3! 得到2余3
4. 用3去除2! 得到1余1
5. 用1去除1! 得到1余0
有0个数比它小的数是1
所以第一位是1
有2个数比它小的数是3,但1已经在之前出现过了所以是4
有1个数比它小的数是2,但1已经在之前出现过了所以是3
有1个数比它小的数是2,但1,3,4都出现过了所以是5
最后一个数只能是2
所以这个数是14352
Nine tiles, each with a number from 1 to 9 on it, are packed into a 3 by 3 frame. Your task is to arrange the tiles so that they are ordered as
1 2 3
4 5 6
7 8 9
At each step, you can do the following operation to the tiles: Choose 2 by tiles, rotate the tiles in clockwise order. For example:
1 2 3-->4 1 3
4 5 6-->5 2 6
7 8 9-->7 8 9
or
1 2 3-->1 2 3
4 5 6-->4 8 5
7 8 9-->7 9 6
Write a program to find the minimum number of steps.
Input
Input contains multiple test cases.
each test case is a description of a configuration of the nine tiles. The description is just a list of the tiles in there initial positions, with the rows listed from top to bottom, and from left to right within a row, where the tiles are represented by numbers 1 to 9. For example:
9 8 7
6 5 4
3 2 1
is described by this list:
9 8 7 6 5 4 3 2 1
Output
Output the minimum number of steps on a single line for each test case.
Sample Input
1 2 3 4 5 6 7 8 9
4 1 3 5 2 6 7 8 9
Sample Output
0
3
X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0!
ai为整数,并且0<=ai<i(1<=i<=n)
应用实例:
{1,2,3,4,...,n}的排列总共有n!种,将它们从小到大排序,怎样知道其中一种排列是有序序列中的第几个?
如 {1,2,3} 按从小到大排列一共6个:123 132 213 231 312 321。想知道321是{1,2,3}中第几个大的数。
这样考虑:第一位是3,小于3的数有1、2 。所以有2*2!个。再看小于第二位,小于2的数只有一个就是1 ,所以有1*1!=1 所以小于32
的{1,2,3}排列数有2*2!+1*1!=5个。所以321是第6个大的数。2*2!+1*1!是康托展开。
再举个例子:1324是{1,2,3,4}排列数中第几个大的数:第一位是1小于1的数没有,是0个,0*3!,第二位是3小于3的数有1和2,但1已经在第一位了,所以只有一个数2,1*2! 。第三位是2小于2的数是1,但1在第一位,所以有0个数,0*1!,所以比1324小的排列有0*3!+1*2!+0*1!=2个,1324是第三个大数。
int fac[] = {1,1,2,6,24,120,720,5040,40320}; //i的阶乘为fac[i]
/* 康托展开.
{1...n}的全排列由小到大有序,s[]为第几个数 */
int KT(int n, int s[])
{
int i, j, t, sum;
sum = 0;
for (i=0; i<n; i++)
{
t = 0;
for (j=i+1; j<n; j++)
if (s[j] < s[i])
t++;
sum += t*fac[n-i-1];
}
return sum+1;
}
康托展开的逆运算:
{1,2,3,4,5}的全排列已经从小到大排序,要找出第16个数:
1. 首先用16-1得到15
2. 用15去除4! 得到0余15
3. 用15去除3! 得到2余3
4. 用3去除2! 得到1余1
5. 用1去除1! 得到1余0
有0个数比它小的数是1
所以第一位是1
有2个数比它小的数是3,但1已经在之前出现过了所以是4
有1个数比它小的数是2,但1已经在之前出现过了所以是3
有1个数比它小的数是2,但1,3,4都出现过了所以是5
最后一个数只能是2
所以这个数是14352
/* 康托展开的逆运算.
{1...n}的全排列,中的第k个数为s[] */
void invKT(int n, int k, int s[])
{
int i, j, t, vst[8]={0};
k--;
for (i=0; i<n; i++)
{
t = k/fac[n-i-1];
for (j=1; j<=n; j++)
if (!vst[j])
{
if (t == 0) break;
t--;
}
s[i] = j;
vst[j] = 1;
k %= fac[n-i-1];
}
}
Problem Description
Nine tiles, each with a number from 1 to 9 on it, are packed into a 3 by 3 frame. Your task is to arrange the tiles so that they are ordered as
1 2 3
4 5 6
7 8 9
At each step, you can do the following operation to the tiles: Choose 2 by tiles, rotate the tiles in clockwise order. For example:
1 2 3-->4 1 3
4 5 6-->5 2 6
7 8 9-->7 8 9
or
1 2 3-->1 2 3
4 5 6-->4 8 5
7 8 9-->7 9 6
Write a program to find the minimum number of steps.
Input
Input contains multiple test cases.
each test case is a description of a configuration of the nine tiles. The description is just a list of the tiles in there initial positions, with the rows listed from top to bottom, and from left to right within a row, where the tiles are represented by numbers 1 to 9. For example:
9 8 7
6 5 4
3 2 1
is described by this list:
9 8 7 6 5 4 3 2 1
Output
Output the minimum number of steps on a single line for each test case.
Sample Input
1 2 3 4 5 6 7 8 9
4 1 3 5 2 6 7 8 9
Sample Output
0
3
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int Max=362880;
int kt[]={1,2,6,24,120,720,5040,40320};
int vis[Max];
struct node
{
int a[9],step;
};
node que[Max];
int cantor(int a[])
{
int ret=0;
for(int i=0;i<8;i++)
for(int j=i+1;j<9;j++) if(a[i]>a[j]) ret+=kt[7-i];
return ret;
}
int mov[4][9]={{1,4,2,0,3,5,6,7,8},{0,2,5,3,1,4,6,7,8},{0,1,2,4,7,5,3,6,8},{0,1,2,3,5,8,6,4,7}};//按顺时针方向的逆方向变换;
void bfs()
{
int front=0,rear=1,i,j;
memset(vis,255,sizeof(vis));
for(i=0;i<9;i++) que[0].a[i]=i+1;
que[0].step=0;
vis[cantor(que[0].a)]=0;
while(front<rear)
{
node nd=que[front],newnd;
newnd=nd;
newnd.step=nd.step+1;
for(i=0;i<4;i++)
{
for(j=0;j<9;j++)
newnd.a[j]=nd.a[mov[i][j]];
int num=cantor(newnd.a);
if(vis[num]<0)
{
vis[num]=newnd.step;
que[rear++]=newnd;
}
}
front++;
}
}
int main()
{
//freopen("b.txt","r",stdin);
int num[9];
bfs();
while(scanf("%d",&num[0])==1)
{
for(int i=1;i<9;i++)
scanf("%d",&num[i]);
printf("%d\n",vis[cantor(num)]);
}
return 0;
}