HDU 4771 stealing Harry Potter ' s precious Dfs+bfs

Source: Internet
Author: User

Stealing Harry Potter ' s Precious problem DescriptionHarry Potter has some precious. For example, he invisible robe, his wand and his owl. When Hogwarts school was in holiday, Harry Potter had to go back to Uncle Vernon ' s home. But he can ' t bring his precious with him. As you know, Uncle Vernon never allows such magic things So Harry had to deposit he precious in the Gringotts wizarding Bank which was owned by some goblins. The bank can be considered as a nxm grid consisting of NXM rooms. Each is a coordinate. The coordinates of the Upper-left is (at), the Down-right is (n,m) and the "the" and the "Below" is ( 2, 1) ..... A 3x4 Bank Grid is shown below:



  Some rooms is indestructible and Some rooms are vulnerable. Goblins always care more about their own safety than their customers ' properties, so they live in the indestructible rooms and put customers ' properties in vulnerable rooms. Harry Potter ' s precious is also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, Uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can ' t access the indestructible rooms. He starts from a certain vulnerable-a, and then moves-Directions:north, east, south and west. Dudely knows where Harry ' s precious is. He wants to collect all Harry's precious by as less steps as possible. Moving from one the another adjacent is called a ' step '. Dudely doesn ' t want to get out of the bank before he collects all Harry ' s things. Dudely is stupid. He pay your $1,000,000 to figure out at least how many steps he must take to Get all Harry ' s precious. 

InputThere is several test cases.
In each test cases:
The first line is both integers N and M, meaning the bank is a nxm grid (0<n,m <= 100).
Then a nxm matrix follows. Each element was a letter standing for a. ' # ' means a indestructible, '. ' means a vulnerable, and the only ' @ ' means the vulnerable hostel from which dudely Starts to move.
The next line was an integer k (0 < K <= 4), indicating there was K Harry Potter ' s precious in the bank.
In next K lines, each line describes the position of a Harry Potter's precious by both integers X and Y, meaning that there is a precious in (x, y).
The input ends with N = 0 and M = 0

OutputFor each test case, print the minimum number of steps dudely must take. If dudely can ' t get all Harry ' s things, print-1.

Sample Input2 3##@#. #12 24 4#@##....#### .... 22 12 40 0

Sample Output-15

Source2013 Asia Hangzhou Regional Contest Test Instructions: give you a @ start, n*m, and give you q a gem position, find the shortest path to all gems Solving: Can find out all gems, the shortest path between the starting point, and then violence for the best solution;
///1085422276#include <bits/stdc++.h>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#defineMeminf (a) memset (A,127,sizeof (a));#defineINF 100000007inline ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){        if(ch=='-') f=-1; ch=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar (); }returnx*F;}//****************************************#defineMAXN 105intdis[maxn][maxn],vis[maxn][maxn],v[maxn],n,m,q,st,ed;intMp[maxn][maxn],mps[maxn][maxn],a[maxn],b[maxn],ans;intss[4][2]={-1,0,0,-1,1,0,0,1};BOOLCheckintXinty) {   if(x<=0|| y<=0|| x>n| | Y&GT;M)return 1;return 0;}voidBFsintXinty) {mem (VIS); memset (DIS,127/2,sizeof(DIS)); Queue<pair<int,int> >Q;   Q.push (Make_pair (x, y)); Vis[x][y]=1;d is[x][y]=0;  while(!Q.empty ()) {Pair<int,int>K; K=Q.front (); Q.pop ();  for(intI=0;i<4; i++){        intxx=k.first+ss[i][0]; intyy=k.second+ss[i][1]; if(Check (xx,yy) | | mp[xx][yy]=='#'|| VIS[XX][YY])Continue; DIS[XX][YY]=dis[k.first][k.second]+1; VIS[XX][YY]=1; Q.push (Make_pair (XX,YY)); }   }}voidFloydintXintsum) {V[x]=1; BOOLflag=1;  for(intI=1; i<=q;i++){        if(!v[i]) flag=0; }     if(flag) ans=min (ans,sum); inttmp=inf*2;  for(intI=1; i<=q;i++)    {        if(!v[i]&&mp[x][i]!=0) {Floyd (I,sum+Mps[x][i]); }} V[x]=0;}voidTest () { for(intI=1; i<=q;i++){     for(intj=1; j<=q;j++) {cout<<mps[i][j]<<" "; } cout<<Endl;}}intMain () { while(SCANF ("%d%d", &n,&m) &&n&&L) {         for(intI=1; i<=n;i++) {GetChar ();  for(intj=1; j<=m;j++) {scanf ("%c",&Mp[i][j]); if(mp[i][j]=='@') {st=i;ed=J;} }} Q=read ();  for(intI=1; i<=q;i++) {scanf ("%d%d",&a[i],&B[i]); }q++;a[q]=st;b[q]=Ed;mem (MPS);  for(intI=1; i<=q;i++) {BFS (a[i],b[i]);//test ();//return 0;             for(intj=1; j<=q;j++){                if(j!=i) {Mps[i][j]=Dis[a[j]][b[j]]; }}}mem (v);//test ();//cout<<q<<endl;Ans=inf;floyd (Q,0);if(Ans>=inf) ans=-1; printf ("%d\n", ans); }    return 0;}
Code

HDU 4771 stealing Harry Potter ' s precious Dfs+bfs

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.