1 条题解

  • 0
    @ 2022-12-8 13:24:27

    广搜做法:

    #include <bits/stdc++.h>
    using namespace std;
    int n , m , b[25][25] , ans = 450 , next[4][2] = {{1 , 0} , 
                                                      {0 , 1} , 
                                                      {-1 , 0} , 
                                                      {0 , -1}};
    char c[25][25];
    struct point
    {
        int x , y , time;
    };
    queue<point> s;
    void bfs()
    {
        while (s.size())
        {
            point now = s.front();
            s.pop();
            for (int i = 0 ; i < 4 ; i++)
            {
                int tx = now.x + next[i][0] , ty = now.y + next[i][1];
                if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && b[tx][ty] == 0 && c[tx][ty] != '#')
                {
                    if (c[tx][ty] == 'a')
                    {
                        ans = min(ans , now.time + 1);
                    }
                    b[tx][ty] = 1;
                    if (c[tx][ty] == 'x') s.push((point) {tx , ty , now.time + 2});
                    else s.push((point) {tx , ty , now.time + 1});
                }
            }
        }
        return;
    }
    
    int main()
    {
        cin >> n >> m;
        for (int i = 1 ; i <= n ; i++)
        {
            for (int j = 1 ; j <= m ; j++)
            {
                cin >> c[i][j];
                if (c[i][j] == 'r')
                {
                    s.push((point) {i , j , 0});
                    b[i][j] = 1;
                }
            }
        }
        bfs();
        if (ans == 450) cout << "Impossible";
        else cout << ans;
        return 0;
    }//已AC
    
    • 1

    信息

    ID
    897
    时间
    1000ms
    内存
    128MiB
    难度
    3
    标签
    递交数
    33
    已通过
    19
    上传者