跳到正文

个人记录

题目

LCXYYDSNB约 14 分钟
原文
文章信息与版本

文章信息

发布
2025年5月14日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 14 分钟

版本

  1. v1当前

第一题:

C++
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;

struct Node {
    int time, x, y;
    Node(int t, int i, int j) : time(t), x(i), y(j) {}
    bool operator<(const Node& other) const {
        return time > other.time;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int S;
    cin >> S;
    while (S--) {
        int N, M;
        cin >> N >> M;
        vector<vector<char>> grid(N, vector<char>(M));
        pair<int, int> start, end;
        bool found_start = false, found_end = false;
        
        for (int i = 0; i < N; ++i) {
            string row;
            cin >> row;
            for (int j = 0; j < M; ++j) {
                grid[i][j] = row[j];
                if (grid[i][j] == 'r') {
                    start = {i, j};
                    found_start = true;
                } else if (grid[i][j] == 'a') {
                    end = {i, j};
                    found_end = true;
                }
            }
        }
        
        if (!found_start || !found_end) {
            cout << "Impossible\n";
            continue;
        }
        
        vector<vector<int>> dist(N, vector<int>(M, INT_MAX));
        priority_queue<Node> pq;
        dist[start.first][start.second] = 0;
        pq.emplace(0, start.first, start.second);
        
        int dx[] = {-1, 1, 0, 0};
        int dy[] = {0, 0, -1, 1};
        bool found = false;
        int result = -1;
        
        while (!pq.empty()) {
            Node node = pq.top();
            pq.pop();
            
            int x = node.x;
            int y = node.y;
            int current_time = node.time;
            
            if (x == end.first && y == end.second) {
                result = current_time;
                found = true;
                break;
            }
            
            if (current_time > dist[x][y]) {
                continue;
            }
            
            for (int k = 0; k < 4; ++k) {
                int ni = x + dx[k];
                int nj = y + dy[k];
                
                if (ni < 0 || ni >= N || nj < 0 || nj >= M) {
                    continue;
                }
                
                if (grid[ni][nj] == '#') {
                    continue;
                }
                
                int add_time = (grid[ni][nj] == 'x') ? 2 : 1;
                int new_time = current_time + add_time;
                
                if (new_time < dist[ni][nj]) {
                    dist[ni][nj] = new_time;
                    pq.emplace(new_time, ni, nj);
                }
            }
        }
        
        if (found) {
            cout << result << "\n";
        } else {
            cout << "Impossible\n";
        }
    }    
    return 0;
}

第二题

C++
#include <iostream>
#include <queue>
#include <climits>
using namespace std;

struct Node {
    int x, y, t, time;
};

int main() {
    int M, N, T;
    cin >> M >> N >> T;
    char grid[200][200];
    int start_x, start_y, end_x, end_y;

    for (int i = 0; i < M; ++i) {
        string s;
        cin >> s;
        for (int j = 0; j < N; ++j) {
            grid[i][j] = s[j];
            if (s[j] == '@') {
                start_x = i;
                start_y = j;
            } else if (s[j] == '+') {
                end_x = i;
                end_y = j;
            }
        }
    }

    int visited[200][200][10];
    for (int i = 0; i < 200; ++i)
        for (int j = 0; j < 200; ++j)
            for (int k = 0; k < 10; ++k)
                visited[i][j][k] = INT_MAX;

    queue<Node> q;
    visited[start_x][start_y][T] = 0;
    q.push({start_x, start_y, T, 0});

    const int dx[] = {-1, 1, 0, 0};
    const int dy[] = {0, 0, -1, 1};

    while (!q.empty()) {
        Node current = q.front();
        q.pop();

        for (int i = 0; i < 4; ++i) {
            int nx = current.x + dx[i];
            int ny = current.y + dy[i];

            if (nx < 0 || nx >= M || ny < 0 || ny >= N) continue;

            if (nx == end_x && ny == end_y) {
                cout << current.time + 1 << endl;
                return 0;
            }

            char cell = grid[nx][ny];
            if (cell == '#') {
                if (current.t > 0) {
                    int new_t = current.t - 1;
                    int new_time = current.time + 1;
                    if (new_time < visited[nx][ny][new_t]) {
                        visited[nx][ny][new_t] = new_time;
                        q.push({nx, ny, new_t, new_time});
                    }
                }
            } else {
                int new_t = current.t;
                int new_time = current.time + 1;
                if (new_time < visited[nx][ny][new_t]) {
                    visited[nx][ny][new_t] = new_time;
                    q.push({nx, ny, new_t, new_time});
                }
            }
        }
    }

    cout << -1 << endl;
    return 0;
}

网格那道

C++
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std;

struct Cell {
    int r, c, e;
    Cell(int r, int c, int e) : r(r), c(c), e(e) {}
};

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> grid(H);
    int si, sj, ti, tj;
    for (int i = 0; i < H; ++i) {
        cin >> grid[i];
        for (int j = 0; j < W; ++j) {
            if (grid[i][j] == 'S') {
                si = i;
                sj = j;
            } else if (grid[i][j] == 'T') {
                ti = i;
                tj = j;
            }
        }
    }
    int N;
    cin >> N;
    unordered_map<int, unordered_map<int, int>> med;
    for (int i = 0; i < N; ++i) {
        int r, c, e;
        cin >> r >> c >> e;
        med[r-1][c-1] = e;
    }
    
    vector<vector<int>> max_e(H, vector<int>(W, -1));
    queue<Cell> q;
    q.emplace(si, sj, 0);
    max_e[si][sj] = 0;
    
    const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    bool found = false;
    
    while (!q.empty()) {
        Cell cell = q.front();
        q.pop();
        
        if (cell.r == ti && cell.c == tj) {
            found = true;
            break;
        }
        
        if (med.count(cell.r) && med[cell.r].count(cell.c)) {
            int Ei = med[cell.r][cell.c];
            if (Ei > cell.e) {
                int new_e = Ei;
                if (new_e > max_e[cell.r][cell.c]) {
                    max_e[cell.r][cell.c] = new_e;
                    q.emplace(cell.r, cell.c, new_e);
                }
            }
        }
        
        for (int d = 0; d < 4; ++d) {
            int ni = cell.r + dir[d][0];
            int nj = cell.c + dir[d][1];
            if (ni < 0 || ni >= H || nj < 0 || nj >= W) continue;
            if (grid[ni][nj] == '#') continue;
            int new_e = cell.e - 1;
            if (new_e < 0) continue;
            if (new_e > max_e[ni][nj]) {
                max_e[ni][nj] = new_e;
                q.emplace(ni, nj, new_e);
            }
        }
    }
    
    cout << (found ? "Yes" : "No") << endl;
    
    return 0;
}
原文