#include #include #include #include #include #include #include #include #include #include using namespace std; #define LL long long #define N 1020 #define M 2020 #define eps 1e-10 #define inf 0x3f3f3f3f #define Pi acos(-1.0) #pragma comment(linker, "/STACK:1024000000,1024000000") struct node{ int x, y, d; node(int x = 0, int y = 0, int d = 0) : x(x), y(y), d(d) {} }; bool vis[N][N]; int n, m, k, g[N][N]; int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; bool in(int x, int y){ if(x > 0 && y > 0 && x <= n && y <= m) return 1; return 0; } void bfs(int x, int y){ for(int i = 0; i <= n; ++i) for(int j = 0; j <= m; ++j) vis[i][j] = 0, g[i][j] = inf; queue q; q.push(node(x, y, 0)); while(!q.empty()){ node tmp = q.front(); q.pop(); int nx = tmp.x, ny = tmp.y, d = tmp.d; if(d > k) continue; g[nx][ny] = d; vis[nx][ny] = 1; for(int i = 0; i < 8; ++i){ int cx = nx + dx[i], cy = ny + dy[i]; if(in(cx, cy) && !vis[cx][cy]){ q.push(node(cx, cy, d + 1)); vis[cx][cy] = 1; } } } } int rx[] = {1, 2, -1, -2, 1, 2, -1, -2}; int ry[] = {-2, -1, -2, -1, 2, 1, 2, 1}; void bfs2(int x, int y){ for(int i = 0; i <= n; ++i) for(int j = 0; j <= m; ++j) vis[i][j] = 0; int ans = inf; queue q; q.push(node(x, y, 0)); while(!q.empty()){ node tmp = q.front(); q.pop(); int nx = tmp.x, ny = tmp.y, d = tmp.d; vis[nx][ny] = 1; if(d <= g[nx][ny]){ if((g[nx][ny] - d) % 2 == 0) ans = min(ans, g[nx][ny]); } if(d > g[nx][ny]){ if((d - g[nx][ny]) % 2 == 0) ans = min(ans, d); } for(int i = 0; i < 8; ++i){ int cx = nx + rx[i], cy = ny + ry[i]; if(in(cx, cy) && !vis[cx][cy]){ // cout << cx << " " << cy << endl; q.push(node(cx, cy, d+1)); vis[cx][cy] = 1; } } } if(ans <= k) printf("%d\n", ans); else puts("OH,NO!"); } void debug(){ for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j) cout << g[i][j] << " "; cout << endl; } } int main(){ int cas, kk = 0; scanf("%d", &cas); while(cas--){ printf("Case #%d:\n", ++kk); scanf("%d%d%d", &n, &m, &k); int kx, ky, sx, sy; scanf("%d%d%d%d", &kx, &ky, &sx, &sy); bfs(kx, ky); //debug(); bfs2(sx, sy); } }