bfs-HDU 5012Dice

152 篇文章 0 订阅
46 篇文章 0 订阅

题意:

给出初状态,给出末状态,4个方向翻转最少几步。

思路:

一开始以为是个规律题。查了下是个搜索。知道是搜索就很好做了

#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
struct node
{
    int left,right,top,bottom,front,back,step;
};
node save[10000];
int cnt=0;
queue<node> s;
node ans;
bool Isover(node t1,node t2)
{
    if(t1.front==t2.front&&t1.back==t2.back&&t1.bottom==t2.bottom&&t1.left==t2.left&&t1.right==t2.right&&t1.top==t2.top)
        return true;
    return false;
}
bool notemp(node t)
{
    for(int i=1;i<=cnt;i++)
    {
    if(t.front==save[i].front&&t.back==save[i].back&&t.bottom==save[i].bottom&&t.left==save[i].left&&t.right==save[i].right&&t.top==save[i].top)
            return false;
    }
    return true;
}
void solve(node temp,int x)
{
    node f=temp;
    if(x==1)
    {
        temp.left=f.top;
        temp.right=f.bottom;
        temp.top=f.right;
        temp.bottom=f.left;
        temp.front=f.front;
        temp.back=f.back;
    }
    else if(x==2)
    {
        temp.left=f.bottom;
        temp.right=f.top;
        temp.front=f.front;
        temp.back=f.back;
        temp.top=f.left;
        temp.bottom=f.right;
    }
    else if(x==3)
    {
        temp.left=f.left;
        temp.right=f.right;
        temp.front=f.top;
        temp.back=f.bottom;
        temp.top=f.back;
        temp.bottom=f.front;
    }
    else if(x==4)
    {
        temp.left=f.left;
        temp.right=f.right;
        temp.front=f.bottom;
        temp.back=f.top;
        temp.top=f.front;
        temp.bottom=f.back;
    }
    if(notemp(temp))
    {
        temp.step=f.step+1;
        s.push(temp);
        save[++cnt]=temp;
    }
}
void bfs()
{
    int yes=1;
    while(!s.empty())
    {

        node t=s.front();
        s.pop();
        if(Isover(t,ans))
           {
               yes=0;
               printf("%d\n",t.step);
               break;
           }
        for(int i=1;i<=4;i++)
        {
            node temp=t;
            solve(temp,i);
        }

    }
    if(yes)
    printf("-1\n");
    return ;
}

int main()
{   node t;
    while(scanf("%d%d%d%d%d%d",&t.top,&t.bottom,&t.left,&t.right,&t.front,&t.back)!=EOF)
    {
        cnt=0;
        t.step=0;
        scanf("%d%d%d%d%d%d",&ans.top,&ans.bottom,&ans.left,&ans.right,&ans.front,&ans.back);
        while(!s.empty())
            s.pop();
        s.push(t);
        save[++cnt]=t;
        bfs();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值