Easy Graph problem?
Topic Abstraction: Give you a matrix of n * M. Each lattice is a number or an obstacle ' * '. First question: The shortest path from the starting point to the end point. Second question: Require the same direction can not walk two times, that is, each to arrive at a lattice, need to change direction. Ask the shortest circuit. But there is no output-1.
Analysis: Can be directly BFS search. The first question has a minv[ms][ms] array record the shortest distance to reach (i,j). Second asked with Flag[ms][ms][4] and record (i,j) lattice K direction whether to walk.
Due to the 2<=n,m<=500, the range is larger. So priority queue optimization is required. Otherwise time out.
1#include <cstdio>2#include <algorithm>3#include <queue>4 using namespacestd;5typedefLong LongLL;6 Const intINF =0x5fffff;7 Const intMS =505;8 9 structNode {Ten intx, Y, dir, cost; One BOOL operator< (ConstNode & A)Const { A returnCost >A.cost; - } - }; the intPic[ms][ms]; - intdir[4][2] = {0,1,1,0,0,-1,-1,0}; - intflag[ms][ms][4]; - intMinv[ms][ms]; + - intN, M, R1, C1, R2, C2; + intKase =1; A node s,t; at - voidbfs1 () { -MINV[R1][C1] =PIC[R1][C1]; -S.x =R1; -S.y =C1; -S.cost =PIC[R1][C1]; inPriority_queue<node>que; - Que.push (s); to while(!Que.empty ()) { +s =que.top (); - Que.pop (); the for(inti =0; I <4; i++) { * intx = s.x + dir[i][0]; $ inty = s.y + dir[i][1];Panax Notoginseng if(X >0&& x <= n && y >0&& y <= M &&Pic[x][y]) { -T.x =x; theT.y =y; +T.cost = S.cost +Pic[x][y]; A if(x = = R2 && y = =C2) { theprintf"%d", t.cost); + return ; - } $ if(T.cost <Minv[x][y]) { $Minv[x][y] =T.cost; - Que.push (t); - } the } - }Wuyi } theprintf"-1"); - } Wu - voidbfs2 () { AboutS.x =R1; $S.y =C1; -S.cost =PIC[R1][C1]; -Priority_queue<node>que; - for(inti =0; I <4; i++) { AFlag[r1][c1][i] =1; + intx = s.x + dir[i][0]; the inty = s.y + dir[i][1]; - if(X >0&& x <= n && y >0&& y <= M &&Pic[x][y]) { $Flag[x][y][i] =1; theT.x =x; theT.y =y; theT.cost = S.cost +Pic[x][y]; theT.dir =i; - Que.push (t); in } the } the while(!Que.empty ()) { Abouts =que.top (); the Que.pop (); the for(inti =0; I <4; i++) { the if(i = =S.dir) + Continue; -T.x = s.x + dir[i][0]; theT.y = S.y + dir[i][1];Bayi if(T.x >0&& t.x <= n && t.y >0&& T.y <= m &&Pic[t.x][t.y]) { theT.cost = S.cost +Pic[t.x][t.y]; theT.dir =i; - if(T.x = = R2 && T.y = =C2) { -printf"%d", t.cost); the return ; the } the if(Flag[t.x][t.y][t.dir] = =0) { theFlag[t.x][t.y][t.dir] =1; - Que.push (t); the } the } the 94 } the } theprintf"-1"); the }98 About intMain () { - CharStr[ms];101 while(SCANF ("%d%d%d%d%d%d", &n, &m, &r1, &c1, &R2, &c2)! =EOF) {102 for(inti =1; I <= N; i++)103 for(intj =1; J <= M; J + +) {104scanf"%s", str); the if(str[0] =='*') {106PIC[I][J] =0;107 }108 Else {109SSCANF (str,"%d", &pic[i][j]);//initialization Diagram the }111MINV[I][J] = INF;//(I,J) Minimum cost initialization the for(intK =0; K <4; k++)113FLAG[I][J][K] =0;//(I,J) four-direction initialization the } theprintf"Case %d:", kase++); the BFS1 ();117 BFS2 ();118printf"\ n");119 } - return 0;121}
Easy Graph problem? (A simple graph thesis?) BFS + Priority Queue