#poj 2286 入门级IDA*

poj 2286 IDA*

题目:

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, outputNo 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.

题意:

就是在一个井字型区域中,每个格子都有1,2,3中的一个数字,井字的每一条边都可以来回抽拉,现在问最小多少次才能把中间的八个格子都变成同一个数字。
相同步数输出字典序最小的那个。

思路:

看到了最小首先就想到了bfs,但是用bf状态上简直爆炸,然后自然想到了A * ,但是A* 的话连hash都不好搞(至少我这么觉得),那么就能想到IDA*就可以避免这些问题了,IDA * 故名思义,就是IDDFS+A * 了。层数限制limit是枚举走到第几步可以成功,估价函数h为八个格子中最多的那个数字还差多少个没有拼满(因为每次操作最多多一个)。IDA * 估价函数的作用说是启发式,感觉更像是结合limit的一刀剪枝 if(cur+h>limit)return false;

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <stdlib.h>
using namespace std;
const int pos[10]={0,7,8,9,12,13,16,17,18};
int A[30];
int ans[1000];//存路径
int res;//存相同的数字
const int p[10]={'\0','A','B','C','D','E','F','G','H','\0'};
int B[10]={0,6,5,8,7,2,1,4,3};
int  get_h()
{
    int c[4]={0};
    for(int i=1;i<=8;i++)
        c[A[pos[i]]]++;
    int maxn;
    maxn=max(c[1],max(c[2],c[3]));
    return 8-maxn;
}
void change(int a)//a是哪一种,b是正转反转
{
    if(a==1){
        int t=A[1];
        A[1]=A[3],A[3]=A[7],A[7]=A[12],A[12]=A[16],A[16]=A[21],A[21]=A[23],A[23]=t;
    }
    else if(a==2){
        int t=A[2];
        A[2]=A[4],A[4]=A[9],A[9]=A[13],A[13]=A[18],A[18]=A[22],A[22]=A[24],A[24]=t;
    }
    else if(a==6){
        int t=A[23];
        A[23]=A[21],A[21]=A[16],A[16]=A[12],A[12]=A[7],A[7]=A[3],A[3]=A[1],A[1]=t;
    }
    else if(a==5){
        int t=A[24];
        A[24]=A[22],A[22]=A[18],A[18]=A[13],A[13]=A[9],A[9]=A[4],A[4]=A[2],A[2]=t;
    }
    else if(a==3){
        int t=A[11];
        for(int i=11;i>=6;i--)
            A[i]=A[i-1];
        A[5]=t;
    }
    else if(a==4){
        int t=A[20];
        for(int i=20;i>=15;i--)
            A[i]=A[i-1];
        A[14]=t;
    }
    else if(a==7){
        int t=A[14];
        for(int i=14;i<=19;i++)
            A[i]=A[i+1];
        A[20]=t;
    }
    else {
        int t=A[5];
        for(int i=5;i<=10;i++)
            A[i]=A[i+1];
        A[11]=t;
    }
}
bool dfs(int cur,int limit,int last)//已经走完了多少步,最多多少步,上一步是怎么走的。
{
    if(cur==limit)
    {
        if(get_h())
            return false;

        return true;
    }

    for(int i=1;i<=8;i++)
    {
        if(B[i]!=last)
        {
            ans[cur+1]=i;
            change(i);
            if(cur+1+get_h()<=limit&&dfs(cur+1,limit,i))
                return true;
            change(B[i]);
        }
    }
    return false;
}
int main()
{
    while(scanf("%d",&A[1]),A[1]!=0)
    {
        for(int i=2;i<=24;i++)
            scanf("%d",&A[i]);
        if(!get_h())
        {
            printf("No moves needed\n%d\n",A[pos[1]]);
            continue;
        }
        int k=1;
        while(!dfs(0,k++,-1));
        for(int i=1;i<k;i++)
            printf("%c",p[ans[i]]);
        printf("\n%d\n",A[7]);
    }
    return 0;
}

P.S.这道题手残最开始输出的地方写的是i<=k,改了好久,测了好多数据都没测出来,感觉自己已经是个残弱了orz

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值