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;
}
相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息