hdu 2918 Tobo or not Tobo(BFS+状态标记)

The game of Tobo is played on a plastic board designed into a 3 × 3 grid with cells numbered from 1 to 9 as shown in figure (a). The grid has four dials (labeled ``A" to ``D" in the figure.) Each dial can be rotated in 90 degrees increment in either direction. Rotating a dial causes the four cells currently adjacent to it to rotate along. For example, figure (b) shows the Tobo after rotating dial ``A" once in a clockwise direction. Figure (c) shows the Tobo in figure (b) after rotating dial ``D" once in a counterclockwise direction. 




Kids love to challenge each other playing the Tobo. Starting with the arrangement shown in figure (a), (which we'll call the standard arrangement,) one kid would randomly rotate the dials, X number of times, in order to ``shuffle" the board. Another kid then tries to bring the board back to its standard arrangement, taking no more than X rotations to do so. The less rotations are needed to restore it, the better. This is where you see a business opportunity. You would like to sell these kids a program to advise them on the minimum number of steps needed to bring a Tobo back to its standard arrangement.
Input
Your program will be tested on one or more test cases. Each test case is specified on a line by itself. Each line is made of 10 decimal digits. Let's call the first digit Y . The remaining 9 digits are non-zeros and describe the current arrangement of the Tobo in a row-major top-down, left-to-right ordering. The first sample case corresponds to figure (c). 

The last line of the input file is a sequence of 10 zeros.
Output
For each test case, print the result using the following format: 


k . R 


where k is the test case number (starting at 1,) is a single space, and R is the minimum number of rotations needed to bring the Tobo back to its standard arrangement. If this can't be done in Y dials or less, then R = -1.
Sample Input
3413569728
1165432789
0000000000
Sample Output
1. 2
2. -1


题意:3X3的方格,ABCD各自表示不同2X2的矩阵,每个矩阵只能旋转90度,即只有2X2的矩形内的数换位置。  给你一个K和9个数,9个数对应3X3的方格 。  问你是否能在K步内从123456789旋转到那九个数,如果可以则输出最小步数,否则-1;

BFS+状态标记,每询问一次,广搜一次,存下到达这个状态的最小步数,下次询问时看这个状态是否走过,没有则继续查询。


代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#define mem(a,c) memset(a,c,sizeof(a));
#define inf 0x6fffffff
using namespace std;
struct SD{
    int x,y;
    bool operator<(const struct SD &a)const{
      return x<a.x;
    }
}p,pp;
int minn,a[10],b[10];
char tree[5000<<2];
map<int,int> bk;
queue<struct SD> q;
int ss(int *a){
    int num=0;
    for(int i=8;i>=0;i--)num=num*10+a[i];
    ///cout<<num<<endl;
    return num;
}
int bfs(int ans,int c){
    int num=0;
    if(bk.count(ans)){
        if(bk[ans]<=c)return bk[ans];
        else return -1;
    }

    while(q.size()){
        p=q.front();
        //bk[p.x]=p.y;
        q.pop();
        if(p.x==ans)return p.y;
        if(p.y>c)return -1;
        int cur=0,nm=p.x;
        while(nm){
            b[cur]=a[cur]=nm%10;//if(p.x==123456789)cout<<b[cur];
            cur++;
            nm/=10;
        }
        int a1=8,a2=7,a3=5,a4=4;
        b[a1]=a[a3],b[a2]=a[a1],b[a3]=a[a4],b[a4]=a[a2];num=ss(b);if(!bk.count(num))pp.x=num,pp.y=p.y+1,bk[num]=pp.y,q.push(pp);
        b[a1]=a[a2],b[a2]=a[a4],b[a3]=a[a1],b[a4]=a[a3];num=ss(b);if(!bk.count(num))pp.x=num,pp.y=p.y+1,bk[num]=pp.y,q.push(pp);
        b[a1]=a[a1],b[a2]=a[a2],b[a3]=a[a3],b[a4]=a[a4];

        a1--,a2--,a3--,a4--;
        b[a1]=a[a3],b[a2]=a[a1],b[a3]=a[a4],b[a4]=a[a2];num=ss(b);if(!bk.count(num))pp.x=num,pp.y=p.y+1,bk[num]=pp.y,q.push(pp);
        b[a1]=a[a2],b[a2]=a[a4],b[a3]=a[a1],b[a4]=a[a3];num=ss(b);if(!bk.count(num))pp.x=num,pp.y=p.y+1,bk[num]=pp.y,q.push(pp);
        b[a1]=a[a1],b[a2]=a[a2],b[a3]=a[a3],b[a4]=a[a4];

        a1-=2,a2-=2,a3-=2,a4-=2;
        b[a1]=a[a3],b[a2]=a[a1],b[a3]=a[a4],b[a4]=a[a2];num=ss(b);if(!bk.count(num))pp.x=num,pp.y=p.y+1,bk[num]=pp.y,q.push(pp);
        b[a1]=a[a2],b[a2]=a[a4],b[a3]=a[a1],b[a4]=a[a3];num=ss(b);if(!bk.count(num))pp.x=num,pp.y=p.y+1,bk[num]=pp.y,q.push(pp);
        b[a1]=a[a1],b[a2]=a[a2],b[a3]=a[a3],b[a4]=a[a4];

        a1--,a2--,a3--,a4--;
        b[a1]=a[a3],b[a2]=a[a1],b[a3]=a[a4],b[a4]=a[a2];num=ss(b);if(!bk.count(num))pp.x=num,pp.y=p.y+1,bk[num]=pp.y,q.push(pp);
        b[a1]=a[a2],b[a2]=a[a4],b[a3]=a[a1],b[a4]=a[a3];num=ss(b);if(!bk.count(num))pp.x=num,pp.y=p.y+1,bk[num]=pp.y,q.push(pp);
        b[a1]=a[a1],b[a2]=a[a2],b[a3]=a[a3],b[a4]=a[a4];
    }
    return -1;
    //return;
}
int main()
{//cout<<__INT32_MAX__;
    bk.clear();
    int num=123456789;
    p.x=num;p.y=0;
    bk[num]=0;
    while(q.size())q.pop();
    //cout<<p.x;
    q.push(p);
    int n,m,k,cu=1;//cout<<"asd"<<endl;
    while(scanf("%1d",&k)){
        int sum=0,p;
        for(int i=0;i<9;i++){
                scanf("%1d",&p);sum=sum*10+p;//cout<<sum<<endl;
        }
        if(!k&&!sum)break;

        cout<<cu++<<". ";
        cout<<bfs(sum,k)<<endl;
    }


    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值