3 条题解

  • 1
    @ 2025-5-28 20:48:33
    #include <iostream>
    #include <vector>
    #include <queue>
    #include <climits>
    using namespace std;
    
    struct sttr {
        int x, y;
        int time;
        sttr(int _x, int _y, int _t) : x(_x), y(_y), time(_t) {}
    };
    
    int main() {
        int t, x, y;
        cin >> t >> x >> y;
        vector<string> grid(y);
        sttr start(-1, -1, 0), end(-1, -1, 0);
        for (int i = 0; i < y; ++i) {
            cin >> grid[i];
            for (int j = 0; j < x; ++j) 
                if (grid[i][j] == 's') start = sttr(j, i, 0);
                else if (grid[i][j] == 'm') end = sttr(j, i, 0);        
        }
        if (start.x == -1 || end.x == -1) {
            cout << "55555" << endl;
            return 0;
        }
        const int dx[] = {0, 0, -1, 1};
        const int dy[] = {-1, 1, 0, 0};
        vector<vector<int>> vv(y, vector<int>(x, INT_MAX));
        vv[start.y][start.x] = 0;
        queue<sttr> q;
        q.push(start);
        while (!q.empty()) {
            sttr str = q.front();
            q.pop();
            if (str.x == end.x && str.y == end.y) {
                if (str.time <= t) cout << str.time << endl;
                 else cout << "55555" << endl;
                return 0;
            }
            for (int i = 0; i < 4; ++i) {
                int nx = str.x + dx[i];
                int ny = str.y + dy[i];
                if (nx >= 0 && nx < x && ny >= 0 && ny < y && grid[ny][nx] != 'o') {
                    int new_time = str.time;
                    if (grid[ny][nx] == '.') 
                        new_time += 1;
                     else if (grid[ny][nx] == '#') new_time += 2;
                     else if (grid[ny][nx] == 'm') new_time += 1; 
                    if (new_time < vv[ny][nx]) {
                        vv[ny][nx] = new_time;
                        q.push(sttr(nx, ny, new_time));
                    }
                }
            }
        }
        cout << "55555";
    }
    
    

    信息

    ID
    3053
    时间
    2000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    139
    已通过
    23
    上传者