The Rotation Game uva1343

5 篇文章 0 订阅
2 篇文章 0 订阅
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
#define N 10
int n;
int mat[N][N];
int pos=0;
int ans[100];
bool read()
{
    if(cin>>mat[1][3]&&mat[1][3]) {
        cin>>mat[1][5];
        cin>>mat[2][3];
        cin>>mat[2][5];
        for(int i=1;i<=7;i++) cin>>mat[3][i];
        cin>>mat[4][3]>>mat[4][5];
        for(int i=1;i<=7;i++) cin>>mat[5][i];
        cin>>mat[6][3]>>mat[6][5];
        cin>>mat[7][3]>>mat[7][5];
        return 1;
    }
    return 0;
}
void aopr()
{
    int buff=mat[1][3];
    for(int i=1;i<=6;i++) mat[i][3]=mat[i+1][3];
    mat[7][3]=buff;
}
void bopr()
{
    int buff=mat[1][5];
    for(int i=1;i<=6;i++) mat[i][5]=mat[i+1][5];
    mat[7][5]=buff;
}
void copr()
{
    int buff=mat[3][7];
    for(int i=7;i>=2;i--) mat[3][i]=mat[3][i-1];
    mat[3][1]=buff;
}
void dopr()
{
    int buff=mat[5][7];
    for(int i=7;i>=2;i--) mat[5][i]=mat[5][i-1];
    mat[5][1]=buff;
}
void eopr()
{
    int buff=mat[7][5];
    for(int i=7;i>=2;i--) mat[i][5]=mat[i-1][5];
    mat[1][5]=buff;
}
void fopr()
{
    int buff=mat[7][3];
    for(int i=7;i>=2;i--) mat[i][3]=mat[i-1][3];
    mat[1][3]=buff;
}
void gopr()
{
    int buff=mat[5][1];
    for(int i=1;i<=6;i++) mat[5][i]=mat[5][i+1];
    mat[5][7]=buff;
}
void hopr()
{
    int buff=mat[3][1];
    for(int i=1;i<=6;i++) mat[3][i]=mat[3][i+1];
    mat[3][7]=buff;
}
void opr(int op)
{
    if(op==1) aopr();
    else if(op==2) bopr();
    else if(op==3) copr();
    else if(op==4) dopr();
    else if(op==5) eopr();
    else if(op==6) fopr();
    else if(op==7) gopr();
    else hopr();
}
void rop(int op)
{
    if(op==1) fopr();
    else if(op==2) eopr();
    else if(op==3) hopr();
    else if(op==4) gopr();
    else if(op==5) bopr();
    else if(op==6) aopr();
    else if(op==7) dopr();
    else if(op==8) copr();
}
int find()
{
    int num[5];for(int i=1;i<=3;i++) num[i]=0;
    for(int i=3;i<=5;i++) ++num[mat[3][i]];
    num[mat[4][3]]++;
    num[mat[4][5]]++;
    for(int i=3;i<=5;i++) ++num[mat[5][i]];
    return max(max(num[1],num[2]),num[3]);
}
void dfs(int now,int depth)
{
    if(pos) return;
    int buff=find();
    //cout<<now<<' ' <<buff<<' '<<depth<<endl;
    if(now+8-buff>depth) return;
    if(now>depth) return;//顶多差一层
    if(buff==8)
    {
        if(now==0) {printf("No moves needed\n");printf("%d\n",mat[3][3]);pos=1;}
        else
        {
        for(int i=0;i<depth;i++) printf("%c",ans[i]+'A'-1);
        printf("\n%d\n",mat[3][3]);
        pos=1;
        }
        return;
    }
    for(int i=1;i<=8;i++) {if(pos) return ;opr(i);ans[now]=i;dfs(now+1,depth);rop(i);}
}
void init()
{
    pos=0;
    memset(mat,0,sizeof(mat));
   // memset(num,0,sizeof(num));
}
void solve()
{
    int depth;
    for(depth=0;;depth++)
    {
        dfs(0,depth);
        if(pos) break;
    }
}
int main()
{
    while(read())
    {
        solve();
        init();
    }
    /*read();
    int op;
    while(cin>>op)
    {
        opr(op);
        for(int i=1;i<=7;i++)
        {
            for(int j=1;j<=7;j++) cout<<mat[i][j]<<' ';
            cout<<endl;
        }
    }*/
    return 0;
}
很简单的ida* 这个剪枝只要一个就好了! 就是 now+ 8 - find()>  depth 就好了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值