nyoj 523 双向广搜

时间:2023-03-09 15:40:34
nyoj 523 双向广搜

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
/*
    用普通搜索TLE,已知起点和终点,可以考虑双向广搜或A*算法加速搜索
    双向广搜,一个方向从出发点向终点搜索,一个方向从终点向出发点搜索,搜索到相同的结点时,即找到最短路径。
*/
;
][] = {
    ,,,  -,,,
    ,,,  ,-,,
    ,,,  ,,-
};
int mp[N][N][N];
int vis[N][N][N];//访问标记: 0没有访问过  1 被从开始到终点的方向访问过  2 被终到始的方向访问过
int step[N][N][N];
int h, m, n;
int times;
struct Node {
    int z, x, y;
    int step;
    Node(int _z, int _x, int _y){
        z = _z; x = _x; y = _y;
    }
};

int bfs()
{
    ][m-][n-] == ) ;//终点可能是墙...
    memset(&vis[][][], , sizeof(vis));
    memset(&step[][][], , sizeof(step));
    Node start(, , );
    vis[][][] = ;
    Node end(h-, m-, n-);
    vis[h-][m-][n-] = ;
    queue<Node> que;
    que.push(start);
    que.push(end);
    while(!que.empty()){
        Node cur = que.front();
        que.pop();
        if(cur.step > times) continue;
        int cx = cur.x, cy = cur.y, cz= cur.z;
        ; i<; i++){
            ];
            ];
            ];
             || tz< || ty< || tz>=h || tx>=m || ty >=n) continue;
            ) continue;
            ){
                Node next(tz, tx, ty);
                step[tz][tx][ty] = step[cz][cx][cy]+;
                vis[tz][tx][ty] = vis[cz][cx][cy];//访问标记要保持一致
                que.push(next);
            } else if(vis[tz][tx][ty] != vis[cz][cx][cy]){//如果访问标记不一致,则说明来自不同方向上搜索的相遇的结点
                 <= times? step[cz][cx][cy]+step[tz][tx][ty]+: -;
            }

        }

    }
    ;
}

int main()
{
    //INPUT;
    //OUTPUT;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d%d", &h, &m, &n, &times);
        ; i<h; i++){
            ; j<m; j++){
                ; k<n; k++){
                    scanf("%d", &mp[i][j][k]);
                }
            }
        }
        printf("%d\n", bfs());
    }

    ;
}