#include #include #include #include using namespace std; #define maxn 1200 int dp[maxn][maxn][2]; int vis[maxn][maxn][2]; int n, m, k; int ca=1; int dx[]={2,2,-2,-2,-1,-1,1,1},dy[]={1,-1,1,-1,2,-2,2,-2}; void spaf(int x,int y,int k) { memset(vis,0,sizeof(vis)); memset(dp,-1,sizeof(dp)); dp[x][y][0] = 0; dp[x][y][1] = 2; queue q,p,qp; q.push(x),p.push(y),qp.push(0); q.push(x),p.push(y),qp.push(1); vis[x][y][0] = 1; vis[x][y][1] = 1; while(!q.empty()) { int i; int u1 =q.front(), u2 = p.front(),u3 = qp.front(); q.pop(),p.pop(),qp.pop(); vis[u1][u2][u3] = 0; if(dp[u1][u2][u3]>=k) continue; for(i=0;i<=7;i++) { int temp_x = u1+dx[i]; int temp_y = u2+dy[i]; if(temp_x<1||temp_x>n||temp_y<1||temp_y>m) continue; if(dp[temp_x][temp_y][u3^1]==-1|| dp[temp_x][temp_y][u3^1]>dp[u1][u2][u3]+1) { dp[temp_x][temp_y][u3^1] = dp[u1][u2][u3]+1; if(vis[temp_x][temp_y][u3^1]==0) { q.push(temp_x); p.push(temp_y); qp.push(u3^1); vis[temp_x][temp_y][u3^1]=1; } } } } } int main() { int t,i,j; scanf("%d",&t); while(t--) { memset(dp,-1,sizeof(dp)); scanf("%d %d %d",&n,&m,&k); int x_ki,y_ki,x_kn,y_kn; scanf("%d %d %d %d",&x_ki,&y_ki,&x_kn,&y_kn); spaf(x_kn,y_kn,k); int flag = 0; int minnum = 9999999; printf("Case #%d:\n",ca++); // printf("%d\n",dp[1][2][1]); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { int dis1,dis2,dis3; int aa = i-x_ki; int bb = j-y_ki; if(aa*bb>=0) { dis1 = min(abs(aa),abs(bb)); dis1+=abs(abs(aa)-abs(bb)); } else { dis1 = min(abs(aa),abs(bb)); dis1+=abs(abs(aa)-abs(bb)); } // printf("%d %d %d\n",i,j,dis1); dis2 = dp[i][j][0]; dis3 = dp[i][j][1]; if(dis1>k) continue; if(dis1%2==0) { if(dis2==-1||dis2>k) continue; minnum = min(minnum,max(dis1,dis2)); if(dis3==-1||dis3>k) continue; if(dis3+1>k) continue; minnum = min(minnum,max(dis1,dis3+1)); } else { if(dis3==-1||dis3>k) continue; minnum = min(minnum,max(dis1,dis3)); if(dis2==-1||dis2>k) continue; if(dis2+1>k) continue; minnum = min(minnum,max(dis1,dis2+1)); } } } if(minnum==9999999) printf("OH,NO!\n"); else printf("%d\n",minnum); } return 0; }