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

来源:互联网 发布:淘宝客佣金 购物车 编辑:程序博客网 时间:2024/04/29 04:22

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;}
0 0
原创粉丝点击