8 条题解

  • 0
    @ 2025-3-30 21:38:24
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    const string target = "tencent";
    int n, m;
    vector<vector<char>> matrix;
    int directions[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
    int ans = 0;
    
    void dfs(int x, int y, int step) {
        if (step == 6) {
            ans++;
            return;
        }
    
        for (int i = 0; i < 4; i++) {
            int nx = x + directions[i][0];
            int ny = y + directions[i][1];
            if (nx >= 0 && nx < n && ny >= 0 && ny < m &&
            matrix[nx][ny] == target[step + 1]) {
                dfs(nx, ny, step + 1);
            }
        }
    }
    
    int main() {
        cin >> n >> m;
        matrix.resize(n, vector<char>(m));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> matrix[i][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (matrix[i][j] == 't') {
                    dfs(i, j, 0);
                }
            }
        }
        cout << ans << endl;
        return 0;
    }
    

    信息

    ID
    81
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    309
    已通过
    84
    上传者