HDU 4771 stealing Harry Potter ' s Precious (BFS)

Source: Internet
Author: User

Stealing Harry Potter ' s Precious

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1961 Accepted Submission (s): 921


Problem Description Harry 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. 

Input There 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

Output for 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

The problem is to find the smallest way to get all the treasures, if you can't get it, output-1

Violence to get all the order of the treasures, and then use BFS to search for the shortest path

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<stdlib.h>#include<queue>#include<stack>#include<algorithm>#defineLL __int64using namespacestd;Const intmaxn= -+5;Const intmax=Ten+5;Const intinf=0x3f3f3f3f;Const Doubleeps=1e-9;intdir4[4][2]={{0,1},{1,0},{0,-1},{-1,0}};intdir8[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};intdir_8[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};intN,m,k,vis[maxn][maxn],d[max][max];CharMAP[MAXN][MAXN];structnode{intx, y; intStep;} p[5];voidinit () {memset (map,0,sizeof(map));  for(intI=1; i<=n;i++) {scanf ("%s", map[i]+1);  for(intj=1; j<=m;j++)            if(map[i][j]=='@') {p[0].x=i; p[0].y=J; }} scanf ("%d",&k);  for(intI=1; i<=k;i++) scanf ("%d%d",&p[i].x,&p[i].y);}intBFS (node Star,node en) {queue<node>Q; memset (Vis,0,sizeof(VIS)); VIS[STAR.X][STAR.Y]=1; Star.step=0;    Q.push (Star);  while(!Q.empty ())        {node Now,next; now=Q.front ();        Q.pop (); if(Now.x==en.x && now.y==en.y)returnNow.step;  for(intI=0;i<4; i++) {Next.x=now.x+dir4[i][0]; Next.y=now.y+dir4[i][1]; if(0<next.x && next.x<=n &&0<next.y && next.y<=m &&!vis[next.x][next.y] && map[next.x][next.y]!='#') {Vis[next.x][next.y]=1; Next.step=now.step+1;            Q.push (next); }        }    }    return-1;}BOOLjudge () {memset (d,0,sizeof(d));  for(intI=0; i<=k;i++)    {         for(intj=i+1; j<=k;j++)        {            intdis=BFS (P[i],p[j]); if(dis==-1)return false; D[I][J]=d[j][i]=dis; }    }    return true;}intsolve () {intp[max],ans=INF;  for(intI=0; i<=k;i++) P[i]=i;  Do    {        intsum=0;  for(intI=0; i<k;i++) Sum+=d[p[i]][p[i+1]]; Ans=min (Sum,ans); } while(Next_permutation (p+1, p+1+k)); returnans;}intMain () { while(SCANF ("%d%d", &n,&m) && (n| |m)) {init (); if(judge ()) printf ("%d\n", Solve ()); Elseprintf"-1\n"); }    return 0;}
View Code

HDU 4771 stealing Harry Potter ' s Precious (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.