Topic Links:
http://hihocoder.com/problemset/problem/1233
Title Description:
Given a maximum of seven boxes, the weight of each box is different, each time a box can be placed in the adjacent position, if the adjacent position of the box weight than it, then can be placed in the adjacent position of the box,
Small can not, ask how many steps can be done in such a way to order these boxes well?
With a maximum of 7 chests and a different weight for each box, there are up to 7! States, which can withstand up to 12 states each time the state of the BFS needs to be updated.
The next is how to express the state, because a certain position may have a lot of numbers, so you cannot directly record, you can record the location of each number to represent the state.
There is also the need to large numbers of decimals, as well as record position, and then order, to find out the size of their relationship, you can be large number of decimal.
#include <stdio.h> #include <cstdlib> #include <cstring> #include <queue> #include < Algorithm>using namespace Std;int vis2[8][8];int vis3[8][8][8];int vis4[8][8][8][8];int vis5[8][8][8][8][8];int Vis6[8][8][8][8][8][8];int vis7[8][8][8][8][8][8][8];bool Check (int a[],int n,int step) {if (n==2) {if (vis2[a[1]] [A[2]]!=-1] return 0; else {Vis2[a[1]][a[2]]=step;return 1; }} else if (n==3) {if (vis3[a[1]][a[2]][a[3]]!=-1) return 0; else {Vis3[a[1]][a[2]][a[3]]=step;return 1; }} else if (n==4) {if (vis4[a[1]][a[2]][a[3]][a[4]]!=-1) return 0; else {Vis4[a[1]][a[2]][a[3]][a[4]]=step;return 1; }} else if (n==5) {if (vis5[a[1]][a[2]][a[3]][a[4]][a[5]]!=-1) return 0; else {Vis5[a[1]][a[2]][a[3]][a[4]][a[5]]=step;return 1; }} else if (n==6) {if (vis6[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]!=-1) return 0; else { Vis6[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=step;return 1; }} else if (n==7) {if (vis7[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]][a[7]]!=-1) return 0; else {Vis7[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]][a[7]]=step;return 1; }}}struct node{int pos[10]; int step;}; void BFs (int n) {int a[10]; node start; for (int i=1;i<=n;i++) a[i]=i; Check (a,n,0); First initialize, the starting state step equals 0 for (int i=1;i<=n;i++) start.pos[i]=i; Start.step=0; Queue < node > que; Que.push (start); while (!que.empty ()) {node Cur=que.front (); Que.pop (); for (int i=1;i<=n;i++) {int l=0,r=0; for (int j=1;j<i;j++) {if (cur.pos[j]==cur.pos[i]-1) l=1;//l==1, indicating that the left is smaller than it and cannot move left if (cur.pos[j]==cur.pos[i]+1) r=1; if (Cur.pos[j]==cur.pos[i]) l=r=1;//cannot move left or right} if (cur.pos[i]-1<1) l=1; if (cur.pos[i]+1>n) r=1; if (l==0) {cur.pos[i]--; cur.step++; if (check (cur.pos,n,cur.step)) Que.push (cur); cur.pos[i]++; cur.step--; } if (r==0) {cur.pos[i]++; cur.step++; if (check (cur.pos,n,cur.step)) Que.push (cur); cur.pos[i]--; cur.step--; }}}}void init () {memset (vis2,-1,sizeof (VIS2)); memset (vis3,-1,sizeof (VIS3)); memset (vis4,-1,sizeof (VIS4)); memset (vis5,-1,sizeof (VIS5)); memset (vis6,-1,sizeof (VIS6)); memset (vis7,-1,sizeof (VIS7)); for (int i=2;i<=7;i++) BFS (i);} struct num{int data,id;} Input[10];bool cmp (num a,num b) {if (a.data<b.data) return 1; else return 0;} int main () {int b[10]; Freopen ("Test.txt", "R", stdin); int t; scanf ("%d", &t); Init (); while (t--) {int n; scanf ("%d", & n); for (int i=1;i<=n;i++) {scanf ("%d", &input[i].data); Input[i].id=i; } sort (input+1,input+n+1,cmp); for (int i=1;i<=n;i++) {b[i]=input[i].id; } if (n==1) printf ("0\n"); if (n==2) printf ("%d\n", Vis2[b[1]][b[2]]); if (n==3) printf ("%d\n", Vis3[b[1]][b[2]][b[3]]); if (n==4) printf ("%d\n", Vis4[b[1]][b[2]][b[3]][b[4]]); if (n==5) printf ("%d\n", Vis5[b[1]][b[2]][b[3]][b[4]][b[5]]); if (n==6) printf ("%d\n", Vis6[b[1]][b[2]][b[3]][b[4]][b[5]][b[6]]); if (n==7) printf ("%d\n", Vis7[b[1]][b[2]][b[3]][b[4]][b[5]][b[6]][b[7]]); } return 0;}
2015 Beijing online Game boxes (BFS)