Básicamente es lo mismo que ACWING 844. Pero aquí está la ruta de salida
Mi enfoque es emparejar un ing [x] [y], ing [x] [y] .primero, ing [x] [y] .segundo registro del punto anterior de x, y Debido a que está en orden inverso, se almacena en una estructura y se genera en orden inverso antes de que se convierta en una secuencia positiva.
#include <iostream> #include <cstdio> #include <cstring> #include <queue> usando el espacio de nombres std; typedef pair < int , int > P; // !!! const int maxn = 105 ; P ing [maxn] [ maxn]; int n, m; int g [maxn] [maxn]; int d [maxn] [maxn]; // La distancia desde cada punto hasta el punto de partida /// No camine int dx [] = { 0 , 0 , - . 1 , 1. }; int Dy [] = {1 , - 1 , 0 , 0 }; struct node { int x, y; } st [maxn]; nulo bfs () { cola <P> q; memset (d, - 1 , sizeof (d)); d [ 0 ] [ 0 ] = 0 ; q.push ({ 0 , 0 }); while (! q.empty ()) { P t = q.front (); q.pop (); para ( inti = 0 ; i < 4 ; i ++ ) { int x = dx [i] + t.first; int y = dy [i] + t.segundo; if (x> = 0 && y> = 0 && x < 5 && y < 5 && g [x] [y] == 0 && d [x] [y] == - 1 ) { d [x] [y] = d [t .primero] [t.segundo] + 1 ; ing [x] [y] .first = t.first; ing [x] [y] .segundo = t.segundo; q.push ({x, y}); } } } {retorno ; } int main () { // cin >> n >> m; para ( int i = 0 ; i < 5 ; i ++ ) para ( int j = 0 ; j < 5 ; j ++ ) cin >> g [i] [j]; bfs (); int x = 4 , y = 4 ; int tot = 0 ; while (! (x == 0 && y == 0 )) st [tot] .x = x; st [tot] .y = y; tot ++ ; int tx = x, ty = y; x = ing [tx] [ty] .first; y = ing [tx] [ty] .second; } cout << " (0, 0) " << endl; para ( int i = tot- 1 ; i> = 0 ; i-- ) printf ( " (% d,% d) \ n " , st [i] .x, st [i] .y); devuelve 0 ; }