1057-collecting Gold (State compression DP)

Source: Internet
Author: User

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&LT;&LT;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&LT;&LT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.