The main thing you don’t understand is std::pair
http://www.cplusplus.com/reference/utility/pair/ which combines two types (or two of the same type) into the type of one object made from those two.
Also notice the using namespace std;
that lets the programmer leave off the std::
. That is poor style for many reasons and you shouldn’t do it yourself, but should understand when others do.
so std::pair<int, int>
is a type. Objects of that type consist of two int
s the first of which is named first
and the second named second
. So it is a type very similar to:
struct { int first; int second; };
If cur
had been an object of the above kind of struct
, I assume you would understand what cur.first
and cur.second
and if you knew what cur.first
is then you would know what cur.first-1
is (no magic meaning, just computing a value one less than the value of cur.first
And, yes, that code is finding a path between two points on a grid.
The programmer chose to represent a position {row,column}
as a pair
, which is a bit lazy. Then compounded that by failing to comment the fact that the pair<int,int>
is used to hold {row,column}
So all those >0
and <4
checks are ugly ways of testing whether a specific neighbor of the current {row,col}
really exists (as opposed to being off the edge of the map).
Next you’ll want to read about std::queue
http://www.cplusplus.com/reference/queue/queue/
One of those is used to hold all the reachable {row,col}
pairs for which the distance from {0,0}
has been computed but for which the distance from {0,0}
of some neighbor might not have been computed. So the basic operation is take one of those from one end of the queue; check all of its neighbors; and for any neighbor whose distance is newly discovered, insert in the other end of the queue.