hdu1667-IDA*-迭代加深搜索 A*算法

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

现有一块有24个格子的井字板子,每个格子用1、2或3标记,每种格子各有8个。 
起初这些格子分布随机,你需要通过A-H 8种操作将中心8个格子作变为相同的标记。(图中使用A操作将A列向上拉了一格,C操作将C列向右拉了一列,中心变为2)

#include<bits/stdc++.h>
using namespace std;
int mp[24];//24个字符
int center[8]={6,7,8,11,12,15,16,17};//中心8个字符位置
int rever[9]={5,4,7,6,1,0,3,2,-1};//abcdefgh操作对应的逆操作
int index[8][7]={
 0,2,6,11,15,20,22,   //A
 1,3,8,12,17,21,23,   //B
 10,9,8,7,6,5,4,      //C
 19,18,17,16,15,14,13,//D
 23,21,17,12,8,3,1,   //E
 22,20,15,11,6,2,0,   //G
 13,14,15,16,17,18,19,//H
 4,5,6,7,8,9,10
};
char ans[105];//输出结果
bool flag;
int deep;//深度
void aa(int x)//转移
{
    int tmp=mp[index[x][0]];
    for(int i=0;i<6;i++)
        mp[index[x][i]]=mp[index[x][i+1]];
    mp[index[x][6]]=tmp;
}
int get_c()//预计至少还要步数
{
    int cnt[4]={0};
    int num=0;
    for(int i=0;i<8;i++)//中间8个数
    {
        cnt[mp[center[i]]]++;
        num=max(num,cnt[mp[center[i]]]);
    }
    return 8-num;
}
void dfs(int leep,int lastop)
{
    if(flag) return;
    if(leep>deep||leep+get_c()>deep) return;
    if(get_c()==0)
    {
        flag=true;
        ans[leep]='\0';
        printf("%s\n%d\n",ans,mp[6]);
        return;
    }
    for(int i=0;i<8;i++)
    {
        if(i!=rever[lastop])
        {
            aa(i);
            ans[leep]=i+'A';
            dfs(leep+1,i);
            aa(rever[i]);
        }
    }
}
int main()
{
  while(~scanf("%d",&mp[0])&&mp[0])
  {
      flag=0;
      for(int i=1;i<24;i++)
        scanf("%d",&mp[i]);
      deep=get_c();
      if(deep==0)
      {
        printf("No moves needed\n%d\n", mp[6]);
        continue;
      }
      while(1)
      {
          dfs(0,8);
          if(flag) break;
          deep++;
      }
  }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值