HDU-1667 The Rotation Game(IDA*)

28 篇文章 0 订阅

The Rotation Game

Time Limit: 45000/15000 MS (Java/Others)    Memory Limit: 150000/150000 K (Java/Others)


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. 

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

IDA*,每进一层DFS,统计当前某个数字出现最多的次数num,8-num即为要完成游戏最少走的步数,step+8-num>depth 则剪枝

感觉时间给的很长,而数据不是很多,模拟游戏进程也才500ms


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

const char ALF[]={"ABCDEFGH"};
int mp[9][9],ans,step,depth,tmp;
char t[1023],way[1023];
struct Node {
    int num,times;
}tar[4];

bool cmp(const Node& a,const Node& b) {
    return a.times<b.times;
}

void Judge() {
    tar[1].times=tar[2].times=tar[3].times=0;
    tar[1].num=1;
    tar[2].num=2;
    tar[3].num=3;
    for(int i=3;i<6;++i)
        for(int j=3;j<6;++j)
            ++tar[mp[i][j]].times;
    sort(tar+1,tar+4,cmp);
}

inline void RatColU(int c) {
    tmp=mp[1][c];
    for(int i=1;i<7;++i)
        mp[i][c]=mp[i+1][c];
    mp[7][c]=tmp;
}

inline void RatColD(int c) {
    tmp=mp[7][c];
    for(int i=7;i>1;--i)
        mp[i][c]=mp[i-1][c];
    mp[1][c]=tmp;
}

inline void RatRowL(int r) {
    tmp=mp[r][1];
    for(int i=1;i<7;++i)
        mp[r][i]=mp[r][i+1];
    mp[r][7]=tmp;
}

inline void RatRowR(int r) {
    tmp=mp[r][7];
    for(int i=7;i>1;--i)
        mp[r][i]=mp[r][i-1];
    mp[r][1]=tmp;
}

void (*Rat[8])(int)={RatColU,RatColU,RatRowR,RatRowR,RatColD,RatColD,RatRowL,RatRowL};
const int num[8]={3,5,3,5,5,3,5,3};

bool DFS() {
    Judge();
/*    printf("\n");
    for(int i=1;i<8;++i) {
        for(int j=1;j<8;++j)
            printf("%d  ",mp[i][j]);
        printf("\n");
    }*/
    if(tar[3].times==8) {
        way[step+1]='\0';
        return true;
    }
    if(step+8-tar[3].times>depth)
        return false;
    int i=0;
    for(;i<4;++i) {
        Rat[i](num[i]);
        ++step;
        way[step]=ALF[i];
        if(DFS())
            return true;
        --step;
        Rat[i+4](num[i]);
    }
    for(;i<8;++i) {
        Rat[i](num[i]);
        ++step;
        way[step]=ALF[i];
        if(DFS())
            return true;
        --step;
        Rat[i-4](num[i]);
    }
    return false;
}

int main()
{
/*    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);*/
    int i;
    while(scanf("%d",&mp[1][3]),mp[1][3]) {
        scanf("%d%d%d",&mp[1][5],&mp[2][3],&mp[2][5]);
        for(i=1;i<8;++i)
            scanf("%d",&mp[3][i]);
        scanf("%d%d",&mp[4][3],&mp[4][5]);
        mp[4][4]=0;
        for(i=1;i<8;++i)
            scanf("%d",&mp[5][i]);
        scanf("%d%d%d%d",&mp[6][3],&mp[6][5],&mp[7][3],&mp[7][5]);
        ans=0x3f3f3f3f;
        step=0;
        depth=1;
        while(!DFS()) {
            ++depth;
        }
        if(step)
            printf("%s\n%d\n",way+1,tar[3].num);
        else
            printf("No moves needed\n%d\n",tar[3].num);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值