Singled female cat burglar

 

 

 

 Why might first to bfs can find the shortest path to understanding, if not, I recommend a look at the up main explanation https://www.bilibili.com/video/av25761720?from=search&seid=17048517788278663520 (roughly understandable to make

Every time updates from all neighboring nodes, equivalent to update layer by layer, from that level, so is the shortest)

Then this question of data is small, without optimization, that is the order of the input and output than the pit

#include <bits / STDC ++ H.> 

the using namespace STD; 

typedef Long Long LL; 

int n-, m; 
int SX, SY, EX, EY; // y coordinates are x-coordinate of the starting point, x coordinate y coordinate of the end point 
int dis [205] [205]; // store from sx, sy distance to the other points, a two-dimensional array subscript is the point of coordinates, when the distance is not -1 
BOOL VIS [205] [205]; // store the point is accessed, to avoid visiting the 
string maze [205] // map 
int dx [8] = {-1 , 1, 0, 0, -1, -1, 1, 1}, dy [8] = {0 , 0, -1, 1, -1, 1, 1, -1}; // eight directions, namely, up and down from left to right lower right upper left upper right lower left, take the same subscripts dx dy can have the current coordinate with to Laid 
																																						// predetermined direction 
void BFS () { 
	for (int I = 0; I <n-; I ++) // this step is initialized, a start point of the distance are all set to -1, the access indicator are located is to false 
		for (int J = 0; J <m; J ++) 
			= -1 DIS [I] [J], VIS [I] [J] = to false; 
	Queue <pair <int, int >> Q; // BFS queue 
	q.push ({sx, sy}) ; // start point to queue
	dis [sx] [sy] = 0; // start point to point distance is 0 
	VIS [SX] [SY] = to true; // origin visited, it is labeled to true 
	the while (q.empty ()!) { 
		pair <int, int> t = q.front (); q.pop (); // point taken from the queue 
		for (int I = 0; I <. 8; I ++) { 
			// this effect loop is traversed eight directions to see whether the hit female cat burglar 

			// can be seen here dx, dy how the function 
			int = t.first NX + DX [I]; 
			int NY t.second + Dy = [I]; 
			the while (. 1 ) { 
				IF (NX && NY == == EX EY) { 
					// described herein hit 
					COUT << DIS [t.first] [t.second] << endl; 
					return; 
				} 
				IF (NX <0 || NX > = n || ny <0 || ny> = m || maze [nx] [ny] == 'X') break; // hindered described herein, the darts stop 

				// continue to the depth direction of 
				nx + DX = [I]; 
				NY + Dy = [I]; 
			} 
		} 
		// done after the above scan, can be described to this point did not hit the node, the node continues processing the vicinity of the node, queues 
		for (int i = 0;i<4; i++){
			// four directions 
			int = t.first NX + DX [I]; 
			int NY t.second + Dy = [I]; 
			IF (NX> NX = 0 && <&& n-NY> = 0 && NY <m && Maze [NX] [NY]! = 'X-' &&! VIS [NX] [NY]) { 
				// the four directions of the lattice join the queue, the current starting node to the current distance is a distance + 1'd 
				q.push ( NX {,} NY); 
				DIS [NX] [NY] = DIS [t.first] [t.second] +. 1; 
				VIS [NX] [NY] = to true; 
			} 
		} 
	} 
	// return all not yet completed the Description hit 
	"! Impossible" COUT << << endl; 
} 

int main () { 
	iOS :: sync_with_stdio (to false); 
	CIN >> >> n-m; 
	for (int I = 0; I <n-; I ++) // is input, needless to say 
		cin >> maze [i];
	while(cin >> ex >> ey >> sx >> sy){
		IF (SX SY == 0 == 0 && && EX == 0 == 0 && EY) BREAK; 
		sx--; sy -; ex--; ey--; 
		bfs (); // action is bfs to give sx, sy as a starting point, a point to each other the shortest distance 
	} 
	return 0; 
}

 

Guess you like

Origin www.cnblogs.com/ssNiper/p/11450483.html