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.
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.
The last line of the input file is a sequence of 10 zeros.
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.
3413569728 1165432789 0000000000
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;
}