while (q.size()) { std::pair<int, int> t = q.front(); q.pop(); int x = t.first, y = t.second;
for (int i = 0; i < 4; i ++ ) { int nx = x + dx[i], ny = y + dy[i];
if (nx < 1 || nx > n || ny < 1 || ny > n) continue;
if (g[nx][ny] == 1) continue; if (dist[nx][ny] == -1) { dist[nx][ny] = dist[x][y] + 1; last[nx][ny] = {x, y}; q.push({nx, ny}); } } } } voidsolve(){ n = 5; for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) cin >> g[i][j];
bfs(); int x = n, y = n; while (last[x][y] != std::make_pair(0, 0)) { ans.push_back({x, y}); std::pair<int, int> t = last[x][y]; x = t.first, y = t.second; } ans.push_back({1, 1});
std::reverse(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i ++ ) { std::pair<int, int> t = ans[i]; cout << "(" << t.first - 1 << ", " << t.second - 1 << ")\n"; } }