6.4.2 走迷宫

image

bfs 最短路径

#include<cstdio>

#include<cstring>

#include<iostream>

#include<string>

#include<algorithm>

#include<queue>

using namespace std;

const int N=105;

int n, m, rs, cs, rt, ct;

typedef int A[N][N];

A maze, vis, dist, last_dir;



int dr[] = {-1,1,0,0};

int dc[] = {0,0,-1,1};

char name[] = "UDLR";



struct Pos {

    Pos(int r=0, int c=0): r(r), c(c) {}

    bool operator == ( const Pos& rhs) {

        return r==rhs.r && c==rhs.c;

    }

    bool operator != ( const Pos& rhs) {

        return !(*this==rhs);

    }

    int r;

    int c;

};



Pos fa[N][N];



void bfs() {

    queue<Pos> q;

    Pos s=Pos(rs, cs);

    q.push(s);

    vis[rs][cs]=1; dist[rs][cs]=0; fa[rs][cs]=s;

    while(!q.empty()) {

        Pos t=q.front(); q.pop();

        if(t.r==rt && t.c==ct)

            return;

        for(int i=0;i<4;i++) {

            int nr,nc;

            nr=t.r+dr[i]; 

            nc=t.c+dc[i]; 

            if(nr>=0 && nr<n && nc>=0 && nc< m && maze[nr][nc] && !vis[nr][nc]) {

                vis[nr][nc]=1;

                dist[nr][nc]=dist[t.r][t.c]+1;

                fa[nr][nc]=t;

                last_dir[nr][nc]=i;

                q.push(Pos(nr, nc));

            }

        }

    }

}



void print_path(const Pos& p) {

    if(fa[p.r][p.c]!=p) {

        print_path(fa[p.r][p.c]);

        printf("%c", name[last_dir[p.r][p.c]]);

    }

}



void dump_dist() {

    printf("%s\n", __FUNCTION__);

    for(int i=0;i<n;i++) {

        for(int j=0;j<m;j++)

            printf("%2d ", dist[i][j]);

        printf("\n");

    }

    printf("\n");

}



int main()

{

    freopen("./maze.in", "r", stdin);

    scanf("%d%d%d%d%d%d", &n, &m, &rs, &cs, &rt, &ct);

    for(int i=0;i<n;i++)

        for(int j=0;j<m;j++)

            scanf("%d", &maze[i][j]);

    bfs();

    dump_dist();

    print_path(Pos(rt, ct));



    return 0;

}

 

lrj代码:  坐标转换思想可取

#include<stdio.h>

#include<string.h>

#define MAXN 105

int n, m, xs, ys, xt, yt;

int maze[MAXN][MAXN], vis[MAXN][MAXN], fa[MAXN][MAXN], dist[MAXN][MAXN], last_dir[MAXN][MAXN], num[MAXN][MAXN];



int dx[] = {-1,1,0,0};

int dy[] = {0,0,-1,1};

char name[] = "UDLR";



int q[MAXN*MAXN];

void bfs(int x, int y) {

  int front=0, rear=0, d, u;

  u = x*m+y;

  vis[x][y] = 1; fa[x][y] = u; dist[x][y] = 0;

  q[rear++] = u;

  while(front<rear) {

    u = q[front++];    

    x = u/m; y = u%m;

    for(d = 0; d < 4; d++) {

      int nx = x+dx[d], ny = y+dy[d];

      if(nx>=0 && nx<n && ny>=0 && ny<m && maze[nx][ny] && !vis[nx][ny]) {

        int v = nx*m+ny;

        q[rear++] = v;

        vis[nx][ny] = 1;

        fa[nx][ny] = u;

        dist[nx][ny] = dist[x][y]+1;

        last_dir[nx][ny] = d;

      }

    }

  }

}



void print_path(int x, int y) {

  int fx = fa[x][y]/m;

  int fy = fa[x][y]%m;

  if(fx != x || fy != y) {

    print_path(fx, fy);

    putchar(name[last_dir[x][y]]);

  }

}



int dir[MAXN*MAXN];

void print_path2(int x, int y) {

  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]]);

}



int main() {

  int i, j;

  scanf("%d%d%d%d%d%d", &n, &m, &xs, &ys, &xt, &yt);

  for(i = 0; i < n; i++)

    for(j = 0; j < m; j++)

      scanf("%d", &maze[i][j]);

  memset(vis, 0, sizeof(vis));

  bfs(xs, ys);

  print_path(xt, yt);

  putchar('\n');

  print_path2(xt, yt);

  putchar('\n');

  return 0;

}

你可能感兴趣的:(迷宫)