POJ 3984 迷宫问题

来源:互联网 发布:淘宝网店装修视频教程 编辑:程序博客网 时间:2024/06/16 17:49

题意:输入一个5*5的迷宫地图,输出一条最短路径。

思路:BFS+路径输出。裸的路径输出问题。开一个存路径的结构体,每个点存来的点的坐标,然后递归到起点,回溯输出路径。

//#include<bits/stdc++.h>#include<iostream>#include<cstdio>#include<queue>using namespace std;int n, m, MAP[15][15], dir[4][2] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};bool vis[15][15];pair<int, int> path[15][15], NOW;void bfs(){    queue<pair<int, int> > q;    q.push(make_pair(0, 0));    while (!q.empty())    {        NOW = q.front();        q.pop();        if (NOW.first == 4 && NOW.second == 4) return ;        for (int i = 0; i < 4; i++)        {            int X = NOW.first + dir[i][0], Y = NOW.second + dir[i][1];            if (X >= 0 && Y >= 0 && X < n && Y < m && MAP[X][Y] == 0 && !vis[X][Y])            {                q.push(make_pair(X, Y));                path[X][Y].first = NOW.first, path[X][Y].second = NOW.second;            }        }    }}void putout(int x, int y){    if (x == 0 && y ==0)    {        printf("(0, 0)\n");        return ;    }    putout(path[x][y].first, path[x][y].second);    printf("(%d, %d)\n",x ,y);}int main(){    n = 5; m = 5;    for (int i = 0; i < n; i++)    {        for (int j = 0; j < m; j++)        {            scanf("%d",&MAP[i][j]);        }    }    bfs();    putout(4, 4);    return 0;}/*0 1 0 0 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0*/


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 现金流动负债比率 嗜酸性粒细胞比率偏高 噬酸性粒细胞比率高 营运资本配置比率 孕妇淋巴细胞比率偏低 嗜酸性粒细胞比率偏低 血常规淋巴细胞比率低 水域占地球表面积约多少比率 比甲 比甲积分榜 比甲图片 菲律宾比索 比索洛尔 比索 比索洛尔片 墨西哥比索 阿根廷比索 富马酸比索洛尔 富马酸比索洛尔片 富马酸比索洛 阿根廷比索大跌 菲律宾比索图片 富马酸比索片会猝死 富马酸比索片吃多久可以停药 富马酸比索洛尔片说明书 富马酸比索洛片 比索洛尔价格 富马酸比索洛尔片价格 康析富马酸比索洛尔片 比索洛尔和美托洛尔的区别 墨西哥比索兑换美元 阿根廷比索兑换美元 富马酸比索洛尔价格 泽比纳 比肩 比肩齐声 比肩继踵 比肩迭迹 比肩随踵 比肩并起 一人之力肩比神明