Skip to content

Problem D: 苦肉浊林的蠕动霸主

📝 题目描述

🔑 算法模块

模拟

💡 思路分析

🖥️ 代码实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, m;
    cin >> n >> m;
    vector<string> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<int, int>> b(10, make_pair(-1, -1)); 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char c = a[i][j];
            if (c >= '0' && c <= '9') {
                int num = c - '0';
                b[num] = make_pair(i, j);
            }
        }
    }
    string ops;
    cin >> ops;
    int x = 0, y = 0;
    bool ok = false; 
    for (char c : ops) {
        if (ok) break; 
        int nx = x, ny = y;
        if (c == 'L') ny--;
        else if (c == 'R') ny++;
        else if (c == 'U') nx--;
        else if (c == 'D') nx++;
        if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
            continue; 
        }
        if (a[nx][ny] == '#') {
            x = nx; y = ny;
            ok = true; 
            break;
        } else if (a[nx][ny] == '.') {
            x = nx; y = ny; 
        } else { 
            x = nx; y = ny; 
            int p = a[x][y] - '0';
            int q = p + 1;
            if (q > 9) {
                continue; 
            }
            if (b[q].first == -1) {
                continue;
            }
            x = b[q].first;
            y = b[q].second;
        }
    }
    cout << x + 1 << " " << y + 1 << endl;
    return 0;
}

⏱️ 复杂度分析

📚 拓展