• 走迷宫BFS算法


     1 #include<iostream>
     2 #include<queue>
     3 #include<stack>
     4 #include<iomanip>
     5 using namespace std;
     6 
     7 int map[40][40];
     8 int done[40][40];
     9 
    10 int dx[4] = { 0,0,1,-1 };
    11 int dy[4] = { 1,-1,0,0 };
    12 
    13 struct Node
    14 {
    15     int x, y, step;
    16 };
    17 
    18 int main() {
    19     int n,ex,ey;
    20     cin >> n>>ex>>ey;
    21 
    22     for (int i = 1; i <= n; i++)
    23         for (int j = 1; j <= n; j++)
    24             done[i][j] = -1;
    25 
    26     for (int i = 1; i <= n; i++)
    27         for (int j = 1; j <= n; j++)
    28             cin >> map[i][j];
    29     
    30     queue<Node> q;
    31     q.push(Node({1,1,0}));
    32     done[1][1] = 0;
    33     while (!q.empty())
    34     {
    35         //cout << q.front().x << ":" << q.front().y << ":" << q.front().step << endl;
    36         for (int i = 0; i < 4; i++)
    37         {
    38             int tx = q.front().x + dx[i];
    39             int ty = q.front().y + dy[i];
    40             
    41             if (tx>=1&&ty>=1&&tx<=n&&ty<=n&&done[tx][ty]==-1&&!map[tx][ty])
    42             {
    43                 q.push(Node({ tx,ty,q.front().step+1 }));
    44                 done[tx][ty] = q.front().step + 1;
    45             }
    46         }
    47         q.pop();
    48     }
    49 
    50     cout << endl;
    51 
    52     for (int i = 1; i <= n; i++) {
    53         for (int j = 1; j <= n; j++) {
    54             if (done[i][j] != -1) cout << left <<setw(2) << done[i][j];
    55             else cout << left << setw(2) << "";
    56         }
    57         cout << endl;
    58     }
    59     
    60     for (int i = 0; i <= n+1; i++)
    61         for (int j = 0; j <= n + 1; j++)
    62             if (i == 0 || i == n + 1 || j == 0 || j == n + 1) done[i][j] = -10;
    63     int ttx = ex, tty = ey;
    64     int step = done[tty][ttx];
    65     stack<char> result;
    66     while (step)
    67     {
    68         step--;
    69         if (done[tty][ttx-1] == step) {
    70             //cout << "L"; 
    71             ttx--; result.push('R');
    72         }else
    73         if (done[tty][ttx+1] == step) {
    74             //cout << "R"; 
    75             ttx++; result.push('L');
    76         }else
    77         if (done[tty-1][ttx] == step) {
    78             //cout << "U"; 
    79             tty--; result.push('D');
    80         }else
    81         if (done[tty+1][ttx] == step) {
    82             //cout << "D"; 
    83             tty++; result.push('U');
    84         }
    85     }
    86     while (!result.empty())
    87     {
    88         cout << result.top();
    89         result.pop();
    90     }
    91 
    92     return 0;
    93 }

    运用技术:BFS算法/栈结构和队列结构

    程序宅男
  • 相关阅读:
    LeetCode 169. Majority Element
    Object-c block块
    Object-c动态特性
    Object-c基础(2)
    Object-c基础
    LeetCode171:Excel Sheet Column Number
    LeetCode242:Valid Anagram
    LeetCood8:String to Integer
    理解qsort 中的 cmp
    google goble cache
  • 原文地址:https://www.cnblogs.com/dreamcenter/p/13592475.html
Copyright © 2020-2023  润新知