Baidu STAR Program Design Competition qualifying round: 1005-chess, Design Competition 1005-
</pre><pre name="code" class="cpp">
// The train of thought is simple: record the shortest time for the king to reach a certain point through extensive search, and record the shortest time for the warrior to reach a certain point through the dual-for loop; then look for the big one (but if the king needs to consider special circumstances when arriving first.
# Include <cstdio> # include <queue> # include <cstring> using namespace std; int n, m, k; int xk, yk, xt, yt; int ans; bool f [1010] [1010]; <span style = "font-family: Arial, Helvetica, sans-serif;"> // array size allocation problem, I am also mentally handicapped </span> const int ary [8] [2] = {-1,-2}, {-2,-1}, {-2, 1 }, {-1010}, {1,-2}, {2,-1}, {1010}, {}; int tans [] []; /// actually, this is not required. You can directly judge int kans [1010] [1010] during extensive search; typedef struct node {int x, y; int step ;} node; que Ue <Node> q; void init_tans (int x, int y) {while (! Q. empty () q. pop (); Node tmp; tmp. x = x; tmp. y = y; tmp. step = 0; q. push (tmp); f [tmp. x] [tmp. y] = true; // mark the initial vertex of the King. The while (! Q. empty () {Node * cnode = & q. front (); q. pop (); for (int I = 0; I <8; I ++) {Node tmp; tmp. x = cnode-> x + ary [I] [0]; tmp. y = cnode-> y + ary [I] [1]; tmp. step = cnode-> step + 1; if (tmp. x> = 1 & tmp. x <= n & tmp. y> = 1 & tmp. y <= m &&! F [tmp. x] [tmp. y]) {f [tmp. x] [tmp. y] = true; tans [tmp. x] [tmp. y] = tmp. step; q. push (tmp); // unexpectedly forgot to enter the queue }}} void init_kans () {for (int I = 1; I <= n; I ++) for (int j = 1; j <= m; j ++) {int tm; if (I <xk) tm = xk-I; else tm = I-xk; if (j <yk) {if (tm <yk-j) tm = yk-j;} else {if (tm <j-yk) tm = j-yk ;} kans [I] [j] = tm ;}int main () {int T; scanf ("% d", & T); for (int t = 1; t <= T; t ++) {scanf ("% d", & n, & m, & k); scanf ("% d ", & xk, & yk); SC Anf ("% d", & xt, & yt); memset (f, 0, sizeof (f); memset (tans, 0, sizeof (tans )); memset (kans, 0, sizeof (kans); init_tans (xt, yt); init_kans (); tans [xt] [yt] = 2; kans [xk] [yk] = 2; int ans = 201; for (int I = 1; I <= n; I ++) for (int j = 1; j <= m; j ++) {if (kans [I] [j]! = 0 & tans [I] [j]! = 0) {// int tmp = max (kans [I] [j], tans [I] [j]); int tmp is faulty here; <span style = "font-family: Arial, Helvetica, sans-serif;"> // The time when the warrior reaches a certain point can be> = any value of the shortest time </span>
If (kans [I] [j]> tans [I] [j]) tmp = kans [I] [j]; else {<span style = "font-family: arial, Helvetica, sans-serif; "> // special treatment should be performed on the king. The time when the king arrives at a certain point is the shortest time + a multiple of 2 (in special cases, if the King arrives in advance) </span>
if((tans[i][j]-kans[i][j])%2==0) tmp=tans[i][j]; else tmp=tans[i][j]+1; } if(ans>tmp) ans=tmp; } } printf("Case #%d:\n",t); if(ans<=k) printf("%d\n",ans); else printf("OH,NO!\n"); } return 0;}