Test instructions: The shortest time from S to M. ("O" Cannot Walk, ' # ' walk a flower Two units of time, '. ') Walk a unit time for a flower)
Idea: Wide search and Priority queue.
1#include <stdio.h>2#include <string.h>3#include <iostream>4#include <queue>5 #defineMAX 306 using namespacestd;7 8 structPOS9 {Ten intx; One inty; A intStep; - }; - the BOOL operator< (ConstPOS &a,ConstPOS &b) - { - returnA.step >B.step; - } + - Pos SP, EP; + CharMap[max][max]; A intdir[4][2] = {{1,0}, {-1,0}, {0,1}, {0, -1}}, M, N, ti; at - intBFS () - { -Priority_queue<pos>Q; - Pos temp, t; -temp =sp; inTemp.step =0; - Q.push (temp); to while(!q.empty ()) + { -temp =q.top (); the Q.pop (); * if(Temp.step >=ti) $ {Panax Notoginseng Continue; - } the if(temp.x = = ep.x && temp.y = = ep.y && Temp.step <ti) + { A returnTemp.step; the } + for(inti =0; I <4; i++) - { $T.x = temp.x + dir[i][0]; $T.y = temp.y + dir[i][1]; - if(T.x >=0&& t.x < n && t.y >=0&& T.y < M && Map[t.x][t.y]! ='o') - { the if(Map[t.x][t.y] = ='.') - {WuyiT.step = Temp.step +1; theMAP[T.X][T.Y] ='o'; - Q.push (t); Wu } - Else if(Map[t.x][t.y] = ='#') About { $T.step = Temp.step +2; -MAP[T.X][T.Y] ='o'; - Q.push (t); - } A Else if(Map[t.x][t.y] = ='m') + { theT.step = Temp.step +1; -MAP[T.X][T.Y] ='o'; $ Q.push (t); the } the } the } the } - return-1; in } the the intMain () About { thescanf"%d%d%d", &ti, &m, &n); the intans; the for(inti =0; I < n; i++) + { - for(intj =0; J < M; J + +) the {BayiCin>>Map[i][j]; the if(Map[i][j] = ='s') the { -sp.x =i; -SP.Y =J; the } the if(Map[i][j] = ='m') the { theep.x =i; -EP.Y =J; the } the } the }94Ans =BFS (); the if(ans = =-1) the { theprintf"55555\n");98 } About Else - {101printf"%d\n", ans);102 }103 return 0;104}
View Code
vijos-p1340 Rescue Ice-cream (wide search + priority queue)