This question means there are some knights and a king on the map, and the knight and the King are going to be in a little bit and you can choose a knight and a king at a little bit and then the Knights will take the King and continue walking, asking them the minimum number of steps and what is the convergence? Consider a knight when there is no king can easily find him to each point of the shortest path A, and now he will take the king, then we can calculate him with the king to a certain point of the shortest path B, then in order to pick up the king he took the road is b-a, which we can maintain two arrays, cost[i][j] Indicates that all knights do not take the shortest number of steps required by the king to reach I, J, Kcost[i][j] indicates that there is a knight to the king to I,j, the number of steps the knight walks (also including the number of steps the King walks), then the answer is min (Cost[i][j]+kcost[i][j]), the code is as follows:
/*id:m1500293 lang:c++ Prog:camelot*/#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespacestd;intN, M;intKX, KY;intcost[ *][ *], kingcost[ *][ *];intkcost[ *][ *];intdis[ *][ *][2];intDx[] = {-2, -2, -1, -1, +1, +1, +2, +2};intDy[] = {-1, +1, -2, +2, -2, +2, -1, +1};BOOLInsideintXintY) {return(x>=0&&x<n&&y>=0&&y<l); }structstate{intx, y, flog; State () {} state (intXintYintflog): X (x), Y (y), flog (flog) {}};intinque[ *][ *][2];voidBFsintKnxintkny) {memset (Inque,0,sizeof(Inque)); memset (DIS,0x3f,sizeof(DIS)); Queue<State>que; dis[knx][kny][0] =0; inque[knx][kny][0] =1; Que.push (State (KNX, Kny,0)); while(!Que.empty ()) {State U=Que.front (); Que.pop (); Inque[u.x][u.y][u.flog]=0; intUdis =Dis[u.x][u.y][u.flog]; for(intI=0; i<8; i++) { intNX = U.x+dx[i], NY = u.y+Dy[i]; if(!inside (NX, NY))Continue; if(Dis[nx][ny][u.flog] > Udis +1) {Dis[nx][ny][u.flog]= udis+1; if(!Inque[nx][ny][u.flog]) {Inque[nx][ny][u.flog]=1; Que.push (State (NX, NY, U.flog)); } } } if(!u.flog && dis[u.x][u.y][1]>udis +Kingcost[u.x][u.y]) {dis[u.x][u.y][1] = udis+KINGCOST[U.X][U.Y]; if(!inque[u.x][u.y][1]) {inque[u.x][u.y][1] =1; Que.push (State (u.x, U.Y,1)); } } }}intMain () {Freopen ("camelot.in","R", stdin); Freopen ("Camelot.out","W", stdout); scanf ("%d%d", &m, &N); Chartp[5];inttpy; scanf ("%s%d", TP, &tpy); KX= tp[0]-'A'; KY = tpy-1; for(intI=0; i<n; i++) for(intj=0; j<m; J + +) Kcost[i][j]= Kingcost[i][j] = MAX (ABS (I-KX), ABS (J-ky)); memset (Cost,0,sizeof(cost)); while(SCANF ("%s%d", TP, &tpy) = =2) { intKNX = tp[0]-'A', Kny = tpy-1; BFS (KNX, kny); for(intI=0; i<n; i++) for(intj=0; j<m; J + +) { if(dis[i][j][0] ==0x3f3f3f3f)//Knight cannot reach this situation {Cost[i][j]=0x3f3f3f3f; Continue; } Cost[i][j]+ = dis[i][j][0]; KCOST[I][J]= Min (Kcost[i][j], dis[i][j][1]-dis[i][j][0]); } } intres =0x3fffffff; for(intI=0; i<n; i++) for(intj=0; j<m; J + +) {res= Min (res, cost[i][j]+Kcost[i][j]); } printf ("%d\n", RES); return 0;}
Usaco Camelot Good Problem