可以试试这个
查看原帖
可以试试这个
1353273
OvOhappy楼主2024/8/23 14:54

可以试试这个

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct Point {
    int row, col;
};

const int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

int main() {
    int R, C;
    cin >> R >> C;
    vector<vector<char>> grid(R, vector<char>(C));
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++) {
            cin >> grid[i][j];
        }
    }
    int A, B;
    cin >> A >> B;

    queue<Point> q;
    vector<vector<bool>> visited(R, vector<bool>(C, false));
    q.push({A, B});
    visited[A][B] = true;
    int totalValue = 0;
    while (!q.empty()) {
        Point curr = q.front();
        q.pop();
        if (grid[curr.row][curr.col] == 'S') totalValue += 1;
        else if (grid[curr.row][curr.col] == 'M') totalValue += 5;
        else if (grid[curr.row][curr.col] == 'L') totalValue += 10;
        for (int d = 0; d < 4; d++) {
            int newRow = curr.row + dirs[d][0];
            int newCol = curr.col + dirs[d][1];
            if (newRow >= 0 && newRow < R && newCol >= 0 && newCol < C &&!visited[newRow][newCol] && grid[newRow][newCol]!= '*') {
                q.push({newRow, newCol});
                visited[newRow][newCol] = true;
            }
        }
    }
    cout << totalValue << endl;
    return 0;
}
2024/8/23 14:54
加载中...