#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 marked[(2<<10)*10]; static int steps[(2<<10)*10]; static int todo[(2<<10)*10+100]; static int todoN; static int Switch[10][10]; static int door[10][10]; static int round=1; static void go(int code,int stepped) { if (marked[code]==round && steps[code]<=stepped) return; marked[code]=round; steps[code]=stepped; todo[todoN++]=code; } int main(void) { int i,R,D,S,s,d,I,J,K,L,r; int want; for (;;) { round++; i=scanf("%d %d %d\n",&R,&D,&S); assert(i==3); if (R==0 && D==0 && S==0) break; todoN=0; for (d=0;d=1 && I<=R); I--; assert(J>=1 && J<=R); J--; door[I][J]=round; door[J][I]=round; } for (s=0;s=1 && K<=R); K--; assert(L>=1 && L<=R); L--; Switch[K][L]=round; } 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