#include #include #include #include #include #include #define CODE(lights,pos) ((lights)*10+(pos)) #define LIGHTS(code) ((code)/10) #define POS(code) ((code)%10) #define ZERO(x) (memset((x),0,sizeof(x))) static int steps[(2<<10)*10]; static int steps_best; static int todo[(2<<10)*10+100]; static int todoN; static int Switch[10][10]; static int door[10][10]; static void go(int code,int stepped) { if (steps[code] && steps[code]<=stepped) return; steps[code]=stepped; todo[todoN++]=code; } int main(void) { int i,R,D,S,s,d,I,J,K,L,r; for (;;) { i=scanf("%d %d %d\n",&R,&D,&S); assert(i==3); if (R==0 && D==0 && S==0) break; ZERO(steps); ZERO(todo); ZERO(Switch); ZERO(door); steps_best=INT_MAX; todoN=0; for (d=0;d=1 && I<=R); I--; assert(J>=1 && J<=R); J--; door[I][J]=1; door[J][I]=1; } for (s=0;s=1 && K<=R); K--; assert(L>=1 && L<=R); L--; Switch[K][L]=1; } go(CODE(1<<0,0),1); while (todoN) { int code=todo[--todoN]; int lights=LIGHTS(code),pos=POS(code); int stepped=steps[code]; for (r=0;r