ACM: Figure BFS, Maze

Source: Internet
Author: User

Title:

Network Maze n rows M unit columns, each cell regardless of space (using 1 representation), whether it is a barrier (using 0 in order to represent). Your task is to find an action sequence that is the shortest from start to finish, where the UDLR increases, the next, left, and right moves to the next cell.

No matter what time you are in the barrier. Can not go beyond the maze.

The starting and ending points are guaranteed to be open spaces.


Analysis: The BFS of the figure.


#include <iostream> #include <string> #include <queue>using namespace std;const int maxn = 500;int maze[ MAXN][MAXN], VIS[MAXN][MAXN], DIST[MAXN][MAXN], FA[MAXN][MAXN], Last_dir[maxn][maxn];int N, M, XS, Ys, XT, yt;int dx[] = { -1, 1, 0, 0};int dy[] = {0, 0,-1, 1};char name[] = "UDLR"; void Print_path (int x, int y) {//recursively print path int FX = Fa[x]    [y]/m;    int fy = fa[x][y]% m;    if (FX! = x | | fy! = y) {print_path (FX, FY);p Utchar (Name[last_dir[x][y]); }}int dir[maxn*maxn];void print_path2 (int x, int y) {//To print the path in an iterative way int c = 0;for (;;) {int FX = Fa[x][y]/m;int fy = fa[x][y]% m;if (FX = = x && fy = = y) break;dir[c++] = Last_dir[x][y];x = FX; y = fy ;} while (c--) Putchar (Name[dir[c]);}   queue<int> q;void BFS (int x, int y) {int u = x*m+y;dist[x][y] = 0;     Initialize yourself to your own distance is 0fa[x][y] = u; The Father node of the starting point is himself.   Easy to follow the printing operation vis[x][y] = 1; Q.push (U), while (!q.empty ()) {u = Q.front (); Q.pop (); x = U/m;y = u%m;for (int d = 0; d < 4; ++d) {int NX = x + dx[d];int n y = Y + dy[d];if (NX >= 0 && NX < n && NY >= 0 && NY < m && Maze[nx][ny] &&amp    ;!vis[nx][ny]) {int v = NX * m + ny;q.push (v); Vis[nx][ny] = 1;dist[nx][ny] = Dist[x][y] + 1;                   Number of steps to walk +1fa[nx][ny] = v;             Record father knot Point last_dir[nx][ny] = D; Record the direction of this node to Father's node now}}}}int main () {cin >> n >> M >> xs >> ys >> xt >> yt;for (int i = 0 ; I < n; ++i) {for (int j = 0; j < m; ++j) {cin >> maze[i][j];}} memset (Vis, 0, sizeof (VIS)), BFS (XS, YS);p Rint_path (XT, YT), cout << endl;print_path2 (XT, YT); cout << Endl; return 0;}


ACM: Figure BFS, Maze

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.