• POJ3984 迷宫问题 —— BFS


    题目链接:http://poj.org/problem?id=3984


    迷宫问题
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 24560   Accepted: 14338

    Description

    定义一个二维数组: 
    int maze[5][5] = {
    
    	0, 1, 0, 0, 0,
    
    	0, 1, 0, 1, 0,
    
    	0, 0, 0, 0, 0,
    
    	0, 1, 1, 1, 0,
    
    	0, 0, 0, 1, 0,
    
    };

    它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

    Input

    一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

    Output

    左上角到右下角的最短路径,格式如样例所示。

    Sample Input

    0 1 0 0 0
    0 1 0 1 0
    0 0 0 0 0
    0 1 1 1 0
    0 0 0 1 0

    Sample Output

    (0, 0)
    (1, 0)
    (2, 0)
    (2, 1)
    (2, 2)
    (2, 3)
    (2, 4)
    (3, 4)
    (4, 4)




    题解:

    简单的BFS输出路径。

    由于每个格子只能从一个格子转移过来(开始格子除外),所以开了fa[x][y][2]三维数组来存格子xy的上一个格子。


    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const int INF = 2e9;
    16 const LL LNF = 9e18;
    17 const int MOD = 1e9+7;
    18 const int MAXN = 5+10;
    19 
    20 struct node
    21 {
    22     int x, y;
    23 };
    24 int m[MAXN][MAXN], fa[MAXN][MAXN][2], vis[MAXN][MAXN];
    25 int dir[4][2] = { 1,0,0,1,-1,0,0,-1 };
    26 
    27 queue<node>que;
    28 void bfs()
    29 {
    30     while(!que.empty()) que.pop();
    31     node now, tmp;
    32     now.x = now.y = 0;
    33     vis[0][0] = 1;
    34     que.push(now);
    35 
    36     while(!que.empty())
    37     {
    38         now = que.front();
    39         que.pop();
    40 
    41         if(now.x==4 && now.y==4)
    42             return;
    43         for(int i = 0; i<4; i++)
    44         {
    45             tmp.x = now.x + dir[i][0];
    46             tmp.y = now.y + dir[i][1];
    47             if(tmp.x>=0 && tmp.x<=4 && tmp.y>=0 && tmp.y<=4 && !vis[tmp.x][tmp.y] && !m[tmp.x][tmp.y])
    48             {
    49                 vis[tmp.x][tmp.y] = 1;
    50                 fa[tmp.x][tmp.y][0] = now.x;
    51                 fa[tmp.x][tmp.y][1] = now.y;
    52                 que.push(tmp);
    53             }
    54         }
    55     }
    56 }
    57 
    58 void Print(int x, int y)
    59 {
    60     if(x!=0 || y!=0)
    61         Print(fa[x][y][0], fa[x][y][1]);
    62     printf("(%d, %d)
    ", x,y);
    63 }
    64 
    65 int main()
    66 {
    67     for(int i = 0; i<5; i++)
    68         for(int j = 0; j<5; j++)
    69             scanf("%d",&m[i][j]);
    70 
    71     bfs();
    72     Print(4,4);
    73 }
    View Code


  • 相关阅读:
    009-Python-面向对象
    008-Python-模块
    007-Python函数-装饰器
    006-Python函数
    005-Python字典
    003-python列表
    PyCharm之python书写规范--消去提示波浪线
    001-python基础
    Java基础总结(一)
    High ASCII字符从bat文件到dos控制台的转化问题
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538597.html
Copyright © 2020-2023  润新知