#include <iostream> #include <cstdio> #include <cstring>using namespace Std;class board{public:int a[ 16]; int h; int n; BOOL Readin () {int i; scanf ("%d", &n); if (n = = 0) return false; for (i=0;i<n;i++) scanf ("%d", a+i); return true; } void Move (int s,int e,int p) {int t[16],i,j; if (p<s) {for (i=0; i<p; i++) t[i]=a[i]; for (j=s; j<=e; j++,i++) t[i]=a[j]; for (j=p;j<s;j++,i++) t[i]=a[j]; for (j=e+1;j<n;j++,i++) t[i]=a[j]; } else {for (i=0;i<s;i++) t[i]=a[i]; for (j=e+1;j<p;j++,i++) t[i]=a[j]; for (j=s;j<=e;j++,i++) t[i]=a[j]; for (j=p;j<n;j++,i++) t[i]=a[j]; } memcpy (A,t,sizeof (t)); } int Geth () { int cnt,i; cnt=0; for (i=0;i<n-1;i++) {if (a[i+1]!=a[i]+1) cnt++; } if (a[i]!=n) cnt++; h=cnt; return CNT; }};int lim;int F (int dep,int h) {return dep*3+h;} BOOL Iddfs (int dep,board b) {int i,j,k; Board TB; B.geth (); if (f (DEP, b.h) > Lim*3) return false; if (B.geth () ==0) return true; for (i=0;i<b.n;i++) {for (j=i;j<b.n;j++) {for (k=0;k<i;k++) { Tb=b; Tb.move (I,J,K); if (Iddfs (DEP+1,TB) ==true) return true; } for (k=j+2;k<b.n;k++) {tb=b; Tb.move (I,J,K); if (Iddfs (DEP+1,TB) ==true) return true; }}} return false;} int main () {Board B; int prob; Prob=1; while (B.readin () ==true) { for (lim=0;; lim++) {if (Iddfs (0,b) ==true) break; } printf ("Case%d:%d\n", Prob, Lim); prob++; } return 0;}
Uva11212-editing a book