1057-collecting Gold

Source: Internet
Author: User

1057-collecting Gold
PDF (中文版) Statistics Forum
Time Limit:2 second (s) Memory limit:32 MB

Finally you found the city of Gold. As you is fond of gold, you start collecting them. But there is so much gold it is getting tired collecting them.

So, your want to find the minimum effort to collect all the gold.

You can describe the city as a 2D grid, where your initial position are marked by an ' x '. A empty place would be denoted by a '. And the cells which contain gold would be denoted by ' G '. In each move you can go to any 8 adjacent places inside the city.

Input

Input starts with an integer T (≤100), denoting the number of test cases.

Each case would start with a blank line and both integers, m and N (0 < M, n <) denoting the row and columns of the city respectively. Each of the next m lines would contain n characters describing the city. There'll is exactly one ' x ' in the city and at the very most positions gold.

Output

For each case of input, you had to print the case number and the minimum steps you had to take to collect all the gold an D go back to ' x '.

Sample Input Output for Sample Input

2

5 5

X....

G....

G....

.....

G....

5 5

X....

G....

G....

.....

.....

Case 1:8

Case 2:4

IDEA: State compression DP;

The first to write in the form of a pressure search WA;

The problem is a state compression dp,dp[i][j], indicating the minimum cost to end the state I to J, the starting point into a pressure, because the beginning is also the end, then the final answer is dp[(1<<CN) -1][CN-1];CN for the beginning of the label, is also the number of points

There can be 8 directions, then the minimum distance of two points is X, y two direction distance of the big one;

1#include <stdio.h>2#include <algorithm>3#include <stdlib.h>4#include <iostream>5#include <math.h>6#include <string.h>7#include <queue>8 using namespacestd;9typedefLong LongLL;Ten Charma[ A][ A]; One int__ma[ A][ A]; A intdp[1<< -][ A]; -typedefstructnode - { the         intx; -         inty; - } SS; -SS ans[ -]; + intDintNintMintNnintmm) - { +         intx = ABS (nnn); A         inty = ABS (M-mm); at         returnMax (x, y); - } - intMainvoid) - { -         intn,m; -         intT,__ca=0; inscanf"%d",&T); -          while(t--) to         { +__ca++; -                 intI, J; the                 //memset (flag,0,sizeof (flag)); *scanf"%d%d",&n,&m); $                  for(i =0; I < n; i++)Panax Notoginseng                 { -scanf"%s", Ma[i]); the                 } +                 int__n,__m; A                 intcn=0; the                  for(i =0; I < n; i++) +                 { -                          for(j =0; J < M; J + +) $                         { $                                 if(Ma[i][j] = ='x') -                                 { -__n=i; the__m=J; -                                 }Wuyi                                 if(ma[i][j]=='g') the                                 { -ans[cn].x =i; WuANS[CN].Y =J; -__MA[I][J] = cn++; About                                 } $                         } -                 } -                 intask=0; -                  for(i =0; I < (1<< -); i++) A                 { +                          for(j =0; J < -; J + +) the                         { -DP[I][J] =1e9; $                         } the                 } theans[cn].x =__n; theANS[CN].Y =__m; the__ma[__n][__m] = cn++; -dp[0| (1<< (cn-1))][cn-1] =0; in                 if(CN! =1) the                 { the                          for(i =0; I < (1&LT;&LT;CN); i++) About                         { the                                  for(intx =0; X < CN; X + +) the{if(I & (1<<x)) the                                                  for(inty =0; Y < cn; y++) +                                                 { -                                          intDi=d (ANS[X].X,ANS[X].Y,ANS[Y].X,ANS[Y].Y); theDp[i| (1<<y)][y] = min (dp[i| (1<<y)][y],dp[i][x]+di);Bayi                                                 } the                                 } the                         } -ask=dp[(1&LT;&LT;CN)-1][cn-1]; -                 } theprintf"Case %d:", __ca); theprintf"%d\n", ask); the         } the         return 0; -}

1057-collecting Gold

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.