#include int map[1000][1000]; int w,h,n; int move(int x,int y,int f){ //static int cnt=0; int r=0; if (x==w-1 && y==h-1) return 0; if (x<0||y<0||w<=x||h<=y) return 0x80000000; //printf(">%d %d %d\n",x,y,map[x][y]);if (cnt++>5)exit(0); if ((map[x][y]&0b0001)&&(f!=0b0001)) r=move(x-1,y,0b0100)+1; if (r>0) return r; if ((map[x][y]&0b0010)&&(f!=0b0010)) r=move(x,y-1,0b1000)+1; if (r>0) return r; if ((map[x][y]&0b0100)&&(f!=0b0100)) r=move(x+1,y,0b0001)+1; if (r>0) return r; if ((map[x][y]&0b1000)&&(f!=0b1000)) r=move(x,y+1,0b0010)+1; if (r>0) return r; return 0x80000000; } main(){ int i,j,k,m,x,y,f,t,vx,vy,v,u; int ada[1000]; scanf("%d%d%d",&w,&h,&n); for (x=0;x0){ if (t-f>=w){ vx=0;vy=1;v=8;u=2; }else{ vx=1;vy=0;v=4;u=1; } }else{ if (t-f<=-w){ vx=0;vy=-1;v=2;u=8; }else{ vx=-1;vy=0;v=1;u=4; } } //printf("%d\n",-1); while (1){ //printf("%d,%d %d\n",f%w,f/w,v); map[f%w][f/w]|=v; f+=vx+vy*w; //printf("%d,%d %d\n",f%w,f/w,u); map[f%w][f/w]|=u; if (f==t) break; } } } //for (y=0;y