int resolve() { memset(head, 0, sizeof(head)); memset(next, 0, sizeof(next)); memset(dist, 0, sizeof(dist)); int front = 1, rear = 2, i, j, x, y, newx, newy, p; try_to_insert(1); while (front < rear) { State ¤t = st[front]; if (memcmp(current, goal, sizeof(current)) == 0) return dist[front]; if (dist[front] == 10) { ++front; continue; } for (j = 0; j < 25; ++j) { if (current[j] == -1) break; } x = j/5; y = j%5; for (i = 0; i < STEP; ++i) { newx = x+dir[i][0]; newy = y+dir[i][1]; if (newx < 0 || newx > 4 || newy < 0 || newy > 4) continue; p = newx*5+newy; State &t = st[rear]; memcpy(&t, ¤t, sizeof(current)); t[j] = t[p]; t[p] = -1; if (try_to_insert(rear)) { dist[rear] = dist[front]+1; ++rear; } } ++front; } return 11; }
int main() { while (scanf("%d", &T) != EOF && T > 0) { for(int cases = 1; cases <= T; cases++) { memset(head, 0, sizeof(head)); memset(next, 0, sizeof(next)); sl = 1;//begin with 1, 0 is init for head and next int sum = 0; scanf("%d%d", &M, &N); getchar();//eat '\n' after scanf M N for(int i = 0; i <= M - 1; i++) { fgets(lan1[i], CL, stdin); lan1[i][strlen(lan1[i]) - 1] = '\0';//if(lan1[i][0] == '\0') lan1[i][0] = ' ';// " " replace the "", in order to hash it , error " a" == "a " } for(int i = 0; i <= N - 1; i++) { fgets(lan2[i], CL, stdin); lan2[i][strlen(lan2[i]) - 1] = '\0';//if(lan2[i][0] == '\0') lan2[i][0] = ' '; } for(int i = 0; i <= M - 1; i++) for(int j = 0; j <= N - 1; j++) { char cats[CL + CL] = {'\0'}; catlang(cats, lan1[i], lan2[j]); strcpy(st[sl++], cats); if(try_to_insert(sl - 1)) sum++; } printf("Case %d: %d\n", cases, sum); } } return 0; }