HDOJ 2612 Find a way【最短路 雙重bfs】

HDOJ 2612 Find a way【最短路 雙重bfs】

題目鏈接 http://acm.hdu.edu.cn/showproblem.php?pid=2612


1、Y與M在KFC約定見面,在此之前不能見面,所以對於Y和M個人來說,M和Y所在的位置是不能走的
2、有一些KFC不能到達,步數爲0,這個時候步數非常小但是不是正確答案,最後排除一下
3、步數計數方式:visY[xx][yy] = visY[x][y]+1;(父結點步數+1)


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXL = 205; // 矩陣最大寬度
char maze[MAXL][MAXL]; // 迷宮
int visY[MAXL][MAXL]; // 訪問標記(Y到達座標點所需步數)
int visM[MAXL][MAXL]; // 訪問標記(M到達座標點所需步數)
int n, m; // 迷宮的長和寬
int dirx[4] = {1, 0, -1, 0};
int diry[4] = {0, 1, 0, -1}; // 移動方向(下 右 上 左)
typedef struct point{
    int x, y;
}P; // 座標
queue<P> path; // 路徑
P Y, M;
const int INF = 1000;

void Output(){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            printf("%c", maze[i][j]);
        }
        printf("\n");
    }
}

void Input(){
    memset(maze, 0, sizeof(maze));
    memset(visY, 0, sizeof(visY));
    memset(visM, 0, sizeof(visM));
    for(int i = 0; i < n; i++){
            scanf("%s", &maze[i]);
        }
    //Output();
}

bool go(int xx, int yy){
    if(xx >= 0 && xx < n && yy >= 0 && yy < m && (maze[xx][yy] == '.' || maze[xx][yy] == '@')){
        return true;
    }
    else
        return false;
}

void bfs(P start, char flag){
    P current, next;
    int x, y, xx, yy; // x,y是當前座標 xx,yy是下一步的座標

    path.push(start);

    while(!path.empty()){ // 如果隊列不爲空
        current = path.front();
        x = current.x;
        y = current.y;
        for(int i = 0; i < 4; i++){
            xx = x + dirx[i];
            yy = y + diry[i];
            if(flag == 'Y' && visY[xx][yy] == 0 && go(xx, yy)){
                visY[xx][yy] = visY[x][y]+1;
                next.x = xx;
                next.y = yy;
                path.push(next);
            }
            else if(flag == 'M' && visM[xx][yy] == 0 && go(xx, yy)){
                visM[xx][yy] = visM[x][y]+1;
                next.x = xx;
                next.y = yy;
                path.push(next);
            }
        }
        path.pop();
    }
}

//void OutputY(){
//    for(int i = 0; i < n; i++){
//        for(int j = 0; j < m; j++){
//            printf("%d\t", visY[i][j]);
//        }
//        printf("\n");
//    }
//}
//
//void OutputM(){
//    for(int i = 0; i < n; i++){
//        for(int j = 0; j < m; j++){
//            printf("%d\t", visM[i][j]);
//        }
//        printf("\n");
//    }
//}

void Resolve(){
    for(int i = 0; i < n; i++){ // 找到Y & M 所在座標
        for(int j = 0; j < m; j++){
            if(maze[i][j] == 'Y'){
                Y.x = i;
                Y.y = j;
            }
            if(maze[i][j] == 'M'){
                M.x = i;
                M.y = j;
            }
        }
    }
    bfs(Y, 'Y');
    bfs(M, 'M');
//
//    printf("\n");
//    OutputY();
//    printf("\n");
//    OutputM();
//    printf("\n");

    int minStep = INF;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(maze[i][j] == '@'){
                if((visY[i][j] + visM[i][j] < minStep) && (visY[i][j] + visM[i][j] != 0)) minStep = visY[i][j] + visM[i][j];
            }
        }
    }
    printf("%d\n", minStep * 11);
}

int main(){
    while(~scanf("%d%d", &n, &m)){
        Input();
        Resolve();
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章