Title: Give you a Matrix, ' X ' is your starting position, ' G ' is the location of the treasure, ask at least how many steps can take all the treasure, and finally return to the starting position. Note: When there is no treasure, output 0 ====================================================================================
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<map>using namespaceStd;typedefLong LongLL;Const intINF = 1e9+7;Const intMAXN =40000;intdp[maxn][ -], M, N, K;///status, where it is currently locatedCharmaps[ -][ -];structpoint{intx, y;} p[ -], Star;intGetlen (Point A, point B) {intlen = min (abs (a.x-b.x), ABS (a.y-b.y)); Len+ = MAX (ABS (a.x-b.x), ABS (A.Y-B.Y))-Len; returnLen;}intDFS (intStaintX///status, where it is now{ if(dp[sta][x]! =-1)returnDp[sta][x]; DP[STA][X]=INF; for(intI=0; i<k; i++) { if((sta& (1<<i)) && I! =x) {dp[sta][x]= Min (dp[sta][x], DFS (sta-(1<<X), I) +Getlen (P[x],p[i])); } } //printf ("dp[%d][%d] =%d\n", STA, X, dp[sta][x]); returndp[sta][x];}intMain () {intT, CAS =1; scanf ("%d", &T); while(T--) {scanf ("%d%d", &n, &m); K=0; Memset (DP,-1,sizeof(DP)); for(intI=0; i<n; i++) {scanf ("%s", Maps[i]); for(intj=0; j<m; J + +) { if(Maps[i][j] = ='g') p[k].x= i, p[k++].y =J; if(Maps[i][j] = ='x') star.x= i, star.y =J; } } for(intI=0; i<k; i++) { intSTA =1<<i; Dp[sta][i]=Getlen (Star, p[i]); } intAns = INF, Lim = (1<<K)-1; for(intI=0; i<k; i++)///last place to stayans = min (Ans,dfs (lim,i) +Getlen (Star,p[i])); if(k = =0) ans=0; printf ("Case %d:%d\n", CAS + +, ans); } return 0;}
1057-collecting Gold (State compression DP)