2*2*2的魔方,给你一个初始状态,和一个限定步数,问你在这么多步数条件下最多能有多少面拼好,(不是累加关系,是某一个状态的最多,最多是6);
思路:
最多是7步,所以直接暴力搜索,第一次写,无脑写的,308行,结果超时了,sb了,哎!有从写了一下,就是找找转换规律,100多行ac,总之就是个水模拟,细心点,转换规律别写错了就行了...
#include<stdio.h> #include<string.h> #include<queue> using namespace std; typedef struct { int a ,b; }SW; typedef struct { int now[25] ,t; }NODE; SW sw[5][15]; NODE xin ,tou; void csh_sw() { sw[1][1].a = 4 ,sw[1][1].b = 6; sw[1][2].a = 5 ,sw[1][2].b = 7; sw[1][3].a = 6 ,sw[1][3].b = 8; sw[1][4].a = 7 ,sw[1][4].b = 9; sw[1][5].a = 8 ,sw[1][5].b = 23; sw[1][6].a = 9 ,sw[1][6].b = 22; sw[1][7].a = 23 ,sw[1][7].b = 4; sw[1][8].a = 22 ,sw[1][8].b = 5; sw[1][9].a = 1 ,sw[1][9].b = 0; sw[1][10].a = 3,sw[1][10].b = 1; sw[1][11].a = 2 ,sw[1][11].b = 3; sw[1][12].a = 0 ,sw[1][12].b = 2; sw[2][1].a = 1 ,sw[2][1].b = 7; sw[2][2].a = 3 ,sw[2][2].b = 13; sw[2][3].a = 7 ,sw[2][3].b = 17; sw[2][4].a = 13 ,sw[2][4].b = 19; sw[2][5].a = 17 ,sw[2][5].b = 21; sw[2][6].a = 19 ,sw[2][6].b = 23; sw[2][7].a = 21 ,sw[2][7].b = 1; sw[2][8].a = 23 ,sw[2][8].b = 3; sw[2][9].a = 9 ,sw[2][9].b = 8; sw[2][10].a = 15,sw[2][10].b = 9; sw[2][11].a = 14 ,sw[2][11].b = 15; sw[2][12].a = 8 ,sw[2][12].b = 14; sw[3][1].a = 2 ,sw[3][1].b = 8; sw[3][2].a = 3 ,sw[3][2].b = 14; sw[3][3].a = 8 ,sw[3][3].b = 17; sw[3][4].a = 14 ,sw[3][4].b = 16; sw[3][5].a = 17 ,sw[3][5].b = 11; sw[3][6].a = 16 ,sw[3][6].b = 5; sw[3][7].a = 11 ,sw[3][7].b = 2; sw[3][8].a = 5 ,sw[3][8].b = 3; sw[3][9].a = 6 ,sw[3][9].b = 7; sw[3][10].a = 7,sw[3][10].b = 13; sw[3][11].a = 13 ,sw[3][11].b = 12; sw[3][12].a = 12 ,sw[3][12].b = 6; } int maxx; void jude(NODE a) { int now = 0; if(a.now[0] == a.now[1] && a.now[1] == a.now[2] && a.now[2] == a.now[3]) now ++; if(a.now[4] == a.now[5] && a.now[5] == a.now[10] && a.now[10] == a.now[11]) now ++; if(a.now[6] == a.now[7] && a.now[7] == a.now[12] && a.now[12] == a.now[13]) now ++; if(a.now[8] == a.now[9] && a.now[9] == a.now[14] && a.now[14] == a.now[15]) now ++; if(a.now[16] == a.now[17] && a.now[17] == a.now[18] && a.now[18] == a.now[19]) now ++; if(a.now[20] == a.now[21] && a.now[21] == a.now[22] && a.now[22] == a.now[23]) now ++; if(maxx < now) maxx = now; } int T; void bfs() { queue<NODE>q; xin.t = 0; q.push(xin); maxx = 0; jude(xin); while(!q.empty()) { tou = q.front(); q.pop(); jude(tou); if(maxx == 6) return; if(tou.t == T) continue; for(int i = 1 ;i <= 3 ;i ++) { xin = tou ,xin.t ++; for(int j = 1 ;j <= 12 ;j ++) xin.now[sw[i][j].a] = tou.now[sw[i][j].b]; q.push(xin); } for(int i = 1 ;i <= 3 ;i ++) { xin = tou ,xin.t ++; for(int j = 1 ;j <= 12 ;j ++) xin.now[sw[i][j].b] = tou.now[sw[i][j].a]; q.push(xin); } } } int main () { int i; csh_sw(); while(~scanf("%d" ,&T)) { maxx = 0; for(i = 0 ;i <= 23 ;i ++) scanf("%d" ,&xin.now[i]); bfs(); printf("%d " ,maxx); } return 0; }