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 was integers N and M, meaning that 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#@##....#### .... 0 sample Output-1 5 source Asia Hangzhou Regional contest &NBSP;&NBSP; I have been doing the search again recently posture do not know whether there has been a lot of can be this question or pit for a long time just start is want to constantly BFS find the nearest one point record steps and then update the starting point to continue BFS but there are bugs have counter example GG&NBSP;&NBSP;&NB sp; first find the shortest way between the treasure and the starting point of the K. BFS processing Then DFS finds the shortest link this place is just beginning to use and check the set but the topic of the requirements of the Unicom is the end-to-end ggdfs+ bfs
#include <bits/stdc++.h>using namespacestd;Chara[ the][ the];intmp[ the][ the];map<int,int>Flag;intmpp[ the][ the];intshorpath[6][2];intdis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};intn,m;intK;ints_x,s_y;intparent[ -];intjishu=0;inta[6][6];intRe=100000;intsum;structnode{intx; inty; intstep;};intFind (intN) { if(n!=Parent[n]) n=Find (Parent[n]); returnN;}voidUnio (intSsintBB) {SS=Find (ss); BB=Find (BB); if(ss!=BB) Parent[ss]=BB;} Queue<node>Q;node N,now;voidInit_ () { for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) Mpp[i][j]=mp[i][j];}intBFsintAintBintCintd) {Init_ (); while(!Q.empty ()) {Q.pop (); } n.x=A; N.Y=b; N.step=0; Q.push (N); MPP[A][B]=0; while(!Q.empty ()) { Now=Q.front (); Q.pop (); if(now.x==c&&now.y==d)returnNow.step; for(intI=0;i<4; i++) { intaa=now.x+dis[i][0]; intbb=now.y+dis[i][1]; if(aa>0&&aa<=n&&bb>0&&bb<=m&&MPP[AA][BB]) {MPP[AA][BB]=0; N.x=AA; N.Y=BB; N.step=now.step+1; Q.push (N); } } } return-1;}voidinit () { for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) Mp[i][j]=0; for(intI=0; i<= -; i++) Parent[i]=i; Re=100000;}/*void Kruskal () {int re=0; for (int i=0;i<jishu;i++) {int qq=a[i].s; int ww=a[i].e; cout<<re<<endl; if (Find (QQ)!=find (WW)) {Unio (QQ,WW); re+=a[i].x; }} printf ("%d\n", re);}*/voidDfsintNintCE) { if(ce==k) {if(sum<re) {Re=sum; } //printf ("%d\n", re); return ; } for(intI=0; i<=k;i++) { if(i!=n&&flag[i]==0) {sum+=A[n][i]; Flag[i]=1; DFS (I,ce+1); Sum-=A[n][i]; Flag[i]=0; } }}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) { if(n==0&&m==0) Break; Init (); GetChar (); for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) {scanf ("%c",&A[i][j]); if(a[i][j]=='@') {s_x=i; S_y=J; } if(a[i][j]=='.'|| a[i][j]=='@') Mp[i][j]=1; } getchar (); } scanf ("%d",&k); shorpath[0][0]=s_x; shorpath[0][1]=s_y; for(intI=1; i<=k;i++) scanf ("%d%d", &shorpath[i][0],&shorpath[i][1]); Jishu=0; for(intI=0; i<=k;i++) for(intj=0; j<=k;j++) {A[i][j]=BFS (shorpath[i][0],shorpath[i][1],shorpath[j][0],shorpath[j][1]); } Sum=0; flag[0]=1; DFS (0,0); printf ("%d\n", re); }return 0;}
View Code
HDU 4771 (DFS+BFS)