题意:问两个迷宫是否存在公共最短路。
题解:两个反向bfs建立层次图,一遍正向bfs寻找公共最短路
#include#include #include using namespace std;const int maxn = 500+1;int d1[maxn][maxn];int d2[maxn][maxn];char g1[maxn][maxn];char g2[maxn][maxn];int n,m;struct node{ int x,y; node(int X = 0, int Y = 0){ x = X; y = Y; }};int dx[] = { 1,-1,0,0};int dy[] = { 0,0,1,-1};//1,1void rbfs(int id){ char (*G)[maxn]; int (*vis)[maxn]; if(id == 1) G = g1, vis = d1; else G = g2, vis = d2; memset(vis,-1,sizeof(d1)); queue q; node u(n-1,m-1); q.push(u); vis[u.x][u.y] = 0; while(q.size()){ u = q.front();q.pop(); if(u.x == 0 && u.y == 0) return; for(int i = 0; i < 4; i++){ int nx = u.x + dx[i], ny = u.y + dy[i]; if(nx>=0&&nx =0&&ny q; q.push(node(0,0)); int tx = n-1, ty = m-1; while(q.size()){ node &u = q.front(); if(u.x == tx && u.y == ty) return true; for(int i = 0; i < 4; i++){ int nx = u.x + dx[i], ny = u.y + dy[i]; if(nx>=0&&nx =0&&ny