香蕉那个不拿拿 发表于 2023-8-30 13:57:45

问题 编译失败

在本地编译器可以正常编译,但是提交洛谷后显示编译错误。
#include<bits/stdc++.h>
using namespace std;
int book,arr,next={{0,-1},{1,0},{0,1}};
int maxn = -9999999,n,m;
void dfs(int x,int y,int sum){
        if(x == m && y == n){
                if(sum > maxn) maxn = sum;
                return ;
        }
        int tx,ty;
        for(int i = 0;i < 3;i++){
                tx = x + next;
                ty = y + next;
                if(tx < 1 || tx > m || ty < 1 || ty > n){
                        continue;
                }
                if(book == 0){
                        book = 1;
                        dfs(tx,ty,sum+arr);
                        book = 0;
                }
        }
}
int main(){
        cin>>n>>m;
        for(int i = 1;i <=n;i++){
                for(int j = 1;j <= m;j++){
                        cin>>arr;
                }
        }
        dfs(1,1,arr);
        cout<<maxn;
}
https://www.luogu.com.cn/problem/P7074

isdkz 发表于 2023-8-30 14:00:10

问题的原因是代码中定义了一个名为 next 的数组,但是 next 是C++标准库中的一个函数名,因此会导致编译错误。为了解决这个问题,我们需要将数组名更改为其他名称。

以下是修改后的代码:

#include<bits/stdc++.h>
using namespace std;
int book,arr,nextPos={{0,-1},{1,0},{0,1}};
int maxn = -9999999,n,m;
void dfs(int x,int y,int sum){
    if(x == m && y == n){
      if(sum > maxn) maxn = sum;
      return ;
    }
    int tx,ty;
    for(int i = 0;i < 3;i++){
      tx = x + nextPos[ i];
      ty = y + nextPos[ i];
      if(tx < 1 || tx > m || ty < 1 || ty > n){
            continue;
      }
      if(book == 0){
            book = 1;
            dfs(tx,ty,sum+arr);
            book = 0;
      }
    }
}
int main(){
    cin>>n>>m;
    for(int i = 1;i <=n;i++){
      for(int j = 1;j <= m;j++){
            cin>>arr[ i];
      }
    }
    dfs(1,1,arr);
    cout<<maxn;
}


通过将 next 数组重命名为 nextPos ,代码将不再与C++标准库中的函数名冲突,从而解决了编译错误的问题。

陈尚涵 发表于 2023-8-30 14:06:14

啥错误?

香蕉那个不拿拿 发表于 2023-8-30 15:21:24

陈尚涵 发表于 2023-8-30 14:06
啥错误?

没错误,你试试放在编译器里,一点错没有;
但是提交后就编译错误,

陈尚涵 发表于 2023-8-30 17:53:50

香蕉那个不拿拿 发表于 2023-8-30 15:21
没错误,你试试放在编译器里,一点错没有;
但是提交后就编译错误,

提交后显示什么错误?

香蕉那个不拿拿 发表于 2023-8-30 18:52:21

陈尚涵 发表于 2023-8-30 17:53
提交后显示什么错误?

解决了,与c++的函数名重了
页: [1]
查看完整版本: 问题 编译失败