UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#212728#3828. Bwangliwen123456Compile Error//C++1.5kb2024-10-20 10:11:202024-10-20 14:36:56

answer

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;
vector<vector<int>> matrix;
vector<bool> visited;
vector<int> currentPath;
vector<int> longestPath;
int maxLength = 0;

// 判断坐标是否合法
bool isValid(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < m;
}

// 深度优先搜索
void dfs(int x, int y, int length) {
    visited[x * m + y] = true;
    currentPath.push_back(matrix[x][y]);

    if (length > maxLength) {
        maxLength = length;
        longestPath = currentPath;
    } else if (length == maxLength && currentPath < longestPath) {
        longestPath = currentPath;
    }

    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
        int newX = x + dx[i];
        int newY = y + dy[i];
        if (isValid(newX, newY) &&!visited[newX * m + newY] && matrix[newX][newY] > matrix[x][y]) {
            dfs(newX, newY, length + 1);
        }
    }

    visited[x * m + y] = false;
    currentPath.pop_back();
}

int main() {
    cin >> n >> m;
    matrix.resize(n, vector<int>(m));
    visited.resize(n * m, false);
    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++) {
            dfs(i, j, 1);
        }
    }

    for (int num : longestPath) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

详细

answer.code:8:18: error: '>>' should be '> >' within a nested template argument list
 vector<vector<int>> matrix;\x0d
                  ^
answer.code: In function 'int main()':
answer.code:61:20: error: range-based 'for' loops are not allowed in C++98 mode
     for (int num : longestPath) {\x0d
                    ^