The Rotation Game (POJ 2286 search ida*)

Source: Internet
Author: User

Language:DefaultThe Rotation Game
Time Limit: 15000MS Memory Limit: 150000K
Total Submissions: 5573 Accepted: 1878

Description

The rotation game uses a # shaped board, which can hold for pieces of square blocks (see Fig.1). The blocks is marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind.

Initially, the blocks is placed on the board randomly. Your task is to move the blocks so, the eight blocks placed in the center square has the same symbol marked. There is only one type of valid move, which are to rotate one of the four lines, each consisting of seven blocks. That was, six blocks in the line was moved towards the head by one block and the head block was moved to the end of the line . The eight possible moves is marked with capital letters A-H. Figure 1 illustrates, consecutive moves, move A and Mo ve C from some initial configuration.

Input

The input consists of no more than-test cases. Each test case have only one line that contains numbers, which is the symbols of the blocks in the initial configuratio N. The rows of blocks is listed from top to bottom. For each row the blocks is listed from left to right. The numbers is separated by spaces. For example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There is no blank lines between cases. There is a line containing a single ' 0 ' after the last test case that ends the input.

Output

For the test case, you must output of the lines. The first line contains all the moves needed to reach the final configuration. Each move was a letter, ranging from ' a ' to ' H ', and there should not being any spaces between the letters on the line. If no moves is needed, output ' no moves needed ' instead. In the second line, you must output the symbol of the blocks in the center square after these moves. If there is several possible solutions, you must output the one that uses the least number of moves. If there is still more than one possible solution, you must output the solution that's smallest in dictionary order for T He letters of the moves. There is no need to output blank lines between cases.

Sample Input

1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 31 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 30

Sample Output

Ac2ddhh2

Source

Shanghai 2004



Test instructions: There are numbers in 24 positions, can be moved, each time a 7 number of translation, ask how to move the center of the 8 squares for the same number, output scheme and the final center of the number.

Idea: ida*, there are eight operations, mainly this move operation is not good, open an auxiliary array record moving position relationship. Each time the center is moved, a number is changed to construct H ().

Code:

#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include < algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include < vector> #include <set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define Pi  ACOs ( -1.0) #define EPS 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b;  i++) #define FREE (i,a,b) for (i = A, I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A i > B; i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d         ", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d ", &a, &b, &c) #define PF Printf#define DBG PF ("hi\n") typedef long long ll;using namespace std; #define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 30;cons t int maxn = 2005;const INT MAXM = 200010;const int N = 1005;int move[8][14]={{1,3,3,7,7,12,12,16,16,21,21,23,23,1},{ 2,4,4,9,9,13,13,18,18,22,22,24,24,2},{11,10,10,9,9,8,8,7,7,6,6,5,5,11},{ 20,19,19,18,18,17,17,16,16,15,15,14,14,20},{24,22,22,18,18,13,13,9,9,4,4,2,2,24},{ 23,21,21,16,16,12,12,7,7,3,3,1,1,23},{14,15,15,16,16,17,17,18,18,19,19,20,20,14},{ 5,6,6,7,7,8,8,9,9,10,10,11,11,5},};int ret[8]={5,4,7,6,1,0,3,2};//for restoring int a[maxn],index,deep;vector<char>    ans;void change (int x) {int y=a[move[x][0]];    for (int i=0;i<12;i+=2) a[move[x][i]]=a[move[x][i+1]]; A[move[x][12]]=y;}    int h (int &x) {int num[4]={0};    for (int i=7;i<=9;i++) num[a[i]]++;    for (int i=16;i<=18;i++) num[a[i]]++; num[a[12]]++;    num[a[13]]++;    int max=0;            for (int i=1;i<=3;i++) {if (Max<num[i]) {max=num[i];        X=i; }} return 8-max;}    bool Dfs (int k) {if (k>deep) return false;    int x, y;    X=h (y);    if (k+x>deep) return false; if (x==0) {index=y;    return true;        } for (int. i=0;i<8;i++) {change (i);        Ans.push_back (i+ ' A ');        if (Dfs (k+1)) return true;        Ans.pop_back ();    Change (Ret[i]); } return false;}    int main () {#ifndef Online_judge freopen ("C:/users/lyf/desktop/in.txt", "R", stdin), #endif int i,j;        while (scanf ("%d", &a[1]) {if (a[1]==0) break;        for (i=2;i<=24;i++) scanf ("%d", &a[i]);        int x, y;        X=h (y);            if (x==0) {printf ("No moves needed\n");            printf ("%d\n", y);        Continue        } deep=1;        Ans.clear ();            while (1) {if (DFS (0)) break;        deep++;        } for (int i=0;i<ans.size (); i++) printf ("%c", Ans[i]);    printf ("\n%d\n", index); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The Rotation Game (POJ 2286 search ida*)

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.