ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

玩具(Toy)

2021-11-27 21:04:31  阅读:304  来源: 互联网

标签:Toy int 玩具 next ZC id dis


清华OJ——数据结构与算法实验(中国石油大学)

玩具(Toy)


Description

ZC God is best at logical reasoning. One day, he talks about his childhood digital toys.

The toy is like a Rubik's cube, but not a Rubik's cube. Specifically, it is not a 3 * 3 * 3 structure, but a 4 * 2 structure.

According to the play of the toy, we can repeatedly transform it in the following three ways:

A. Exchange the upper and lower lines. For example, the result of the transformation of Figure (a) is shown in Figure (b).

B. Loop right shift (ZC God knows what this means from an early age). For example, the result of the transformation of Figure (b) is shown in Figure (c).

C. The center rotates clockwise. For example, the result of the transformation of Figure (c) is shown in Figure (d).

ZC God is a genius in this respect. He often has a hand that has not dried his nose, the other hand has quickly restored the toy in any state to the initial state shown in Figure (a). In the year when the material was extremely scarce, ZC God had only one such toy; today, the material is extremely rich, and you have many toys in different states. Now, please restore them all.

Input

The first line is a positive integer, which is the total number of Rubik's cube toys you have.

In each one of the following N lines, 8 positive integers, i.e. an arrangement of 1~8, are included, indicating the current state of the toy.

Here, the representation rule of the cube state is that the first four numbers represent the first line of the cube from left to right, and the last four numbers represent the second line from right to left. For example, the initial state is expressed as "1 2 3 4 5 6 7 8".

Output

A total of N lines, each line containing an integer, in turn corresponding to the minimum number of transform needs to be performed to restore each toy.

In particular, if a toy is not recoverable, the corresponding line outputs -1.

Example

Input

2
1 2 3 4 5 6 7 8
8 6 3 5 4 2 7 1

Output

0
2

Restrictions

For 60% of the data, N = 1

For 100% of the data,1 <= N <= 1,000

Time: 1 sec

Memory: 20 MB

Hints

State transition diagram and its search

描述

ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具。

该玩具酷似魔方,又不是魔方。具体来说,它不是一个3 * 3 * 3的结构,而是4 * 2的结构。

按照该玩具约定的玩法,我们可反复地以如下三种方式对其做变换:

A. 交换上下两行。比如,图(a)经此变换后结果如图(b)所示。

B. 循环右移(ZC神从小就懂得这是什么意思的)。比如,图(b)经此变换后结果如图(c)所示。

C. 中心顺时针旋转。比如,图(c)经此变换后结果如图(d)所示。

ZC神自小就是这方面的天才,他往往是一只手还没揩干鼻涕,另一只手已经迅速地将处于任意状态的玩具复原至如图(a)所示的初始状态。物质极其匮乏的当年,ZC神只有一个这样的玩具;物质极大丰富的今天,你已拥有多个处于不同状态的玩具。现在,就请将它们全部复原吧。

输入

第一行是一个正整数,即你拥有的魔方玩具总数N。

接下来共N行,每行8个正整数,是1~8的排列,表示该玩具的当前状态。

这里,魔方状态的表示规则为:前四个数自左向右给出魔方的第一行,后四个数自右向左给出第二行。比如,初始状态表示为“1 2 3 4 5 6 7 8”。

输出

共N行,各含一个整数,依次对应于复原各玩具所需执行变换的最少次数。

特别地,若某个玩具不可复原,则相应行输出-1。

样例

见英文题面

限制

对于60%的数据,N = 1

对于100%的数据,1 <= N <= 1,000

时间:1 sec

空间:20MB

提示

状态转换图及其搜索

 

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 using namespace std;
  5 
  6 int fac[]= {1, 1, 2, 6, 24, 120, 720, 5040, 40320}; //阶乘
  7 
  8 int f(int x)
  9 {
 10     int a[9],k=0;
 11     while(x)
 12     {
 13         a[k++]=x%10;
 14         x/=10;
 15     }
 16 //    for(int i=0;i<k;i++)printf("%d",a[i]);
 17 //    printf("\n");
 18     int ans=0,tmp;
 19     for(int i=0; i<k; i++)
 20     {
 21         tmp=0;//记录有几个比它小的数
 22         for(int j=i+1; j<k; j++)
 23         {
 24             if(a[j]<a[i])tmp++;
 25         }
 26         ans+=tmp*fac[k-i-1];
 27     }
 28     return ans;
 29 }
 30 
 31 int dis[45050]={0};
 32 int q[45050],c1=0,c2=0;
 33 int getnum(char a[])
 34 {
 35     int ans=0;
 36     for(int i=0;i<8;i++)
 37     {
 38         ans*=10;
 39         ans+=a[i]-'0';
 40     }
 41     return ans;
 42 }
 43 
 44 void bfs()
 45 {
 46     c1=0;
 47     c2=1;
 48     q[0]=12345678;
 49     dis[f(12345678)]=0;
 50     
 51     while(c1!=c2)
 52     {
 53         int now=q[c1++];
 54         
 55         
 56         int next;
 57         char a[10],b[10];
 58         sprintf(a,"%d",now);
 59         now=f(now);
 60         
 61         for(int i=7;i>=0;i--)b[i]=a[7-i];
 62     
 63         next=getnum(b);
 64             //printf("aaaa");
 65         int id=f(next);
 66         if(dis[id]==-1)
 67         {
 68             dis[id]=dis[now]+1;
 69             q[c2++]=next;
 70         }
 71         //58763214  a
 72         //87654321  b
 73         b[0]=a[1];
 74         b[1]=a[2];
 75         b[2]=a[3];
 76         b[3]=a[0];
 77         b[4]=a[7];
 78         b[5]=a[4];
 79         b[6]=a[5];
 80         b[7]=a[6];
 81         next=getnum(b);
 82         id=f(next);
 83         if(dis[id]==-1)
 84         {
 85             dis[id]=dis[now]+1;
 86             q[c2++]=next;
 87         }
 88         
 89         //51863724 a
 90         //58763214 b
 91         b[0]=a[0];
 92         b[1]=a[2];
 93         b[2]=a[5];
 94         b[3]=a[3];
 95         b[4]=a[4];
 96         b[5]=a[6];
 97         b[6]=a[1];
 98         b[7]=a[7];
 99         next=getnum(b);
100         id=f(next);
101         if(dis[id]==-1)
102         {
103             dis[id]=dis[now]+1;
104             q[c2++]=next;
105         }
106     }
107 }
108 
109 
110 int main()
111 {
112     memset(dis,-1,sizeof(dis));
113     bfs();
114     
115     int n;
116     scanf("%d",&n);
117     while(n--)
118     {
119         int now=0;
120         for(int i=0;i<8;i++)
121         {
122             int a;
123             scanf("%d",&a);
124             now*=10;
125             now+=a;
126         }
127         
128         printf("%d\n",dis[f(now)]);
129     }    
130     return 0;
131 }

 

标签:Toy,int,玩具,next,ZC,id,dis
来源: https://www.cnblogs.com/sylvia1111/p/15613044.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有