hdu1667(IDA*)

題目描述:c++

The Rotation Game

Time Limit: 45000/15000 MS (Java/Others)    Memory Limit: 150000/150000 K (Java/Others)
Total Submission(s): 2898    Accepted Submission(s): 1169


api

Problem Description
The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind.
watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

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

 

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

 

 

Output
For each test case, you must output two lines. The first line contains all the moves needed to reach the final configuration. Each move is a letter, ranging from `A' to `H', and there should not be any spaces between the letters in the line. If no moves are 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 are 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 is smallest in dictionary order for the 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 3 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 0
 

 

Sample Output
AC
2
DDHH
2
思路:嗯,深度無限深,廣度也很廣,迭代加深應該比較好想到(我口胡的),可是這題是真的快樂,好多麻煩的地方,
有八種移動的方式,每種方式要移動7個方塊,因此要開一個二維數組記錄,還必須手打,並且這題回溯能夠不用
記錄以前的狀態,由於每種移動方式和另外一種移動方式是相反的,因此咱們只須要記錄每一種移動方式相對應
的移動方式,而後回溯調用,反向移動便可,並且你不這樣作也會爆棧的^_^,中心地八個點地座標也須要用一個數組
標記起來,用來判斷是否已經知足,而後就是整體設計了
迭代加深,每次只移動depth深度,不停地加深depth,知道求出解,A*剪枝,就是你中心的可能最少移動次數=(8-任意數字在中心位置的最大出現次數)
若是大於depth,說明這樣走下去也不願能有解,不得行,因此剪掉,仍是看代碼吧,學長給我調了半天hh
AC代碼:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100;
const int inf = 0x3f3f3f3f;
int center[8] = { 6,7,8,11,12,15,16,17 };
int reveropt[8] = { 5,4,7,6,1,0,3,2 };
int change[8][7] = {
    {0,2,6,11,15,20,22},
    {1,3,8,12,17,21,23},
    {10,9,8,7,6,5,4},
    {19,18,17,16,15,14,13},
    {23,21,17,12,8,3,1},
    {22,20,15,11,6,2,0},
    {13,14,15,16,17,18,19},
    {4,5,6,7,8,9,10}
};
int tmp[24];
int cnt[5];
int get() {//找出8-出現次數最多的數的出現次數,即至少須要移動的次數
    int maxcnt = 0;
    memset(cnt, 0, sizeof(cnt));
    for (int i = 0; i < 8; i++) {
        cnt[tmp[center[i]]]++;
        maxcnt = max(maxcnt, cnt[tmp[center[i]]]);
    }
    return 8 - maxcnt;
}
void option(int opt) {//移動
    int t = tmp[change[opt][0]];
    for (int j = 0; j < 6; j++) {
        tmp[change[opt][j]] = tmp[change[opt][j + 1]];
    }
    tmp[change[opt][6]] = t;
}
string res;
int depth;
bool dfs(int step, int lastopt) {
    int last = get();
    if (step + last >= depth) return false;//已經移動+至少須要移動>預設深度
    if (!last) {//中心點全都相同了
        printf("%s\n", res.c_str());
        printf("%d\n", tmp[center[0]]);
        return true;
    }
    for (int i = 0; i < 8; i++) {
        if (lastopt != -1 && i == reveropt[lastopt])continue;//若是與上次操做恰好相反的就不要
        res.push_back('A' + i);
        option(i);//移動
        if (dfs(step + 1, i))return true;
        option(reveropt[i]);//回溯
        res.pop_back();
    }
    return false;
}
int main() {
    //freopen("test.txt", "r", stdin);
    while (~scanf("%d", &tmp[0])) {
        if (tmp[0] == 0)break;
        for (int i = 1; i < 24; i++) {
            scanf("%d", &tmp[i]);
        }
        res.clear();
        if (!get()) {
            printf("No moves needed\n%d\n",tmp[center[0]]); continue;
        }
        depth = 1;
        while (1) {
            if (dfs(0, -1))break;
            depth++;
        }
    }
    return 0;
}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息