HDU 3459 Rubik 2x2x2 second-order cube restoration (IDA *)

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = contents by --- cxlove

 

The restoration of the second-order cube is complicated and abstract.

The minimum number of questions is not required, but Ida * should write the shortest number and the smallest Lexicographic Order.

Let's talk about one of the characteristics of this second-order cube. Although there are three types of rotations, there is always one that is not moving, that is, the one that is leaning back to the left. Since it does not move, the color of the three faces in the cube is determined. The color of the three faces can be used to determine the color of the other three faces.

In this way, we can obtain an estimation function H, but it is too weak to pruning. The colors of the six faces are different from those of the target. Each rotation can change 8 positions (total number + 7)/8;

The code is very difficult to write, but I guess this is not the way it is, it is quite long to look at other people's code, but the efficiency difference is great, continue to want to cut.

# Include <iostream> # include <cstdio> # include <cstring> # include <queue> # include <cmath> # include <string> # include <vector> # include <algorithm> # include <map> # include <set> # define INF 1 <30 using namespace STD; struct node {int X, Y;} block [8] [3], side [6] [4]; // records three faces of the eight cubes of the cube, four coordinates of each face: Char STR [10] [10]; char each_col [6]; // The color of each face, which is top, left, front, right, and back, bool flag; int ans [100]; int depth; void Init () {// obtain the coordinates of each plane of each cube and bloc of each coordinate of each plane of the cube K [0] [0]. X = 3; block [0] [0]. y = 2; block [0] [1]. X = 3; block [0] [1]. y = 1; block [0] [2]. X = 4; block [0] [2]. y = 2; block [1] [0]. X = 3; block [1] [0]. y = 3; block [1] [1]. X = 3; block [1] [1]. y = 4; block [1] [2]. X = 4; block [1] [2]. y = 3; block [2] [0]. X = 2; block [2] [0]. y = 2; block [2] [1]. X = 2; block [2] [1]. y = 1; block [2] [2]. X = 1; block [2] [2]. y = 2; block [3] [0]. X = 2; block [3] [0]. y = 3; block [3] [1]. X = 1; block [3] [1]. y = 3; block [3] [2]. X = 2; block [3] [2]. y = 4; block [4] [0]. X = 3; Bloc K [4] [0]. y = 0; block [4] [1]. X = 5; block [4] [1]. y = 2; block [4] [2]. X = 3; block [4] [2]. y = 7; block [5] [0]. X = 5; block [5] [0]. y = 3; block [5] [1]. X = 3; block [5] [1]. y = 5; block [5] [2]. X = 3; block [5] [2]. y = 6; block [6] [0]. X = 0; block [6] [0]. y = 2; block [6] [1]. X = 2; block [6] [1]. y = 7; block [6] [2]. X = 2; block [6] [2]. y = 0; block [7] [0]. X = 0; block [7] [0]. y = 3; block [7] [1]. X = 2; block [7] [1]. y = 5; block [7] [2]. X = 2; block [7] [2]. y = 6; side [0] [0]. X = 0; side [0] [0]. y = 2; side [0] [1]. X = 0; side [0] [1]. y = 3; side [0] [2]. X = 1; side [0] [2]. y = 2; side [0] [3]. X = 1; side [0] [3]. y = 3; side [1] [0]. X = 2; side [1] [0]. y = 0; side [1] [1]. X = 2; side [1] [1]. y = 1; side [1] [2]. X = 3; side [1] [2]. y = 0; side [1] [3]. X = 3; side [1] [3]. y = 1; side [2] [0]. X = 2; side [2] [0]. y = 2; side [2] [1]. X = 2; side [2] [1]. y = 3; side [2] [2]. X = 3; side [2] [2]. y = 2; side [2] [3]. X = 3; side [2] [3]. y = 3; side [3] [0]. X = 2; side [3] [0]. y = 4; side [3] [1]. X = 2; side [3] [1]. y = 5; side [3] [2]. X = 3; S IDE [3] [2]. y = 4; side [3] [3]. X = 3; side [3] [3]. y = 5; side [4] [0]. X = 2; side [4] [0]. y = 6; side [4] [1]. X = 2; side [4] [1]. y = 7; side [4] [2]. X = 3; side [4] [2]. y = 6; side [4] [3]. X = 3; side [4] [3]. y = 7; side [5] [0]. X = 4; side [5] [0]. y = 2; side [5] [1]. X = 4; side [5] [1]. y = 3; side [5] [2]. X = 5; side [5] [2]. y = 2; side [5] [3]. X = 5; side [5] [3]. y = 3;} Char get_color (int A, int B, int c) {// obtain the color of each face, and use the three faces for (INT I = 0; I <8; I ++) {If (STR [block [I] [0]. x] [block [I] [0]. y] = EA Ch_col [a] & STR [block [I] [1]. x] [block [I] [1]. y] = each_col [B] & STR [block [I] [2]. x] [block [I] [2]. y]! = Each_col [c]) return STR [block [I] [2]. x] [block [I] [2]. y]; If (STR [block [I] [1]. x] [block [I] [1]. y] = each_col [a] & STR [block [I] [0]. x] [block [I] [0]. y] = each_col [B] & STR [block [I] [2]. x] [block [I] [2]. y]! = Each_col [c]) return STR [block [I] [2]. x] [block [I] [2]. y]; If (STR [block [I] [0]. x] [block [I] [0]. y] = each_col [a] & STR [block [I] [2]. x] [block [I] [2]. y] = each_col [B] & STR [block [I] [1]. x] [block [I] [1]. y]! = Each_col [c]) return STR [block [I] [1]. x] [block [I] [1]. y]; If (STR [block [I] [2]. x] [block [I] [2]. y] = each_col [a] & STR [block [I] [0]. x] [block [I] [0]. y] = each_col [B] & STR [block [I] [1]. x] [block [I] [1]. y]! = Each_col [c]) return STR [block [I] [1]. x] [block [I] [1]. y]; If (STR [block [I] [1]. x] [block [I] [1]. y] = each_col [a] & STR [block [I] [2]. x] [block [I] [2]. y] = each_col [B] & STR [block [I] [0]. x] [block [I] [0]. y]! = Each_col [c]) return STR [block [I] [0]. x] [block [I] [0]. y]; If (STR [block [I] [2]. x] [block [I] [2]. y] = each_col [a] & STR [block [I] [1]. x] [block [I] [1]. y] = each_col [B] & STR [block [I] [0]. x] [block [I] [0]. y]! = Each_col [c]) return STR [block [I] [0]. x] [block [I] [0]. y] ;}} void change_x (char maze [10] [10]) {// rotate char TMP on the X axis; TMP = maze [2] [4]; maze [2] [4] = maze [2] [5]; maze [2] [5] = maze [3] [5]; maze [3] [5] = maze [3] [4]; maze [3] [4] = TMP; TMP = maze [1] [3]; maze [1] [3] = maze [2] [6]; maze [2] [6] = maze [5] [3]; maze [5] [3] = maze [3] [3]; maze [3] [3] = TMP; TMP = maze [0] [3]; maze [0] [3] = maze [3] [6]; maze [3] [6] = maze [4] [3]; maze [4] [3] = maze [2] [3]; maze [2] [3] = TMP;} void ch Ange_y (char maze [10] [10]) {// rotate char TMP on the Y axis; TMP = maze [2] [0]; maze [2] [0] = maze [2] [6]; maze [2] [6] = maze [2] [4]; maze [2] [4] = maze [2] [2]; maze [2] [2] = TMP; TMP = maze [2] [1]; maze [2] [1] = maze [2] [7]; maze [2] [7] = maze [2] [5]; maze [2] [5] = maze [2] [3]; maze [2] [3] = TMP; TMP = maze [0] [2]; maze [0] [2] = maze [0] [3]; maze [0] [3] = maze [1] [3]; maze [1] [3] = maze [1] [2]; maze [1] [2] = TMP;} void change_z (char maze [10] [10]) {// Z axis rotating char TMP; TMP = maze [2] [1]; maze [2] [1] = maze [1] [3]; maze [1] [3] = maze [3] [4]; maze [3] [4] = maze [4] [2]; maze [4] [2] = TMP; TMP = maze [3] [1]; maze [3] [1] = maze [1] [2]; maze [1] [2] = maze [2] [4]; maze [2] [4] = maze [4] [3]; maze [4] [3] = TMP; TMP = maze [2] [2]; maze [2] [2] = maze [2] [3]; maze [2] [3] = maze [3] [3]; maze [3] [3] = maze [3] [2]; maze [3] [2] = TMP;} int get_h (char maze [10] [10]) {int ret = 0; For (INT I = 0; I <6; I ++) {for (Int J = 0; j <4; j ++) if (maze [side [I] [J]. x] [side [I] [J]. Y]! = Each_col [I]) RET ++;} return (Ret + 7)/8 ;}; void idastar (char maze [10] [10], int tmp_depth) {If (FLAG) return; If (get_h (maze)> tmp_depth) return; If (tmp_depth = 0) {flag = true; return;} For (INT I = 0; I <3; I ++) {If (FLAG) return; char temp [10] [10]; for (INT I = 0; I <6; I ++) for (Int J = 0; j <8; j ++) temp [I] [J] = maze [I] [J]; if (I = 0) change_x (temp); else if (I = 1) change_y (temp); else change_z (temp); ans [tmp_depth] = I; idastar (temp, tmp_depth-1 );}} in T Main () {Init (); While (scanf ("% s", STR [0])! = EOF) {for (INT I = 1; I <6; I ++) scanf ("% s", STR [I]); if (strcmp (STR [2], "...... ") = 0) break; each_col [1] = STR [3] [0]; each_col [5] = STR [5] [2]; each_col [4] = STR [3] [7]; // One of the blocks remains inactive, you can determine the color of the three faces in the final result. each_col [0] = get_color (, 5); each_col [2] = get_color (, 4 ); each_col [3] = get_color (4,5, 1); flag = false; For (depth = get_h (STR); depth ++) {idastar (STR, depth ); if (FLAG) {for (INT I = depth; I> 0; I --) printf ("% C", ANS [I] + 'X '); printf ("\ n"); break ;}} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.