poj2286The Rotation Game【深搜IDA*】

来源:互联网 发布:凸包算法代码 编辑:程序博客网 时间:2024/04/28 12:14

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.

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 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

题意:八的方向的头位数字放到尾部,其他依次挪一格,问怎么移动达到中间8个相同

看到IDA*感觉自己有发现了新大陆QAQ 深搜嘛,肯定要考虑时间的优化、即剪枝,A*的算法侧重于每次选择最优的方向进行探索,这个题是要在最好的情况下也无法达到预期的步数,那么预期的步数++,当然了,二分写的话肯定更快但是15s是白给的吗,没必要是不是~两次剪枝:1.刚刚移动过来的方向就不要返回去了  2、最多的方块全部归位步数也达不到要求

/**************poj22862016.2.28164K219MSC++1805B**************/#include <iostream>#include<cstdio>#include<cstring>using namespace std;int res[8]={5,4,7,6,1,0,3,2};int rot[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 num[24];//记录原始数据int cnt[4];//1-3出现次数char op[100];//方法int pathlen;//方法步数bool check(int num[]){    int q=num[6];    for(int i=0;i<3;i++) if(q!=num[i+15]||q!=num[i+6]) return false;    if(q!=num[11]||q!=num[12]) return false;    return true;}void rotate(int k,int num[]){    int tmp=num[rot[k][0]];    for(int i=0;i<6;i++) num[rot[k][i]]=num[rot[k][i+1]];    num[rot[k][6]]=tmp;}bool dfs(int k,int num[],char op[],int la){    cnt[1]=cnt[2]=cnt[3]=0;    for(int i=6;i<=8;i++) cnt[num[i]]++,cnt[num[i+9]]++;    cnt[num[11]]++,cnt[num[12]]++;    if(cnt[1]<cnt[2]) cnt[0]=cnt[2];    else cnt[0]=cnt[1];    if(cnt[0]<cnt[3]) cnt[0]=cnt[3];    if(k+8-cnt[0]>=pathlen) return false;    for(int i=0;i<8;i++)    {        if(la!=-1&&res[i]==la) continue;//来的方向不必走回去!        rotate(i,num);//上下rotate的参数不一样!!!        op[k]='A'+i;        if(check(num))        {            op[k+1]='\0';            return true;        }         else if(dfs(k+1,num,op,i))return true;         rotate(res[i],num);    }    return false;}int main(){    //freopen("cin.txt","r",stdin);    int h;    while(~scanf("%d",&h)&&h)    {        num[0]=h;        for(int i=1;i<24;i++) scanf("%d",&num[i]);        if(check(num))        {            printf("No moves needed\n%d\n",num[6]);            continue;        }        pathlen=0;        op[0]='\0';        while(!dfs(0,num,op,-1)) pathlen++;        printf("%s\n%d\n",op,num[6]);    }    return 0;}


0 0
原创粉丝点击