#include #include #include #include #include #include #include using namespace std; #define mem(a,t) memset(a,t,sizeof(a)) #define LL long long const int inf=0x1f1f1f1f; const int mod=1000000007; #define N 1005 int n,m,k; int g[N][N]; int e[N][N]; struct node { int x,y,t,v; bool friend operator<(node a,node b) { return a.t>b.t; } }; int dx[8]={0,1,1,1,0,-1,-1,-1}; int dy[8]={1,1,0,-1,-1,-1,0,1}; int dix[8]={1,2,2,1,-1,-2,-2,-1}; int diy[8]={2,1,-1,-2,-2,-1,1,2}; int judge(int x,int y) { if(x<1||x>n||y<1||y>m) return 0; return 1; } void inti() { int i,j; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { g[i][j]=e[i][j]=-1; } } } int bfs(int x1,int x2,int y1,int y2) { int i,di,dj; priority_queueq; node cur,next; cur.x=x1; cur.y=y1; cur.t=0; cur.v=1; //国王 q.push(cur); cur.x=x2; cur.y=y2; cur.t=0; cur.v=-1; //骑士 q.push(cur); g[x1][y1]=0; e[x2][y2]=0; while(!q.empty()) { cur=q.top(); q.pop(); if(cur.t>k) return k+1; next.v=cur.v; next.t=cur.t+1; if(cur.v==1) { if(e[cur.x][cur.y]>-1) { //printf("gg= %d %d \n",cur.t,e[cur.x][cur.y]); if(cur.t%2==e[cur.x][cur.y]%2) return cur.t; } for(i=0;i<8;i++) { next.x=di=cur.x+dx[i]; next.y=dj=cur.y+dy[i]; if(judge(di,dj)&&g[di][dj]==-1) { q.push(next); g[di][dj]=next.t; } } } else { if(g[cur.x][cur.y]>-1) { //printf("ee=%d %d \n",cur.t,g[cur.x][cur.y]); if(cur.t%2==g[cur.x][cur.y]%2) return cur.t; } for(i=0;i<8;i++) { next.x=di=cur.x+dix[i]; next.y=dj=cur.y+diy[i]; if(judge(di,dj)&&e[di][dj]==-1) { q.push(next); e[di][dj]=next.t; } } } } return k+1; } int main() { int T,cnt=1; int x1,x2,y1,y2; int ans; //printf("%.2f\n",tan(45*1.0/180*PI)); scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&k); scanf("%d%d",&x1,&y1); scanf("%d%d",&x2,&y2); inti(); ans=bfs(x1,x2,y1,y2); printf("Case #%d:\n",cnt++); if(ans<=k) printf("%d\n",ans); else printf("OH,NO!\n"); } return 0; }